Assignment #4 – Student Ranking :
In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking.
Follow the Steps Below
Rubric
#include <iostream>
#include<string>
using namespace std;
#define MAX 100 // or use const int MAX=100;
//const int MAX=100;
#define init 0
#define TS 3
void findAverage(const string [MAX],const float [MAX][TS],float [MAX],int );//prototypes
void displayList(const string [MAX],const float [MAX],int );
void sort(string [MAX],float [MAX],int );
int main()
{
float grades[MAX][TS],avg[MAX];
string name[MAX];
int i,n;
cout<<"User can enter 3 test scores and any number of students limited to 100."<<endl;
while(1) //check validity of input
{
cout<<" Enter number of the students ";
cin>>n;
if(n>=1 && n<=MAX)
break;
else
cout<<"Invalid::valid number of students 1-100"<<endl;
}
for(i=init;i<n;i++) //to enter student details
{
cout<<"enter the name of sudent["<<i+1<<"]::";
cin.ignore();
getline(cin,name[i]);
for(int j=init;j<TS;j++)
{
cout<<"enter score["<<j+1<<"]the name of sudent["<<i+1<<"]::";
cin>>grades[i][j];
}
}
findAverage(name,grades,avg,n); //function calls
cout<<"before sorting"<<endl;
displayList(name,avg,n);
sort(name,avg,n);
cout<<"after sorting"<<endl;
displayList(name,avg,n);
return 0;
}
void findAverage(const string name[MAX],const float grades[MAX][TS],float avg[MAX],int n)
{
for(int i=init;i<n;i++) //find average
{
float sum=init;
for(int j=init;j<TS;j++)
{
sum=sum+grades[i][j];
}
avg[i]=sum/TS;
}
}
void displayList(const string name[MAX],const float avg[MAX],int n)
{
cout<<"Student Details"<<endl;
for(int i=init;i<n;i++)
{
cout<<"student["<<i+1<<"]:: name:"<<name[i]<<"\t"<<"average::"<<avg[i]<<endl;
}
}
void sort(string name[MAX],float avg[MAX],int n)
{
for(int i=init;i<n-1;i++) //selection sort
{
for(int j=i+1;j<n;j++)
{
if(avg[i]<avg[j])
{
float t1=avg[i];
avg[i]=avg[j];
avg[j]=t1;
string t2=name[i];
name[i]=name[j];
name[j]=t2;
}
}
}
}
Get Answers For Free
Most questions answered within 1 hours.