Compile Error Array/Void function

So I'm creating a program with an array that reads grades and averages them and displays them. I am having a compilation error "/buildSources/program_4.cpp:29: undefined reference to `Average(int*, char*, double*)'
/buildSources/program_4.cpp:30: undefined reference to `PrintResults(double*, int*, char*, int, int)'
collect2: error: ld returned 1 exit status
Return code is not 0"

I don't really know how to fix this.
Here is my code, any input would be awesome.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

// constant for how many grades are to be entered
const int ARRAY_SIZE = 50;
// a function called GetData to read and store data into two arrays,
void GetData(int scores[], int i);
// character value for the grade
char alphaGrade(double avg);
// a function called Average that is used to calculate the average test score and grade,
void Average(int scores[], char grade[],double avg[]);
// function called PrintResults to output the results.
void PrintResults(double avg[],int scores[],char grade[],int i, int j);

int static i;
int j;

int main()
{
 int scores[ARRAY_SIZE];
 char grade[ARRAY_SIZE];
 double avg[ARRAY_SIZE];
 do { 
        
 GetData(scores, i);
 Average(scores, grade, avg);
 PrintResults(avg, scores, grade, i, j);
        
}
 while (scores >= 0);   
  
}

void GetData(int scores[], int i){

   for (int i = 0 ; i < 50 ; i ++){
        cout << "Enter 50 grades: " << endl;
        cin >> scores[i]; 
    }    
}

char alphaGrade(double avg) 
{ 
if(avg>=90 && avg<=100) 
   return 'A'; 
else if(avg>=80 && avg<=89) 
   return 'B'; 
if(avg>=70 && avg<=79) 
   return 'C'; 
if(avg>=60 && avg<=69) 
   return 'D'; 
if(avg>=50 && avg<=59) 
   return 'F'; 
} 

void Average(int scores[], char grade[], double avg[], int i, int j)
{
    double sum =0;
    
    for(int j=0; j<5; j++){
    sum+= scores[i];
    avg[i] = sum/static_cast<double> (5);
    grade[i] = alphaGrade(avg[i]);
    }
}

void PrintResults(double avg[],int scores[],char grade[], int i)
{  
    for (i = 0; i < ARRAY_SIZE; i++){
    cout << setw(8) << "Average" << avg << endl;
    cout << setw(4) << "Grade" << grade << endl;
 }
}
 
Line 14(3 parameters) does not match with line 59(5 parameters) due to the number of parameters. Line 16 does not match with line 70 due to the same reason. The number of parameters are different.

in your for loop, you use the variable j as the counter to iterate thru your array. However, you use the variable i, which is undefined, to perform the calculations.
1
2
3
4
5
6
7
8
9
10
void Average(int scores[], char grade[], double avg[], int i, int j)
{
    double sum =0;
    
    for(int j=0; j<5; j++){
    sum+= scores[i]; // i is undefined . Did you mean j?
    avg[i] = sum/static_cast<double> (5); // i is undefined . Did you mean j?
    grade[i] = alphaGrade(avg[i]); // i is undefined . Did you mean j?
    }
}


1
2
3
4
5
6
7
8
// If the intention of your variable i is for the for loop, then I would do something like this...
void PrintResults(double avg[],int scores[],char grade[])
{  
    for (int i = 0; i < ARRAY_SIZE; i++){
    cout << setw(8) << "Average" << avg << endl;
    cout << setw(4) << "Grade" << grade << endl;
 }
}
Last edited on
That totally helped. It compiles now, but when I run it after typing in 50 numbers, the output is broken. Something is breaking it.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

// constant for how many grades are to be entered
const int ARRAY_SIZE = 50;
// a function called GetData to read and store data into two arrays,
void GetData(int scores[], int i);
// character value for the grade
char alphaGrade(int avg);
// a function called Average that is used to calculate the average test score and grade,
void Average(int scores[], char grade[],int avg[]);
// function called PrintResults to output the results.
void PrintResults(int avg[],int scores[],char grade[]);

int i;

int main()
{
 int scores[ARRAY_SIZE];
 char grade[ARRAY_SIZE];
 int avg[ARRAY_SIZE];
 do { 
        
 GetData(scores, i);
 Average(scores, grade, avg);
 PrintResults(avg, scores, grade);
        
}
 while (scores >= 0);   
 
    return 0; 
}

void GetData(int scores[], int i){

        cout << "Enter 50 grades: " << endl;
        cin >> scores[i];    
}

char alphaGrade(int avg) 
{ 
if(avg>=90 && avg<=100) 
   return 'A'; 
else if(avg>=80 && avg<=89) 
   return 'B'; 
else if(avg>=70 && avg<=79) 
   return 'C'; 
else if(avg>=60 && avg<=69) 
   return 'D'; 
else if(avg>=50 && avg<=59) 
   return 'F'; 
} 

void Average(int scores[], char grade[], int avg[])
{
    int sum = 0;   
    for(int i=0; i<5; i++){
    sum = sum+scores[i];
    avg[i] = sum/50;
    grade[i] = alphaGrade(avg[i]);
    }
}

void PrintResults(int avg[],int scores[],char grade[])
{  
    for (i = 0; i < ARRAY_SIZE; i++){
    cout << setw(8) << "Average" << avg[i] << endl;
    cout << setw(4) << "Grade" << grade[i] << endl;
 }
}
 
Since you have the global variable i undefined on line 18, I would recommend you to change line 27 from this: GetData(scores, i); to this: GetData(scores, ARRAY_SIZE);

Again, since you are using the variable i as a Global on line 18, I recommend you to change the second parameter and add a for loop to input 50 values. From this:

1
2
3
4
5
void GetData(int scores[], int i){

        cout << "Enter 50 grades: " << endl;
        cin >> scores[i]; You are trying to input to scores[50] only. Thus, it will be outside of the array (Element 51).   
}


To something like this:
1
2
3
4
5
6
7
8
9
void GetData(int scores[], int sz){

    for (int count = 0; count < sz; count++)
    {
	   cout << "Enter 50 grades: " << endl;
	   cin >> scores[count];
    }

}

I keep getting 50+ lines of "Please enter 50 grades:" when I run the program. I can't figure out what I'm doing wrong. And I really appreciate the help.
Does your output supposed to look like this?
I cut it down from 50 to 5 to simplicity.
Enter 50 grades: 
88
Enter 50 grades: 
96
Enter 50 grades: 
75
Enter 50 grades: 
86
Enter 50 grades: 
92
Average: 87
Grade: B
Enter 50 grades: 
 


Depending on what you answer, I may be able to help you.
it was just

Please enter 50 grades:
Please enter 50 grades:
Please enter 50 grades:
Please enter 50 grades:
Please enter 50 grades:
Please enter 50 grades:
Please enter 50 grades:
etc.

I placed the counter after the cout and before the cin and that problem went away. That helped, but my averages are coming out with crazy numbers.
Try this code... It is set up for 5 grades, but you can change it to whatever number of grades you one by changing the value on line 8 const int ARRAY_SIZE = 5;
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

// constant for how many grades are to be entered
const int ARRAY_SIZE = 5;
// a function called GetData to read and store data into two arrays,
void GetData(int scores[], int i);
// character value for the grade
char alphaGrade(int avg);
// a function called Average that is used to calculate the average test score and grade,
void Average(int scores[], char& grade, int& avg);
// function called PrintResults to output the results.
void PrintResults(int avg, char grade);


int main()
{
    int scores[ARRAY_SIZE];
    char grade;
    int avg;
    do {

	   GetData(scores, ARRAY_SIZE);
	   Average(scores, grade, avg);
	   PrintResults(avg, grade);

    } while (scores >= 0);

    return 0;
}

void GetData(int scores[], int sz){

    for (int count = 0; count < sz; count++)
    {
	   cout << "Enter 50 grades: " << endl;
	   cin >> scores[count];
    }

}

char alphaGrade(int avg)
{
    if (avg >= 90 && avg <= 100)
	   return 'A';
    else if (avg >= 80 && avg <= 89)
	   return 'B';
    else if (avg >= 70 && avg <= 79)
	   return 'C';
    else if (avg >= 60 && avg <= 69)
	   return 'D';
    else if (avg >= 50 && avg <= 59)
	   return 'F';
}

void Average(int scores[], char& grade, int& avg)
{
    int sum = 0;
    for (int i = 0; i < ARRAY_SIZE; i++){
	   sum = sum + scores[i];
    }
	   avg = sum / ARRAY_SIZE;
	   grade = alphaGrade(avg);
}

void PrintResults(int avg, char grade)
{
	   cout << setw(8) << "Average: " << avg << endl;
	   cout << setw(4) << "Grade: " << grade << endl;
}

Beautiful! What if I need to display all the grades?
avg = sum / ARRAY_SIZE;

that's integer division :+(

The OP had it correct :+)
Last edited on
Something like this?
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

// constant for how many grades are to be entered
const int ARRAY_SIZE = 5;
// a function called GetData to read and store data into two arrays,
void GetData(int scores[], int i);
// character value for the grade
char alphaGrade(int avg);
// a function called Average that is used to calculate the average test score and grade,
void Average(int scores[], char& grade, int& avg);
// function called PrintResults to output the results.
void PrintResults(int avg, char grade);


int main()
{
    int scores[ARRAY_SIZE];
    char grade;
    int avg;
    do {

	   GetData(scores, ARRAY_SIZE);
	   Average(scores, grade, avg);
	   PrintResults(avg, grade);

    } while (scores >= 0);

    return 0;
}

void GetData(int scores[], int sz){

    for (int count = 0; count < sz; count++)
    {
	   cout << "Enter 50 grades: " << endl;
	   cin >> scores[count];
	   int numOfScores = scores[count];
	   cout << alphaGrade(numOfScores) << endl;
    }

}

char alphaGrade(int avg)
{
    if (avg >= 90 && avg <= 100)
	   return 'A';
    else if (avg >= 80 && avg <= 89)
	   return 'B';
    else if (avg >= 70 && avg <= 79)
	   return 'C';
    else if (avg >= 60 && avg <= 69)
	   return 'D';
    else if (avg >= 50 && avg <= 59)
	   return 'F';
}

void Average(int scores[], char& grade, int& avg)
{
    int sum = 0;
    for (int i = 0; i < ARRAY_SIZE; i++){
	   sum = sum + scores[i];
    }
	   avg = sum / ARRAY_SIZE;
	   grade = alphaGrade(avg);
}

void PrintResults(int avg, char grade)
{
	   cout << setw(8) << "Average: " << avg << endl;
	   cout << setw(4) << "Grade: " << grade << endl;
}

@ TheIdeasMan, thanks for pointing it out.
@ celticqueenkira, listen to what @TheIdeasMan mentioned and please do not take this as a final code!!!. It needs a lot of work like, what happens if the user input a score of 49? Is it an F grade? your output will be some garbage character. Now, if the value cannot be lower than 50, then you may want to consider to validate the user input.
Okay, I fixed the range for an F. It's now 0 through 59. Still getting a weird ? when it does the average. Otherwise, I'm pretty pleased with it. Minus the fact i need a termination for a negative
This is what I am getting from the code below...
Enter 50 grades: 
89
B
Enter 50 grades: 
78
C
Enter 50 grades: 
65
D
Enter 50 grades: 
25
F
Enter 50 grades: 
99
A
Average: 71.20
Grade: C


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

// constant for how many grades are to be entered
const int ARRAY_SIZE = 5;
// a function called GetData to read and store data into two arrays,
void GetData(int scores[], int sz);
// character value for the grade
char alphaGrade(double avg);
// a function called Average that is used to calculate the average test score and grade,
void Average(int scores[], char& grade, double & avg);
// function called PrintResults to output the results.
void PrintResults(double avg, char grade);


int main()
{
    int scores[ARRAY_SIZE];
    char grade;
    double avg;
    do {

	   GetData(scores, ARRAY_SIZE);
	   Average(scores, grade, avg);
	   PrintResults(avg, grade);

    } while (scores >= 0);

    return 0;
}

void GetData(int scores[], int sz){

    for (int count = 0; count < sz; count++)
    {
	   cout << "Enter 50 grades: " << endl;
	   cin >> scores[count];
	   int numOfScores = scores[count];
	   cout << alphaGrade(numOfScores) << endl;
    }

}

char alphaGrade(double avg)
{
    if (avg >= 90 && avg <= 100)
	   return 'A';
    else if (avg >= 80 && avg <= 89)
	   return 'B';
    else if (avg >= 70 && avg <= 79)
	   return 'C';
    else if (avg >= 60 && avg <= 69)
	   return 'D';
    else if (avg <= 59)
	   return 'F';
}

void Average(int scores[], char& grade, double& avg)
{
    double sum = 0;
    for (int i = 0; i < ARRAY_SIZE; i++){
	   sum = sum + scores[i];
    }
	   avg = sum / ARRAY_SIZE;
	   grade = alphaGrade(avg);
}

void PrintResults(double avg, char grade)
{
    cout << fixed << showpoint << setprecision(2);
	   cout << setw(8) << "Average: " << avg << endl;
	   cout << setw(4) << "Grade: " << grade << endl;
}
I gave it more tweaks this morning. It ran beautifully. I can't express my gratitude enough, especially through a computer, but you have been a lifesaver. Thank you so much for helping me!
Topic archived. No new replies allowed.