I have to write a C++ function for Bubble and Selection sort algorithms for stri
ID: 3649706 • Letter: I
Question
I have to write a C++ function for Bubble and Selection sort algorithms for strings. Each function gets string as an argument and returns string array as output.#include<iostream>
#include<string>
#include <stdio.h>
#include <conio.h>
using namespace std;
bool compareString(char str1, string str2)
{int i = 0;
while(i < str1.lenght() && i < str2. length(), i++){
if (str1[i] > str2[i])
return true;
if (str1[i] < str2[i])
return false;
}
}
void sortArray(char array[], int size)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 0; count < (size -1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count+1] = temp;
swap = true;
}
}
}while(swap);
}
void selectionSort(char array[], int size)
{
int startScan, minIndex, minValue;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
Errors
1>c:usersjamesdesktoplab4james_johnson_a4-1.cpp(14) : error C2228: left of '.lenght' must have class/struct/union
1> type is 'char'
1>c:usersjamesdesktoplab4james_johnson_a4-1.cpp(14) : warning C4018: '<' : signed/unsigned mismatch
1>c:usersjamesdesktoplab4james_johnson_a4-1.cpp(14) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Explanation / Answer
t work we have a C# solution with over 80 projects. In VS 2008 we use a macro to stop the compile as soon as a project in the solution fails to build (see this question for several options for VS 2005 & VS 2008: http://stackoverflow.com/questions/134796/how-to-automatically-stop-visual-c-build-at-first-compile-error).