I am trying something similar to quicksort.
I wrote this but it is giving me some errors:
Code:
#include<iostream>
#include<algorithm>
void sort();
void display();
char names[5][20];
using namespace std;
int main()
{
char temp[20];
int x=0,y=0;
for(x=0;x<=4;x++)
{
cout<<endl<<"Enter the "<<x<<"th name";
cin>>temp; //I used this temporary string for reason I can't explain in words!
for(y=0;temp[y]!='\0';y++)
{
names[x][y]=temp[y];
}
names[x][y]='\0';
} //I have tested the code upto this point.I could make five inputs and then print them one by one.
sort();
display();
cin.get();
return 0;
}
void sort()
{
int x,y,var,num1,num2;
for(x=0;x<4;x++)
{
for(var=x+1;var<=4;var++)
{
for(y=0;((names[x][y]!='\0')&&(names[var][y]!='\0'));y++)
{
num1=(int)names[x][y]; //This is to get the ASCII value
num2=(int)names[var][y];//This is to get the ASCII value
if(num1>num2)
{
swap(names[x],names[var]);
}
}
}
}
}
void display()
{
for(int x=0;x<=4;x++)
{
cout<<endl<<names[x];
}
}
I know I have not made the code readable.....sorry for that
Code:
The error messeges are:
C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h In function `void std::swap(_Tp&, _Tp&) [with _Tp = char[20]]':
43 C:\Dev-Cpp\alpha.cpp instantiated from here
130 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h invalid initializer
131 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h ISO C++ forbids assignment of arrays
132 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h ISO C++ forbids assignment of arrays
What do you think is the problem? (If you can read that bad coding)
Thanks!