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?

This is what I did so far...

#include <iostream>
using namespace std;

void fillup(double GPAs[], int size, int numitems);

int main()
{
    const int x=6;
    double gpa[x];
    int numitems = 0;

    fillup(gpa, x, numitems);
    cout<<"\n-----------------\n";
    for ( int i=0; i<x; ++i )
        cout<<gpa[i]<<endl;
}
void fillup(double GPAs[], int size, int numitems)
{

for (int i=0; i<size; i++)
        cin>> gpa[i];
}
Member Avatar for iamthwee

You need a sort algo.

Look up bubble sort. You will also need to use a nested for loop eg:

for
{
    for
    {

    }
}

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:

#include <iostream>
#include <cstdlib>
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);


int main()
{
    const int s=30;//to Exit enter "-1"
    int STUDENT_ID[s];
    double GPA[s];
    int num=0;
    int y;

    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 ids;
    int i= 0;
    SIZE = 0;

    cout<< "Enter IDs: " ;
    cin >> ids;

    while (ids>10000 && i<size)
    {
        id[i]=ids;
        i++;
        SIZE++;
        cin>>ids;
    }

}
void studentgpa(double gpa[], int size, int & SIZE)
{
    double GPAs;
    int i= 0;
    cout<< "Enter GPAs: " ;
    cin >> GPAs;

    while(GPAs > 0 && i<SIZE)
    {
        gpa[i] = GPAs;
        i++;
        cin>> GPAs;
    }
}
void swap(int v1,int & v2)
{
    int Z;
    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++)

        if (id[i]<min)

        {

            min = id[i];
            MIN = i;


        }

    return MIN;
}
void sort( int id[], int & Index, int & SIZE)
{
    int k;

    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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.