I am new for c++. I was working on a small program that requires to read items from a text file and print out the sorted list using quicksort. The items to be sorted include names and ages(double sort).; I tried it to some extent but the program is not responding at all(it just prints the list as is)
my program is :
//Read file of name and year born. Print name and age
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int readfile(ifstream& members,char names[][20],int years[])
{ int count=0;
while (!members.eof() && count<29) //eof=end of file
{ members>>names[count];
members>>years[count];
count++;
} //while not end of file
members.close();
return count;
} //readfile
void Partition(char names[][20], int iSize) {
// Partitions of size 0 or 1 are already sorted
if (iSize <= 1) {
return;
}
// Select the pivot randomly, but not the first element
int iLower = 0;
int iPivot = names[iLower][20];//(rand() % (iSize - 1)) + 1][20];
// Indices of the entries to be swapped
int iUpper = iSize;
// Partition array into sections above and below the pivot
while (iLower < iUpper) {
do {
++iLower;
} while (names[iLower][20] < iPivot);
do {
--iUpper;
} while (names[iUpper][20] > iPivot);
// Swap the entries at the lower and upper indices
if (iLower < iUpper) {
int iTemp = names[iLower][20];
names[iLower][20] = names[iUpper][20];
names[iUpper][20] = iTemp;
}
}
// Recursively call partition on each partititon.
Partition(names, iLower);
Partition(&(names[iLower]), iUpper - iLower);
}
void Quicksort(char names[][20],int years[], int iSize) {
// Seed the random number generator
srand((unsigned int)time(0));
void Partition(char names[][20],int years[], int iSize);
}
void listAll(char names[][20],int years[], int count)
{ int age;
cout<<"Name\t\tAge\n\n";
for(int i=0; i<count; i++)
{ age=2013-years[i];
cout<<names[i]<<"\t\t"<<age<<"\n";
} //for loop
} //listAll
void main(void)
{ ifstream names; //input file stream
char members[30][20]; //allow 30 members with 14 letters (12-1) for name
int year[31]; //yearborn for same 20 members
int numMembers; //number of members, there may be less than 20
int i=0;
names.open("names.txt"); //associates the name inch with a file
if (names.fail()) //maybe no disk in A:, or the file does not exist
cout<<"File not found\n"; //cout is used to notify user of error
else
{ numMembers=readfile(names, members,year);
listAll(members, year, numMembers);
Partition(members, numMembers);
Quicksort(members, year, numMembers);
In this line: void Partition(char names[][20],int years[], int iSize);
you are re-declaring your arguments, essentially hiding the arguments you put into the function. Therefore, your call to Partition isn't actually sorting the list you passed in. Try: Partition(names, years, iSize);
Edit: In fact, that exposes another problem with your code, which is that your Partition function doesn't handle three arguments. Are you trying to sort by name and by age separately, or are your supposed to be using age for tie-breakers?
thanks I improved my code and could sort the names, but the age stayed unsorted. I am supposed to sort both of them without messing the name age combination. my new code is:
//Read file of name and year born. Print name and age
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int readfile(ifstream& members,char names[][20],int years[])
{ int count=0;
while (!members.eof() && count<29) //eof=end of file
{ members>>names[count];
members>>years[count];
count++;
} //while not end of file
members.close();
return count;
} //readfile
void listAll(char names[][20],int years[], int count)
{ int age;
cout<<"Name\t\tAge\n\n";
for(int i=0; i<count; i++)
{ age=2013-years[i];
cout<<names[i]<<"\t\t"<<age<<"\n";
} //for loop
} //listAll
void quickSort(char names[][20],int years[], int left, int right) {
int i =left, j = right;
int tmp;
int pivot = names[i][20];
while (i <= j) {
while (names[i][20] < pivot)
i++;
while (names[j][20] > pivot)
j--;
if (i <= j) {
tmp = names[i][20];
names[i][20] = names[j][20];
names[j][20] = tmp;
i++;
j--;
}
}
if (left < j)
quickSort(names,years, left, j);
if (i < right)
quickSort(names,years, i, right);
}
void main(void)
{ ifstream names; //input file stream
char members[29][20]; //allow 30 members with 14 letters (12-1) for name
int year[29]; //yearborn for same 20 members
int numMembers; //number of members, there may be less than 20
int i=0;
names.open("names.txt"); //associates the name inch with a file
if (names.fail()) //maybe no disk in A:, or the file does not exist
{
cout<<"File not found\n"; //cout is used to notify user of error
}
else
{ numMembers=readfile(names,members,year);
quickSort(members, year,i, numMembers);
listAll(members, year, numMembers);
}
system("pause");
} //main