Arrays as Function Arguments

I am almost finished with this program! The instructions ask:
Write a program that will input letter grades (A, B, C, D, F). The
grades will be read into an array. A function will be called five times (once
for each letter grade) and will return the total number of grades in that
category. The input to the function will include the array, number of
elements in the array and the letter category (A, B, C, D or F). The program
will print the number of grades that are A, B, etc.

I keep getting errors (line 52) when I try to compare the value of the array to the letter grade. ex: undefined reference to `GradeCount(char*, int, char)'

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
 #include <iostream>
 using namespace std;

 //Function prototype
 int GradeCount(char [], int, char);  

 int main ()
{
	const int SIZE = 50;        //Array size
        int numOfGrades;            //Variable to hold num of grades the user is entering
	char grades[numOfGrades];   //Array to hold grades
	char A,B,C,D,E,F;           //Character for call to function
	char letter;                //Letter
	
	
	cout << "LETTER GRADES\n" <<endl;
	
	//How many grades are you entering??
	cout << "How many grades do you want to enter: ";
	cin  >> numOfGrades;
	cout << "\nPlease enter grade score in uppercase letters only!\n" <<endl;
	
	//Loop to populate the grades array!
	for(int index = 0; index < numOfGrades; index++)
	{
	  cout << "Grade #" << ( index+1) << ": ";
	  cin  >> grades[index];
	}
		
        GradeCount(grades, SIZE, A);
        GradeCount(grades, SIZE, B);
        GradeCount(grades, SIZE, C);
        GradeCount(grades, SIZE, D);
        GradeCount(grades, SIZE, E);
        GradeCount(grades, SIZE, F);
			
}

//********************************************
//Definition of function gradeCount.         *
//This function accepts an arry of chars the *
//array size and a char as it's arguments.   *
//the contents of the array ar displayed!    *
//********************************************

 int gradeCount (char grades[], int numOfGrades, char letter)
{
	for(int index = 0; index < numOfGrades; index++)
	{ 
	  int amtOfValue = 0; //Tally number of each grade value
	  
	  if (grades[index] == letter)
          amtOfValue = amtOfValue + 1;
          return amtOfValue;
	}
}
Last edited on
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>
// using namespace std; // **** avoid. just write std::cout etc.

//Function prototype:
int GradeCount( const char[], int, char ); // **** make it const-correct ****

int main ()
{
    const int SIZE = 50;        //Array size
    int numOfGrades = 0 ; // **** initialise ****
    char grades[SIZE];  // **** size of an array must be an integral constant ****
    const char letter_grades[] = { 'A', 'B', 'C', 'D', 'E', 'F' } ; // *** characters for letter grades; const

    std::cout << "LETTER GRADES\n\n"  ;

    //How many grades are you entering??
    std::cout << "How many grades do you want to enter: ";
    std::cin  >> numOfGrades;
    if( numOfGrades > SIZE ) numOfGrades = SIZE ; // **** make sure that we do not exceed the range

    std::cout << "\nPlease enter grade score in uppercase letters only!\n\n" ; // <<endl;

    //Loop to populate the grades array!
    for( int index = 0; index < numOfGrades; ++index ) // use prefix increment/decrement as the default
                                                       // it does not matter here; but let us form good habits
    {
        std::cout << "Grade #" << (index+1) << ": ";
        std::cin >> grades[index];
    }

    // because we now have an array of letter grades, we can write a loop
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char letter : letter_grades ) // for each letter grade
    {
        // print out the count
        std::cout << letter << ": " << GradeCount( grades, SIZE, letter ) << '\n' ;
    }
}

//********************************************
//Definition of function gradeCount.         *
//This function accepts an arry of chars the *
//array size and a char as it's arguments.   *
//the contents of the array ar displayed!    *
//********************************************

// **** note: spelling (case sensitive!) ****
// int gradeCount( const char grades[], int numOfGrades, char letter )
int GradeCount( const char grades[], int numOfGrades, char letter )
{
    int amtOfValue = 0; // **** outside the loop ****

    for( int index = 0; index < numOfGrades; ++index )
    {
        if (grades[index] == letter) ++amtOfValue ;
    }

    return amtOfValue ; // **** outside the loop ****
}
Last edited on
Topic archived. No new replies allowed.