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.

MENU


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

|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;

 

void showheading();

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

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

void displaymenu(){

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

cout<<"                         MENU                                  "<<"\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;

//display heading

showheading();

 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;

     cout<<"Please enter a student's ID:";

     cin>>id;

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

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

                        showheading();l=true;

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

       cout<<"\n";}

      if(l==false) cout<<"Not found!\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";

    

     }

void showheading(){

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}};

 

//show menu

displaymenu();

int yourchoice;

char confirm;

do

{

cout<<"Enter your choice(1-3):";

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;

}


Comments

lol comment

 lol

Write a program that defines an array of 10 integers ranging from 1 to 100. Prompt the user to enter 10 integers and store in an array.


2018-04-05
focuz comment

 focuz

nice one, but somehow cumbersome.


2017-11-29
reet srivastava comment

 reet srivastava

good but should be written in a more convenient way


2017-07-30
mohamed fergani comment

 mohamed fergani

it`s good ,but i want to simple away to
can understande whit easy.....................


2017-04-27
MUHAMMAD NABEEL comment

 MUHAMMAD NABEEL

It's good. But you should write it in a more simple and general way so that every student will be able to understand it


2017-02-05



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.