Question

# Assignment #4 – Student Ranking : In this assignment you are going to write a program...

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.

1. Use functions to divide your program into manageable pieces. Define three functions: displayList, findAverage, and sort.
1. Write function prototypes at the top and define the functions after the main function.
2. findAverage function:
1. This function will get 4 parameters: array of student names, 2D array of grades from 3 tests of each student, array of averages to fill in, number of students as the arrays size.
2. You need nested for loop to calculate average of 3 test scores for each student and record it to the array of averages. (You may define array of names and 2D array of grades as constants in the parameter list since you don’t need to make any changes.)
3. No return value needed; changes will apply on the original array of averages.
3. displayList function:
1. This function has 3 parameters: array of student names, array average scores, and the size of the arrays.
2. The function doesn’t make any changes on the arrays, so define them as constants in the parameter list.
3. Call the function twice in your program
1. When averages are calculated
2. When averages are sorted
4. sort function:
1. This function gets 3 parameters: array of students, array of averages, and number of students as the size of the arrays.
2. Use selection sort algorithm. Sort both arrays based on the average scores in descending order. (You will apply the sort on the array of averages by comparing the scores, but apply the swapping on both arrays. They are parallel arrays. This means they use the same index numbers.)
5. Define constants as global constants. Do not use any literal in the program, use their identifiers.
6. Write function prototypes.
7. Keep variable definitions local.
8. At the beginning of the program inform user about the program. Display a message stating that they can enter 3 test scores and any number of students limited to 100.
9. Ask for number of the students to user.
10. Array of students, test scores, and averages are parallel arrays. This means they store related data in the same subscript.
11. The array of test scores is a two-dimensional array. First subscript holds number of students and second subscript holds number of test scores.
12. Validate user input with a while or do-while loop. They shouldn’t exceed the maximum number; also, it can’t be less than 1.
13. Ask for the student names and their test scores. Use getline function to get the students’ full names. You must use cin.ignore() before using the getline function since you also use cin object.
14. Call findAverage function.
15. Call displayList function.
16. Call sort function.
17. Call displayList function.
18. Use comment lines to describe block of code statements in the program.

Rubric

• Complete and working program with correct calculations and no errors. (5 points)
• Code readability with indentations, spaces when necessary (4 points)
• Comment lines for description (4 points)
• Local variable definitions in the appropriate function. (5 points)
• Global constants definition and using the named constant in the program (5 points)
• Array definitions (2D and parallel arrays) (10 points)
• Input Validation. (10 points)
• Recording user inputs in arrays in a nested for loop(10 points)
• Working with parallel arrays (4points)
• Sorting function prototype, definition, and call (15 points)

``````#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()
{
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<<"]::";
}
}

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++)
{
}
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;
}
}
}
}
``````

#### Earn Coins

Coins can be redeemed for fabulous gifts.