C++ : Please provide a detailed pseudocode as a multi lined comment for the sour
ID: 3742225 • Letter: C
Question
C++ : Please provide a detailed pseudocode as a multi lined comment for the source code below. Also please include any commenting you would include next to the statements.
#include<iostream>
using namespace std;
void GetGrades(double* scores, string* students, int size)
{
int i;
for( i = 0 ; i < size ; i++ )
{
cout<<"Enter name : ";
// read complete line
cin>>*( students + i );
cout<<"Enter score : ";
cin>>*(scores + i);
// loop untill valid data is entered
while( *(scores + i) < 0 )
{
cout<<"Error!Entered data can't be negative ... ";
cout<<"Enter score : ";
cin>>*(scores + i);
}
cout<<endl;
}
}
void DisplayGrades(double* scores, string* students, int size, double avg)
{
cout<<"Student Score ";
int i;
// display the data
for( i = 0 ; i < size ; i++ )
cout<<*(students + i)<<" "<<*(scores + i)<<endl;
cout<<"Class average is: "<<avg<<endl;
}
void Sort(double* scores, string* students, int size)
{
int i, j;
for (i = 0; i < size - 1; i++)
{
// the previous i elements are placed
for (j = 0; j < size - i - 1; j++)
{
// swap the two elements
if (*(scores + j) > *(scores + j + 1))
{
double temp = *(scores + j);
*(scores + j) = *(scores + j + 1);
*(scores + j + 1) = temp;
string temp1 = *(students + j);
*(students + j) = *(students + j + 1);
*(students + j + 1) = temp1;
}
}
}
}
double Average(double* scores, int size)
{
double sum = 0;
int i;
// get the sum oof all elements of scores
for( i = 0 ; i < size ; i++ )
sum += *(scores + i);
// calculate average and return it
return sum / (double)size;
}
int main()
{
int size;
cout<<"Enter number of students : ";
cin>>size;
while( size < 0 )
{
cout<<"Error!Entered data can't be negative ... ";
cout<<"Enter score : ";
cin>>size;
}
cout<<endl;
double *scores = new double[size];
string *students = new string[size];
GetGrades( scores , students , size );
cout<<endl;
// sort the data
Sort( scores , students , size );
// calculate average
double avg = Average( scores , size );
DisplayGrades( scores, students, size, avg);
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
//this function is used to take input from user using pointer techneques, you can think this pointer access as array
// suppose you want to access array location A[5](array notation), in pointer you will access as *(A+5), so don't get confuse
//this function takes input (Student name and score), here number of Students is is stored in variable "size"
void GetGrades(double* scores, string* students, int size)
{
//for loop control
int i;
//for each student, take input
for( i = 0 ; i < size ; i++ )
{
cout<<"Enter name : ";
// read complete line
//take students name input
cin>>*( students + i );
cout<<"Enter score : ";
//take student's score input
cin>>*(scores + i);
// loop untill valid data is entered
while( *(scores + i) < 0 )
{
cout<<"Error!Entered data can't be negative ... ";
cout<<"Enter score : ";
//again take input, because input was wrong priviously
cin>>*(scores + i);
}
//add empty line to console
cout<<endl;
}
}
//Display each student's name along with hw much they have score.
void DisplayGrades(double* scores, string* students, int size, double avg)
{
cout<<"Student Score ";
int i;
// display the data
for( i = 0 ; i < size ; i++ )
cout<<*(students + i)<<" "<<*(scores + i)<<endl;
cout<<"Class average is: "<<avg<<endl;
}
//Sort student in ascending orderaccording to their score
void Sort(double* scores, string* students, int size)
{
//temporary variable
int i, j;
//Here bubble sort if used
for (i = 0; i < size - 1; i++)
{
// the previous i elements are placed
for (j = 0; j < size - i - 1; j++)
{
// swap the two elements
//if student j's score is greater than student j, then swap their name and also score
if (*(scores + j) > *(scores + j + 1))
{
double temp = *(scores + j);
*(scores + j) = *(scores + j + 1);
*(scores + j + 1) = temp;
string temp1 = *(students + j);
*(students + j) = *(students + j + 1);
*(students + j + 1) = temp1;
}
}
}
}
//calculate average of Scores scored bu Students. i.e Class average
double Average(double* scores, int size)
{
//initialize sum with 0
double sum = 0;
int i;
// get the sum oof all elements of scores
for( i = 0 ; i < size ; i++ )
sum += *(scores + i);
// calculate average and return it
return sum / (double)size;
}
int main()
{
//variable to store number of students
int size;
cout<<"Enter number of students : ";
//take input from user the number of students
cin>>size;
//while user not enters correct input continue ask
while( size < 0 )
{
cout<<"Error!Entered data can't be negative ... ";
cout<<"Enter score : ";
//ask again because priviously, it failed
cin>>size;
}
cout<<endl;
//create score array for number of student entered by user, to store scores
double *scores = new double[size];
//create student array for number of student entered by user to store name of students
string *students = new string[size];
//take input from user in console
GetGrades( scores , students , size );
cout<<endl;
// sort the data
Sort( scores , students , size );
// calculate average
double avg = Average( scores , size );
//display each student's name along with their score, and then display Class average
DisplayGrades( scores, students, size, avg);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.