This function will have five parameters passed by value and return the average

Write a function that will compute the average of a specific grade type. This function will have five parameters passed by value and return the average.
● Array of scores
● Array of grade types
● Array of the max points per score
● Size of the arrays
● Grade type

Help please I am not sure how to write this out this is my code




#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include <math.h>
#include <fstream>

using namespace std;

//pass in the parameters


void inputData(double score[], int type[], int max[], int& size) {
//open the input file

ifstream intput("X:\\Downloads\\lab4input.txt");
if (intput)
{
cout << "quizz grades my guy " << endl;

}
//In the while condition, add the instream variable
while (intput >> score[size] >> type[size] >> max[size])
{
size++;
}
//clsoe the file
char letter_grade(double score) {
char limit[][4] = { 90,80,70,60,'A','B','C','D' };

for (int i = 0; i < 4; i++) {
if (score >= limit[0][i])
return limit[1][i];

}

return 'F';
}

intput.close();

}


int main() {

//Array to store the scores
double score[50];
//Array to store the types
int type[50];
//Array to store the max points
int max[50];
//Size
int size = 0;
inputData(score, type, max, size);
for (int i = 0; i < size; i++) {
if (score[i] == 1)
{
score[i];
}



}

for (int i = 0; i < size; i++) {
if (type[i] == 1)
{
type[i];
}
}for (int i = 0; i < size; i++) {
if (max[i] == 1) {

max[i];
}
cout << "type " << type[i] << endl;
cout << "max " << max[i] << endl;
cout << "score " << score[i] << endl;



}



system(" pause");
return 1;
}
Last edited on
Can you post the .txt file?
Scores, I assume, run from 0 – 100 and grades map ranges of scores?
Max points per score – is that the upper score boundary for each grade or max score actually received within each grade?
What does grade type mean? Type of the variable?
These are some of the things not clear from your post
99.0 1 100
87.5 1 100
103.0 2 100
96.0 2 100
48.5 2 100
78.0 2 100
10.0 3 10
98.5 3 100
73.0 3 75
98.5 3 100
97.0 3 100
100.0 3 100
100.0 4 100
96.5 4 100
95.0 4 100
94.0 4 100
80.0 4 100
90.0 4 100
100.0 4 100
10.0 4 10
20.0 4 20
15.0 4 15
9.0 5 10
8.0 5 10
10.0 5 10
7.0 5 10
14.0 5 20
20.0 5 20
17.0 5 20
21.0 5 21
10.0 5 10
5.0 5 5
71.5 6 100
I am sorry this is the txt file
ok, the rows are the students I suppose, and the columns are? also there are some qs in my previous post that you didn't answer
types are either quiz exam or test
what about the columns? if you're serious about getting help read the posts and answers all the qs or else it's just a waste of my time engaging with you
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>

// read data from the input stream into the arrays, up to a maximum of max_size entries
// return the actual number of entries that were read 
// input is assumed to be valid; no error checks are done
// each line of input contains three numbers: score (int or double), grade_type (int), max_score (int)
int read_scores( std::istream& stm, double score[], int type[], int max[], int max_size )
{
    int cnt = 0 ; // count of actual entries read
    while( cnt < max_size && stm >> score[cnt] >> type[cnt] >> max[cnt] ) ++cnt ;
    return cnt ;
}

// normalise the score to a percentage (ie. max score == 100 )
// eg. normalise( 12.3, 20 ) would return 61.5 (12.3/20 == 61.5/100)
// invariant: score is non-negative, max_score is positive
double normalise( double score, int max_score ) { return score * 100.0 / max_score ; }

// return the average percentage score for a particular grade
double average_score_for_grade( const double score[], const int grade_type[], const int max_score[],
                                int size, int grade )
{
    int num_scores = 0.0 ; // number of scores for this grade
    double total_score = 0.0 ; // total scores for this grade (normalised)

    for( int i = 0 ; i < size ; ++i ) // for each entry
    {
        if( grade_type[i] == grade ) // if the grade type is what we are looking for
        {
            ++num_scores ;
            total_score += normalise( score[i], max_score[i] ) ;
        }
    }

    return total_score / num_scores ;
}

int main()
{
    const int MAX_ENTRIES = 50 ;
    double score[MAX_ENTRIES] {} ;
    int grade_type[MAX_ENTRIES] {} ;
    int max_score[MAX_ENTRIES] {} ;

    std::ifstream file( "scores.txt" ) ; // change file name as appropriate
    const int num_scores = read_scores( file, score, grade_type, max_score, MAX_ENTRIES ) ;
    // num_scores scores were read from the file

    if( num_scores > 0 ) // if at least one score was read
    {
        int grade ;
        std::cout << "enter a grade type: " ;
        std::cin >> grade ;

        const double average = average_score_for_grade( score, grade_type, max_score, num_scores, grade ) ;
        std::cout << "the average score for grade type " << grade << " is " << average << "%\n" ;
    }
}
Topic archived. No new replies allowed.