a C++ program that reads in two partially filled arrays. One array contains student IDs (with int type) and the other contains GPAs (with double type). The program will sort the two arrays according to the student ID and print out the results?
using namespace std;
void fillup(double GPAs, int size, int numitems);
const int x=6;
int numitems = 0;
fillup(gpa, x, numitems);
for ( int i=0; i<x; ++i )
void fillup(double GPAs, int size, int numitems)
for (int i=0; i<size; i++)
You don't have an array for the student IDs in your code. I assume that fillup is expected to fill that array at the same time it fills the GPAs. One would expect that the student ID values would be interleaved with GPA values in the input data (ID, GPA, ID, GPA, ...). Whenever you swap the Ith and Jth ID, you need to swap the Ith and Jth GPA in your sort procedure.
It also seems odd that you would set the number of GPAs to a constant 6. I expect that would be an input value, perhaps the first value from cin (prior to the data) or else you need to determine that by counting the GPAs input until EOF is reached. If it is determined based on EOF being encountered then you have other changes that will be needed since you will not know the size of the array until after all data is input.
You have a variables 'size' and 'numitems' as parameters in your fillup() function, but the latter is not used in your code.
Whichever sort algorithm you use you have 2 main choices, either change the gpa array with the same index parameters you're changing the ID array with. Or, use a custom class and put the data in an array or vector of that class. This way the data is strongly coupled and one is always with the other.
Well, I made some improvments and the qutput is so weird:
using namespace std;
void studentid ( int id, int size, int & SIZE);
void studentgpa ( double gpa, int size, int & SIZE);
int index( int id, int Index, int SIZE);
void swap( int v1, int & v2);
void sort( int id, int &Index, int & SIZE);
const int s=30;//to Exit enter "-1"
studentid (STUDENT_ID, s, y);
studentgpa (GPA, s, y);
index(STUDENT_ID, num, y);
sort(STUDENT_ID, num, y);
for (int i=0; i<y; i++)
cout<< STUDENT_ID[i]<< " = " << GPA[i]<<endl;
void studentid (int id, int size, int &SIZE)
int i= 0;
SIZE = 0;
cout<< "Enter IDs: " ;
cin >> ids;
while (ids>10000 && i<size)
void studentgpa(double gpa, int size, int & SIZE)
int i= 0;
cout<< "Enter GPAs: " ;
cin >> GPAs;
while(GPAs > 0 && i<SIZE)
gpa[i] = GPAs;
void swap(int v1,int & v2)
Z = v1;
v1 = v2;
v2 = Z;
int index( int id, int Index, int SIZE)
int min = id[Index],
MIN = Index;
for ( int i= Index+1; i< SIZE; i++)
min = id[i];
MIN = i;
void sort( int id, int & Index, int & SIZE)
for ( int i =0; i<(SIZE-1); i++)
k = index(id, Index, SIZE);
swap ( id[i] , id[k]);
cout << "The sorted order is:\n" ;
for (int i = 0;i<SIZE; i++)
cout << id[i] <<endl;
There are lots of design issues here. The printout in the sort algorithm I assume is just for debugging. The sort algorithm only sorts the ID list and therefore the GPAs don't get sorted. Whenever the i and k elements are swapped in the ID list they need to be swapped in the GPA list. Apparently you are being asked to put them in ascending order of ID's rather than based on the GPA values since that is what you are doing. In my mind, the studentID and studentGPA functions should be combined into one getData function since there should be one GPA for each ID, and that is the most logical way to get the data, in pairs.
An earlier poster suggested coupling them in pairs and having a list of those pairs, but I assume you are not that far along in your C++ so I've not suggested that. But you do need to have the idea of side-by-side arrays that have one-to-one correspondences between the arrays and you need to make swaps that maintain that correspondence.
My preference for naming functions is "Verb Object" form. I already mentioned "getData" idea. I would change the name of "index()" to "getMinIndex()". The sort and swap names are okay.