i am trying to import into a 2d array the following:


line one : gpa e.x.[2.3,5.1,....]
^ ^
| |
line two : id e.x.[1 ,2 ,....]

and after that to sort the gpa in a descending way but in the same time the id to follow each ones gpa.


could someone help me out?

Recommended Answers

All 3 Replies

the gpa and id could be better if they where 2 different arrays and when the gpa is sorted the id would change accordingly.

1) create an array of struct objects that will hold ID and GPA information:

struct Student
{
     int id;
     double gpa;
     
}data[50];

2) Now you can fill up your array of structs:

int size=0;
while(infile >> data[size].id)
{
     infile >> data[size].gpa;
     size++;
}

3) Now ye' can sort:

#include<algorithm>

sort(&data[0].gpa, &data[size].gpa);

4) Display your sorted data with great ease:

cout << "ID:      GPA:";
for(int i=0; i<size; i++)
{
     cout << data[i].id << "      " << data[i].gpa << endl;
}

ok you got me:P
me program is made with struct but i didn't know there is a sorting algorithm.
(i am a noobie in coding)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <memory.h>
#include <algorithm>


using namespace std;

struct node
{
    char name[10];
    int age;
    double gpa;
    char sex[1];
    int ID;
    struct node *nextPtr;    // Pointer to next node
    struct node *prevPtr;    // Pointer to previous node
};
typedef struct node Student;
typedef Student *StudentPtr;

void insert( Student **headOfList,  char *name, int age, double gpa, char *sex)  // insert students
{
    static int ID; // id for each student
    StudentPtr newPtr;
    StudentPtr previousPtr;
    StudentPtr nextPtr;
    StudentPtr currentPtr;
    ID++;

    newPtr = ( Student * ) malloc( sizeof( Student ) ); // creation of student

    if ( newPtr != NULL )
    {

        strcpy(newPtr->name, name);
        newPtr->age = age;
        newPtr->gpa = gpa;
        strcpy(newPtr->sex, sex);
        newPtr->ID = ID;

        newPtr->nextPtr = NULL;


        previousPtr = NULL;
        currentPtr = *headOfList;
        //check state of list and insert accordingly
        if ( currentPtr != NULL )
        {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextPtr;
        }

        if ( previousPtr == NULL )
        {
            newPtr->nextPtr = *headOfList;
            *headOfList = newPtr;
        }
        else
        {
            previousPtr->nextPtr = newPtr;
            newPtr->nextPtr = currentPtr;
        }
    }

    else
    {
        printf( "%s not inserted. No memory available.\n", name );
    }
}

void printList( Student *headOfList ) // print all students
{

    while ( headOfList != NULL )
    {

        cout<<"-------------------------------"<< endl;
        cout<<"Student ID:"<< headOfList->ID << endl;
        cout<<"Student name:"<< headOfList->name << endl;
        cout<<"Student age:"<< headOfList->age << endl;
        cout<<"Student G.P.A.:"<< headOfList->gpa << endl;
        cout<<"Student sex:"<< headOfList->sex << endl;
        cout<<"-------------------------------"<< endl;
        headOfList = headOfList->nextPtr;//move to next student in list

    }
    return;


}

void deleteNode( Student  **headOfList, int tempID )// delete student
{
    StudentPtr previousPtr;
    StudentPtr currentPtr;
    StudentPtr tempPtr;
    StudentPtr nextPtr;


    if ( tempID == (*headOfList)->ID )
    {

        tempPtr = (*headOfList);

        if((*headOfList)->nextPtr == NULL)
        {

            free( tempPtr );

        }
        else
        {

            (*headOfList) = (*headOfList)->nextPtr;
            free( tempPtr );

        }

    }
    else
    {
        previousPtr = (*headOfList);
        currentPtr = (*headOfList)->nextPtr;

        if(currentPtr == NULL)
        {
            cout << "student not found" << endl;


        }


        else
        {


            while (currentPtr->nextPtr != NULL && currentPtr->ID != tempID )
            {
                previousPtr = currentPtr;
                currentPtr = currentPtr->nextPtr;
            }

            if ( currentPtr->nextPtr != NULL)
            {
                tempPtr = currentPtr;
                previousPtr->nextPtr = currentPtr->nextPtr;
                previousPtr->nextPtr = currentPtr->nextPtr;
                free( tempPtr );
            }
            else
            {
                tempPtr = currentPtr;
                previousPtr->nextPtr = NULL;
                free( tempPtr );
            }
        }
    }


}

void editStud( Student **headOfList, int studID)//edit student
{

    StudentPtr previousPtr;
    StudentPtr currentPtr;



    char tempName[10];
    int tempAge;
    double tempGpa;
    char tempSex[1];




    if ( studID == (*headOfList)->ID )
    {


        cout << "Please enter the new name" << endl;
        cin >> tempName;
        cout << "Please enter the new age" << endl;
        cin >> tempAge;
        cout << "Please enter the new G.P.A." << endl;
        cin >> tempGpa;
        cout << "Please enter the new sex" << endl;
        cin >> tempSex;


        strcpy((*headOfList)->name, tempName);
        (*headOfList)->age = tempAge;
        (*headOfList)->gpa = tempGpa;
        strcpy((*headOfList)->sex, tempSex);


    }
    else
    {


        currentPtr = (*headOfList)->nextPtr;


        while (currentPtr->nextPtr != NULL && currentPtr->ID != studID )
        {


            currentPtr = currentPtr->nextPtr;
        }


        cout << "Please enter the new name" << endl;
        cin >>tempName;
        cout << "Please enter the new age" << endl;
        cin >>tempAge;
        cout << "Please enter the new G.P.A." << endl;
        cin >>tempGpa;
        cout << "Please enter the new sex" << endl;
        cin >>tempSex;


        strcpy(currentPtr->name, tempName);
        (currentPtr)->age = tempAge;
        (currentPtr)->gpa = tempGpa;
        strcpy(currentPtr->sex, tempSex);

    }



}

int maxGpa(Student *headOfList)
{
    StudentPtr startptr = NULL;
    StudentPtr previousPtr;
    StudentPtr currentPtr;

    currentPtr = headOfList;

    int tempid = 0;
    double tempmax;
    tempmax == currentPtr->gpa;

    while (currentPtr != 0)
    {

        if (currentPtr->gpa > tempmax)
        {

            tempmax = currentPtr->gpa;
            tempid = currentPtr->ID;

        }

        currentPtr = currentPtr->nextPtr;




    }

    return tempid;


}

int minGpa(Student *headOfList)
{
    StudentPtr startptr = NULL;
    StudentPtr previousPtr;
    StudentPtr currentPtr;

    currentPtr = headOfList;

    int tempid = 0;
    double tempmax;
    tempmax == currentPtr->gpa;

    while (currentPtr != NULL)
    {

        if (currentPtr->gpa <= tempmax)
        {

            tempmax = currentPtr->gpa;
            tempid = currentPtr->ID;

        }

        currentPtr = currentPtr->nextPtr;




    }

    return tempid;


}

void allMale(Student *headOfList)
{
    StudentPtr startptr = NULL;
    StudentPtr previousPtr;
    StudentPtr currentPtr;





    int tempid[100];
    currentPtr = headOfList;
    int i = 0;



    while (currentPtr != NULL)
    {

        if (currentPtr->sex[0] == 'm')
        {

            tempid[i] = currentPtr->ID;
            i++;



        }

        currentPtr = currentPtr->nextPtr;
    }

    if (i==0)
    {

        cout<< "No male students" << endl;



    }

    else
    {


        int j = 0;

        while (j <= i)
        {

            if ( tempid[j] == (headOfList)->ID )
            {

                cout<<"-------------------------------"<< endl;
                cout<<"Student ID:"<< headOfList->ID << endl;
                cout<<"Student name:"<< headOfList->name << endl;
                cout<<"Student age:"<< headOfList->age << endl;
                cout<<"Student G.P.A.:"<< headOfList->gpa << endl;
                cout<<"Student sex:"<< headOfList->sex << endl;
                cout<<"-------------------------------"<< endl;



            }

            else
            {


                currentPtr = (headOfList)->nextPtr;




                while (currentPtr->nextPtr != NULL && currentPtr->ID != tempid[j] )
                {


                    currentPtr = currentPtr->nextPtr;
                }


                if(currentPtr->ID == tempid[j])
                {
                    cout<<"-------------------------------"<< endl;
                    cout<<"Student ID:"<< currentPtr->ID << endl;
                    cout<<"Student name:"<< currentPtr->name << endl;
                    cout<<"Student age:"<< currentPtr->age << endl;
                    cout<<"Student G.P.A.:"<< currentPtr->gpa << endl;
                    cout<<"Student sex:"<< currentPtr->sex << endl;
                    cout<<"-------------------------------"<< endl;



                }

            }

            j++;
        }
    }

}

void allFemale(Student *headOfList)
{
    StudentPtr startptr = NULL;
    StudentPtr previousPtr;
    StudentPtr currentPtr;


    int tempid[100];
    currentPtr = headOfList;
    int i = 0;



    while (currentPtr != NULL)
    {

        if (currentPtr->sex[0] == 'f')
        {

            tempid[i] = currentPtr->ID;
            i++;



        }

        currentPtr = currentPtr->nextPtr;
    }



    if (i==0)
    {

        cout<< "No female students" << endl;



    }

    else
    {


        int j = 0;

        while (j <= i)
        {

            if ( tempid[j] == (headOfList)->ID )
            {

                cout<<"-------------------------------"<< endl;
                cout<<"Student ID:"<< headOfList->ID << endl;
                cout<<"Student name:"<< headOfList->name << endl;
                cout<<"Student age:"<< headOfList->age << endl;
                cout<<"Student G.P.A.:"<< headOfList->gpa << endl;
                cout<<"Student sex:"<< headOfList->sex << endl;
                cout<<"-------------------------------"<< endl;



            }

            else
            {


                currentPtr = (headOfList)->nextPtr;




                while (currentPtr->nextPtr != NULL && currentPtr->ID != tempid[j] )
                {


                    currentPtr = currentPtr->nextPtr;
                }


                if(currentPtr->ID == tempid[j])
                {
                    cout<<"-------------------------------"<< endl;
                    cout<<"Student ID:"<< currentPtr->ID << endl;
                    cout<<"Student name:"<< currentPtr->name << endl;
                    cout<<"Student age:"<< currentPtr->age << endl;
                    cout<<"Student G.P.A.:"<< currentPtr->gpa << endl;
                    cout<<"Student sex:"<< currentPtr->sex << endl;
                    cout<<"-------------------------------"<< endl;



                }

            }

            j++;
        }
    }

}

void gpaDesending (Student *headOfList)
{
    StudentPtr startptr = NULL;
    StudentPtr previousPtr;
    StudentPtr currentPtr;

    currentPtr = headOfList;
    int temparrayone[100];

    int temparraytwo[100];
    int i = 0;
    int j = 0;
    int k = 0;
    int tempone = 0;
    int temptwo = 0;

    while (currentPtr != NULL)
    {

        temparrayone[i] = currentPtr->gpa;
        temparraytwo[i] = currentPtr->ID;
        currentPtr = currentPtr->nextPtr;
        i++;

    }




    for (j=0; j < i-1; j++)
    {
        for (k=j; k < i; k++)
        {

            if (temparrayone[k+1]>temparrayone[k])
            {

                temparrayone[k] = tempone;
                temparraytwo[k] = temptwo;
                temparrayone[k] = temparrayone[k+1];
                temparraytwo[k] = temparraytwo[k+1];
                temparrayone[k+1] = tempone;
                temparraytwo[k+1] = temptwo;



            }



        }
    }
    /*
        k=0;
        while (k<i){
        cout<<temparrayone[k];

        k++;

        }
        cout<<endl;
        k=0;
        while (k<i){

        cout<<temparraytwo[k];
        k++;

        }
        cout<<endl; */
    int tempid =0;
    j = 0;
    while (j <= i)
    {

        tempid = temparraytwo[j];

        if ( tempid == (headOfList)->ID )
        {

            cout<<"-------------------------------"<< endl;
            cout<<"Student ID:"<< headOfList->ID << endl;
            cout<<"Student name:"<< headOfList->name << endl;
            cout<<"Student age:"<< headOfList->age << endl;
            cout<<"Student G.P.A.:"<< headOfList->gpa << endl;
            cout<<"Student sex:"<< headOfList->sex << endl;
            cout<<"-------------------------------"<< endl;



        }

        else
        {


            currentPtr = (headOfList)->nextPtr;




            /*while (currentPtr->nextPtr != NULL && currentPtr->ID != tempid )
            {


                currentPtr = currentPtr->nextPtr;
            }
            */


            if(currentPtr->ID == tempid)
            {
                cout<<"-------------------------------"<< endl;
                cout<<"Student ID:"<< currentPtr->ID << endl;
                cout<<"Student name:"<< currentPtr->name << endl;
                cout<<"Student age:"<< currentPtr->age << endl;
                cout<<"Student G.P.A.:"<< currentPtr->gpa << endl;
                cout<<"Student sex:"<< currentPtr->sex << endl;
                cout<<"-------------------------------"<< endl;



            }

        }

        j++;
    }

}


void printStud( Student *headOfList, int studID)//print particular student
{

    StudentPtr previousPtr;
    StudentPtr currentPtr;



    if ( studID == (headOfList)->ID )
    {

        cout<<"-------------------------------"<< endl;
        cout<<"Student ID:"<< headOfList->ID << endl;
        cout<<"Student name:"<< headOfList->name << endl;
        cout<<"Student age:"<< headOfList->age << endl;
        cout<<"Student G.P.A.:"<< headOfList->gpa << endl;
        cout<<"Student sex:"<< headOfList->sex << endl;
        cout<<"-------------------------------"<< endl;

    }
    else
    {


        currentPtr = (headOfList)->nextPtr;


        if(currentPtr == NULL)
        {
            cout << "student not found" << endl;


        }


        else
        {

            while (currentPtr->nextPtr != NULL && currentPtr->ID != studID )
            {


                currentPtr = currentPtr->nextPtr;
            }


            if(currentPtr->ID == studID)
            {
                cout<<"-------------------------------"<< endl;
                cout<<"Student ID:"<< currentPtr->ID << endl;
                cout<<"Student name:"<< currentPtr->name << endl;
                cout<<"Student age:"<< currentPtr->age << endl;
                cout<<"Student G.P.A.:"<< currentPtr->gpa << endl;
                cout<<"Student sex:"<< currentPtr->sex << endl;
                cout<<"-------------------------------"<< endl;

            }

            else
            {

                cout << "student not found" << endl;

            }

        }




    }

}



int main(void)//main
{




    int tempID;
    int studID;
    StudentPtr startptr = NULL;
    char choice;
    static int i;

    char name[50];
    int age;
    double gpa;
    char sex[1];
    i = 0;
    int yo=1;
    while(yo != 0)
    {



        fflush(stdin);
        system("cls");
        printf("\n       This is a program that will manipulate student data\n");    //main menue
        printf("         in a database feel program.\n");
        printf("\n         **********      PROGRAM MENU      **********      \n");
        printf("\n     A. New student.");
        printf("\n     B. Verify student.");
        printf("\n     C. Edit student.");
        printf("\n     D. Remove student.");
        printf("\n     E. Student with the highest GPA.");
        printf("\n     F. Student with the lowest GPA.");
        printf("\n     G. Male students.");
        printf("\n     H. Female students.");
        printf("\n     I. Show all students.");
        printf("\n     J. GPA descending.");
        printf("\n     K. Age descending.");
        printf("\n\n     Q. Quit.");
        printf("\n\n                      Please make a choice: ");
        scanf("%c", &choice);


        switch (choice)
        {
        case 'a':
        case 'A':



            cout << "insert student last name" << endl;
            cin >> name;
            cout << "insert student age" << endl;
            cin >> age;
            cout << "insert student gpa" << endl;
            cin >> gpa;
            cout << "insert student sex" << endl;
            cin >> sex;

            insert( &startptr, name, age, gpa, sex);

            printf("Press any key to return back to the menu.");
            getch();
            break;

        case 'b':
        case 'B':

            cout << "Please enter the id of the student you want to print:" << endl;
            cin >> studID;
            printStud( startptr, studID);




            printf("Press any key to return back to the menu.");
            getch();
            break;

        case 'c':
        case 'C':

            studID = 0;
            cout << "Please enter the id of the student you want to edit:" << endl;
            cin >> studID;

            editStud( &startptr, studID);

            printf("Press any key to return back to the menu.");
            getch();
            break;

        case 'd':
        case 'D':



            cout << "Please enter the id of the student you want to delete:" << endl;
            cin >> tempID;

            deleteNode( &startptr, tempID);




        case 'e':
        case 'E':



            maxGpa(startptr);
            cout<<"Student with max G.P.A. is:"<< endl;
            printStud(startptr, maxGpa(startptr));
            printf("Press any key to return back to the menu.");
            getch();
            break;


        case 'f':
        case 'F':


            minGpa(startptr);
            printStud(startptr, minGpa(startptr));
            printf("Press any key to return back to the menu.");
            getch();
            break;

        case 'g':
        case 'G':




            allMale(startptr);
            printf("Press any key to return back to the menu.");
            getch();
            break;



        case 'h':
        case 'H':

            allFemale(startptr);
            printf("Press any key to return back to the menu.");
            getch();
            break;




        case 'i':
        case 'I':


            printList( startptr );
            printf("Press any key to return back to the menu.");
            getch();
            break;




        case 'j':
        case 'J':


            gpaDesending(startptr);
            printf("Press any key to return back to the menu.");
            getch();
            break;







        }
    }
}

using namespace std;

this is what i have done so far. i know my implementations of my functions are not really good(but they work for me for the moment :P) i want to stress out the
void gpaDesending (Student *headOfList) function. it really doesn't work for me although i thought that in theory it would have done the job.

My question is:

how will i use this sorting algorithm in my code????

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.