hi guys!!! i have written small program here about customers, its for my assignment, emmm there's a small problem, i am using merge sort to sort customers in order by their ID, the only thing i dont know how to do i how would i pass ID into merge sort, so it will use it as sorting parameter, program runs, but doesn't show the correct output!!!if anyone know the solution for it, please help me with it.
thanks in advance!!
here' the code:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <string>
using namespace std;
struct student
{
int ID;
string name;
string address;
int courseCode;
string courseTitle;
};
typedef struct student S;
//***SORTING FUNCTION***
//int a[50];
void merge(int,int,int);
void merge_sort(int low,int high);
int ID[10];
//***SORTING FUNCTION***
//*** STUDENT DETAILS FUNCTION***
void enterDetails(S * sArray, int numStudents);
void printDetails(S * sArray, int numStudents);
//***STUDENT DETAILS FUNCTION***
//-----------------------------------------------
int main()
{
int menuChoice = 0;
int numStudents;
cout << "Enter how many students do you require: " << endl;
cin >> numStudents;
system("CLS");
S * sArray = new S[numStudents];
int i;
while (menuChoice != 4)
{
cout << "\n" << setw (61) << "*** CUSTOMER DETAILS TABLE ***\n\n" << endl;
cout << setw (50) << "1 -- Add New Cutomer\n" << endl;
cout << setw (57) << "2 -- Display Customer Table\n" << endl;
cout << setw (48) << "3 -- Sort Students\n" << endl;
cout << setw (52) << "4 -- Quit the program\n\n" << endl;
cout << setw (54) << " Please make your choice: ";
cin >> menuChoice;
system("CLS");
if (menuChoice == 1)
{
cin.ignore(); //int following a string
for(int i =0; i < numStudents; i++)
{
cout << setw (43) << "Enter Student "<<i+1<<" Name: ";
getline(cin, sArray[i].name);
cout << setw (47) << "Enter Student ID: ";
cin >> sArray[i].ID;
cout << setw (52) <<"Enter Student Address: ";
cin >> sArray[i].address;
cout << setw (48) <<"Enter Course Code: ";
cin >> sArray[i].courseCode;
cout << setw (49) <<"Enter Course Title: ";
cin >> sArray[i].courseTitle;
system("CLS");
cin.ignore();//int following a string
}
}
else if (menuChoice == 2)
{
cout << "ID \t" << "Name \t\t" << "Address \t" << "Course Code \t" << "Course Title \t" << endl;
for(int i =0; i < numStudents; i++)
{
cout << sArray[i].ID << "\t" << sArray[i].name << "\t\t" << sArray[i].address << "\t\t" << sArray[i].courseCode << "\t\t" << sArray[i].courseTitle << endl;
cin.ignore();
}
system("pause");
system("CLS");
}
else if(menuChoice == 3)
{
int ID,i;
/*cout << "********************************************************************************" << endl;
cout << setw(50) << " MERGE SORT PROGRAM " << endl;
cout << "********************************************************************************" << endl;
cout<<endl<<endl;
cout << "Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:" << endl;
cin>>numStudents;
cout<<endl;
cout << "Now, Please Enter the ( "<< numStudents <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:" << endl;
for(i=1;i<=numStudents;i++)
{
cin>>ID[i] ;
}
merge_sort(1,numStudents);
cout << endl;
cin.get();
*/
cout << "So, the sorted list (using MERGE SORT) will be :"<<endl;
cout << endl << endl;
for(i=1;i<=numStudents;i++)
cout << ID << " ";
cout << endl << endl << endl << endl;
system("pause");
}
}
return 0;
enterDetails(sArray, numStudents);
printDetails(sArray, numStudents);
}
//----------------------------------------------
void enterDetails( S * sArray, int numStudents)
{
cin.ignore(); //int following a string
for(int i =0; i < numStudents; i++)
{
cout << "Enter Student Name: "<< i+1 << endl;
getline(cin, sArray[i].name);
cout << "Enter Student ID: " << endl;
cin >> sArray[i].ID;
cout << "Enter Student Address: " << endl;
cin >> sArray[i].address;
cout << "Enter Course Code: " << endl;
cin >> sArray[i].courseCode;
cout << "Enter Course Title: " << endl;
cin >> sArray[i].courseTitle;
cin.ignore();//int following a string
}
//system("pause");
}
void printDetails( S * sArray, int numStudents)
{
for(int i =0; i < numStudents; i++)
{
system("pause");
cout << "Name \t" << sArray[i].name << endl;
cout << "ID \t" << sArray[i].ID << endl;
cout << "Address \t" << sArray[i].address << endl;
cout << "Course Code \t" << sArray[i].courseCode << endl;
cout << "Course Title\t" << sArray[i].courseTitle << endl;
cin.ignore();
}
}
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int ID[10];
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(ID[h]<=ID[j])
{
b[i]=ID[h];
h++;
}
else
{
b[i]=ID[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=ID[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=ID[k];
i++;
}
}
for(k=low;k<=high;k++) ID[k]=b[k];
}