write a c++ program and use visual studio Write a C++ program that will input a
ID: 3717610 • Letter: W
Question
write a c++ program and use visual studio
Write a C++ program that will input a student's name and four test scores. you want to understand what affect there is if you "toss out" the lowest grade. Thank goodness you only have 3 students in your class and only four scores for each student. you do not need to do any error checking for valid input (see below for the exception to this direction). calculate the average with and without the lowest score and display the results in the following format:
1 2 3 4 Low Average (4) Average (3)
Sally 95 75 83 89 75 85.5 89.0
Billy 78 79 88 91 nn xxxx yyyy
Student3 a b c d e xxxx yyyy
You Must use functions to make your code easy to follow. At a minimum, you must have separate function for the following program parts:
1) Find the low number
2) Calculate the average
3) Display the results
You must use arrays as your data structure. You can use a one or two dimensional array. if you want, you can use multiple dimensional arrays.
Sample output:
Note: The italicized data is user inputs.
Input the name of student 1: sally
Input the 4 scores for sally: 95, 75, 83, 89
Input the name of student 2: Billy
Input the 4 scores for Billy: 78, 79, 88, 91
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
string names[3];
int marks[3][4];
int maxNamelength = 0;
for(int i=0; i<3; i++) {
cout<<" Input the name of student "<<i+1<<" : ";
getline(cin, names[i]);
if(names[i].length() > maxNamelength)
maxNamelength = names[i].length();
cout<<" Input the 4 scores for "<<names[i]<<" : ";
for(int j=0; j<4; j++){
cin>>marks[i][j];
}
}
for(int i=0; i<3; i++)
cout<<names[i];
for(int i=0; i<3 ;i++){
cout<<" ";
for(int j=0;j<3; j++){
cout<<marks[i][j]<<" ";
}
}
int result[3][3];
float min;
float avg4 = 0;
float avg3 = 0;
int sum ;
for(int i=0; i<4; i++){
min = marks[i][0]; //taking first mark as min
sum = 0; //initialize sum as 0
for(int j=0; j<4 ; j++){
//calculate low
if(marks[i][j] < min)
min = marks[i][j];
//calculate sum
sum = sum + marks[i][j];
}
//assign result for student
avg4 = (float) sum/4;
avg3 = (float) (sum-min)/3;
result[i][0] = min;
result[i][1] = avg4;
result[i][2] = avg3;
}
//printing the result
cout<<" ";
for(int i = 0; i<= maxNamelength ; i++){
cout<<" ";
}
//print the header line after adequate number of spaces
cout<<" 1 "<<" "<<" 2 "<<" "<<" 3 "<<" "<<" 4 "<<" "<<" Low "<<" "<<" Average(4) "<<" "<<" Average(3) ";
for(int i=0 ; i<3; i++){
cout<<" "<<names[i]<<" ";
int j=0;
while(j < 4){
cout<<" "<<marks[i][j]<<" ";
j++;
}
j = 0;
while(j < 3){
cout<<" "<<result[i][j]<<" ";
j++;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.