(Make the changes in this program so that arrays are transferred to the function
ID: 3704535 • Letter: #
Question
(Make the changes in this program so that arrays are transferred to the functions as pointer variable parameters and the items of the arrays are accessed using pointer variables inside the functions.)
(See the sample program - Pr7-12 Modified with Functions using Pointers)
Write a program in C++ that will compute each student’s test score, class average and Student IDs those who score over 80.
The total number of questions (NUM_Q) in the test is 50 that is defined as a constant. There are 4 students in the class and (NUM_STUDENTS) is defined as a constant.
Use the following arrays:
id- correct - incorrect - score -
stores student id
stores number of correct answers stores number of incorrect answers stores test score
- type string - type int
- type int
- type int
Use the following functions:
1. Function getData will get data - id and correct for each student and stored in two arrays – id and correct.
getData (id, correct, NUM_STUSENTS); Sample Input screen
Student’s ID : Number of correct answers
:
Function calculate will find out the number of incorrect answers (NUM_Q – correct answers) and score (2 * number of correct answers) and those data will be stored in incorrect and score array.
calculate (correct, incorrect, score, NUM_STUDENTS);
Function findAverage function will calculate the test average of all students. The variable average is defined as type double.
average = findAverage (score, NUM_STUDENTS);
Function findHigh will find out the highest test score and return the value to the calling program.
high = findHigh (id, score, NUM_STUDENTS);
Function display will display the table with id, number of correct answers, number of incorrect answers, and score. This function will also print the average test score, highest test score and a table with id and scores those are above 80. Test score average will be displayed with 2 digits after decimal point.
display (id, correct, incorrect, score, average, high, NUM_STUDENTS); Sample data:
ID
ID Correct Answers
Sample Output:
Correct Answers
40 47 35 41
Incorrect Answers Score
40
47 3 94 35 15 70 41 9 82
____ ____
Scores above 80
ID Score
3427 94
3215 82
Test Score Average: Highest Test Score:
10 80
Explanation / Answer
#include <iostream>
using namespace std;
#define NUM_STUDENTS 4
#define NUM_Q 50
void getData (int *id, int *correct)
{
for(int i=0;i<NUM_STUDENTS;i++)
{
cout<<" Enter Student’s ID : ";
cin>>*(id+i);
cout<<" Enter the number of correct answers : ";
cin>>*(correct+i);
}
}
void calculate (int *correct, int *incorrect, int *score)
{
for(int i=0;i<NUM_STUDENTS;i++)
{
*(incorrect+i) = NUM_Q - *(correct+i);
*(score+i) = 2* *(correct+i);
}
}
double findAverage (int *score)
{
double avg = 0;
for(int i = 0;i<NUM_STUDENTS;i++)
{
avg = avg+ *(score+i);
}
return avg/NUM_STUDENTS;
}
double findHigh (int id[NUM_STUDENTS], int score[NUM_STUDENTS])
{
double high = 0;
for(int i = 0;i<NUM_STUDENTS;i++)
{
if(high < *(score+i))
high = *(score+i);
}
return high;
}
void display (int id[NUM_STUDENTS], int correct[NUM_STUDENTS], int incorrect[NUM_STUDENTS], int score[NUM_STUDENTS], double average,double high)
{
cout<<" Student id Correct Answer Incorrect Answer Score";
for(int i = 0;i<NUM_STUDENTS;i++)
{
cout<<" "<<*(id+i)<<" "<< *(correct+i)<<" "<<*(incorrect+i)<<" "<<*(score+i);
}
cout<<" Average Score : "<<average;
cout<<" HighestScore : "<<high;
}
int main() {
int id[4],correct[50],incorrect[50],score[50];
getData (id, correct);
calculate (correct, incorrect, score);
double average = findAverage (score);
double high = findHigh (id, score);
display (id, correct, incorrect, score, average, high);
return 0;
}
Output:
Enter Student’s ID :1234
Enter the number of correct answers :40
Enter Student’s ID :3427
Enter the number of correct answers :
Enter Student’s ID :47
Enter the number of correct answers :2020
Enter Student’s ID :3215
Enter the number of correct answers :35
Student id Correct Answer Incorrect Answer Score
1234 40 10 80
3427 47 3 94
2020 35 15 70
3215 41 9 82
Average Score : 81.5
HighestScore : 94
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.