Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) Write a program that prompts the user to enter ten student last names and

ID: 3661847 • Letter: #

Question

(C++) Write a program that prompts the user to enter ten student last names and their GPAs. Store this data in an array (or arrays). Do not allow the user to enter a GPA outside of the range of [0. 4.0]

Once the data has been read into the program. Give the user the choice to see the student data (name and GPA) sorted by either name of GPA. If the user chooses sort by name, on the screen present all ten student names and their corresponding GPA alphabetically. It the user choses sort by GPA, present all ten GPAs and the corresponding name on the screen sorted with the highest GPA first.

Explanation / Answer

we will implement this using an array of structures and then sort the arrays with methos calculate1() and calculate2() depending on the names and gpa.

#include <stdio.h>

    struct student

    {            

        char name[20];

        int gpa

        

    };

  

  

void calculate1 (struct student *);

void calculate2 (struct student *);

int main ()

{

    int i ;

     inct choice;

     struct student s[10];/* creating array of structures for multiples students

here */

    cout<<”enter the student details “;

    for (i=0;i<10;i++)

    { cout<<”enter name”;

gets(s[i].name);

cout<<”enter gpa”;

     cin>>s[i].gpa;

}

cout<<” enter choice 1.sort by name 2.sort by gpa”;

cin>>choice;

if(choice==1)

{calculate1(s);

}

  else

calculate2(s);

  

   

  

  

      

void calculate1 (struct student *s)/* sorting on name alphabetically*/

{

   int i,j ;

   struct student temp;

for(i=1;i<10;i++) {

for(j=1;j<10;j++) {

if(strcmp(s[j-1].name,s[j].name>0) {

strcpy(temp, s[j-1]);

strcpy(s[j-1],s[j]);            

strcpy(s[j], temp);

}

for(i=0;i<10;i++)

{ cout<<s[i].name;

cout<<s[i].gpa;

}

void calculate2 (struct student *s) /* sorting on the gpa in descending */

{

   int i,j ;

   struct student temp;

   

for(i=0;i<10;i++) {

for(j=1;j<9;j++) {

if(s[j-1].gpa < s[j].gpa ) {

temp=s[j-1];

s[j-1] = s[j];

s[j]=temp;

}

    for(i=0;i<10;i++)

{cout<<"the sorted order is;

cout<" descending order ";

cout<<s[i].name;

cout<<s[i].gpa;

}

}

OUTPUT :

enter student details

enter name : amy

enter gpa:2

enter name: susie

enter gpa: 3

enter name: paul

enter gpa:4

enter 1.sort by name 2.sort by gpa

1

the sorted order is

he alphabetical order is:

amy

2

paul

4

susie

3

for choice 2

the sorted order is

descending order

paul

4

susie

3

amy

2