﻿ C++ exercises and solutions: C++ arrays (more)

# C++ exercises and solutions: C++ arrays (more)

## C++ arrays (more)

### 3. Write a C++ program to answer inquiries about student data.

The program will display a menu that enables the users to choose whether they want to view all students ’records or view only the records of a specific student by the student’s id. See sample below.

1. View all students’ records
2. View a student’s records by ID
3. Show the highest and the lowest final scores

|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |   ==================================================
|1232          |  10    | 23       | 45             | 56     |
|2343          |  45      | 43     | 24             |  78    |
|2343          |  34      | 45     | 45             | 45     |
|3423          |  67      | 6       | 65             | 56     |

Note: These records will be stored in a two-dimensional array

Solution:

#include <cstdlib>

#include <iostream>

using namespace std;

int hivalue(int stu[4][5]);

int lovalue(int stu[4][5]);

cout<<"========================================================"<<"\n";

cout<<"========================================================"<<"\n";

cout<<"     1.View all student records"<<"\n";

cout<<"     2.View a student records by ID"<<"\n";

cout<<"     3.Show the highest and the lowest scores"<<"\n";

}

void viewall(int stu[4][5]){

int i,j;

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

for(j=0;j<5;j++) cout<<stu[i][j]<<"\t\t";

cout<<"\n";

}

}

void viewbyid(int stu[4][5]){

int id,i,j;

bool l=false;

cin>>id;

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

if(stu[i][0]==id){

for(j=0;j<5;j++)cout<<stu[i][j]<<"\t\t";}

cout<<"\n";}

}

void showhl(int stu[4][5]){

cout<<"The higest final score is:"<<hivalue(stu);

cout<<"\n";

cout<<"The lowest final score is:"<<lovalue(stu);

cout<<"\n";

}

cout<<"========================================================\n";

cout<<"StudentID      Quiz1          Quiz2          Mid-term         Final\n";

cout<<"========================================================\n";

}

int hivalue(int stu[4][5]){

int *max,i;

max=&stu[0][4];

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

if(*max<stu[i][4])*max=stu[i][4];

return(*max);

}

int lovalue(int stu[4][5]){

int *min,i;

min=&stu[0][4];

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

if(*min>stu[i][4])*min=stu[i][4];

return(*min);

}

int main(int argc, char *argv[])

{

//construct 2d array to store students'records

int stu[4][5]={{1232,32,34,43,43},{2345,34,34,54,35},{3432,45,54,56,34},{3456,56,34,34,56}};

int yourchoice;

char confirm;

do

{

cin>>yourchoice;

switch(yourchoice){

case 1:viewall(stu);break;

case 2:viewbyid(stu);break;

case 3:showhl(stu);break;

default:cout<<"invalid";

}

cout<<"Press y or Y to continue:";

cin>>confirm;

}while(confirm=='y'||confirm=='Y');

system("PAUSE");

return EXIT_SUCCESS;

}