New to FUNCTIONS

Pages: 12
Im trying to write and use a function to calculate an average grade with the user inputting the number of grades and the the numeric grade for each one and i want to write a program that calculates the average and outputs the grade as a letter i.e. A, B, C, etc so far all i can come up with is
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;

void findAverage(int &, int &, int);
void findAverage(float totalSum, int numOfGrades, float& avgGrade)
{
	avgGrade = totalSum / numOfGrades;
}
Well, I would say that your first step would be to get input from the user on what grades the student has. Make sure that that input is able to take a partial grade, i.e. 97.2%. I suggest a type of double. After that, I would suggest adding those grades together, and then dividing them by the number of grades added. My preferred method would be to make a do-while with a sentinel, and increment a counter in each iteration of the loop, and then use the counter as the number of grades that were entered.

After that, I would send the average to a series of if statements that hold the range of grade averages that correspond to each letter grade, and test the result of the average, then output to the user the appropriate one.
that sounds like what i want to do.. i was thinking of using a for loop though? would a do-while be better? also does the function im using look ok?
I know that sounds like what you want to do.

What I want you to do is go make an attempt at it, and come back here to report any difficulties you're having (ideally with code samples).

For loops are okay, but it's hard to use a sentinel with them; they also tend to be more difficult to correctly iterate permanently, which is the purpose of a do-while loop (in this case).
Example:
1
2
3
do{
    // Stuff
}while(input != -1);


No, the function is broken. The reason it's broken is because it doesn't match it's prototype.
I wouldn't use that function in any case. I would use:
1
2
3
double findAverage(double totalSum, int numOfGrades){
    return totalSum / numOfGrades;
}

Assuming that I used a function, which I wouldn't, since it's five lines of code to do one operation.
1
2
3
4
5
6
7
8
9
10
11
{
		int numOfGrades, grade;
		cout << "Enter the number of grades" <<endl;
		cin >> numOfGrades;
		do
		{
            cout << "Enter a numeric grade between 0-100" <<endl;
			cin >> grade;
			numOfGrades++;
		}
		while(
I want to write a while where it comes out of the loop after its output "enter a numeric grade..." the number of times entered at numOfGrades
You have to terminate it, otherwise it will run forever.
1
2
3
4
5
6
7
do{
    // Stuff
}while(true); // This loop will never end.

do{
    // Stuff
}while(false); // This loop will only run once. 


I think that what you attempted would be better served if you didn't ask how many grades they want, and just count the number of grades they enter instead:
1
2
3
4
5
6
7
8
9
10
11
12
{
    int numOfGrades = 0; // Keep track of how many grades you entered.
    double grade, gradeTotal = 0; // Doubles so that you can put in decimal grades.
    do{ // Open the do loop.
        // Tell the user to enter grades, and how to end the loop.
        // Get a grade.
        // Add the grade to the total.
        ++numOfGrades; // Increase the number of grades entered.
    }while(grade != -1); // End the loop if '-1' is entered.
    gradeTotal += -1; // Add the -1 back on to the grades for accuracy.
    // Divide the grade total by the number of grades.
    // Put all of your checking for what letter grade they got here. 
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
#include<iostream>
using namespace std;

void findAverage(int &, int &, int);
void findAverage(float totalSum, int numOfGrades, float& avgGrade)
{
	avgGrade = totalSum / numOfGrades;
}

	int main()
	{
		int numOfGrades, grade, i;
		float totalSum, avgGrade;
		cout << "Enter the number of grades" <<endl;
		cin >> numOfGrades;
		for(i=0; i < numOfGrades; i++)
		{

            cout << "Enter a numeric grade between 0-100" <<endl;
			cin >> grade;
		}


		if(grade <= 59) grade = F;
		if(grade <= 69 && grade >=60) avgGrade = D;
		if(grade <=79 && grade >=70) avgGrade = C;
		if(grade <=89 && grade >=80) avgGrade = B;
		if(grade<=100 && grade >=90) avgGrade = A;

		cout << "The grade is " << findAverage(

			system("pause");
			return 0;
	}
Where did i go wrong here?? im so lost..
I explained in detail what you need to do to start fixing the program, but it doesn't appear that you've worked on what I told you about it. I think it's admirable that you have made progress in other areas, but there are still errors in there that were specifically pointed out to you that you haven't addressed yet.
Where did i go wrong here?? im so lost..


Let's first get this thing to compile (and link).
* Delete line 4. It is a forward declaration for a function that you never define (because the signatures differ).
* On lines 24-18, you are assigning letters to numeric types. A char can be wrapped in single quotes, such as 'F'.
* Line 30 is incomplete, consider commenting it out for now.

Now it should compile.
Thank you moorecm, it does compile now. but it is a practice homework problem in which i must use a function... so how can i fix my function?
ok well im not familiar with that.. so if i want to call it in the main how would i go about that??
this is what ive come to.. and basically i dont know how to add all the grades together or how to call the function
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
double findAverage(double totalSum, int numOfGrades){
    return totalSum / numOfGrades;
}


	int main()
	{
		int numOfGrades, grade, i;
		float totalSum, avgGrade;
		cout << "Enter the number of grades" <<endl;
		cin >> numOfGrades;
		for(i=0; i < numOfGrades; i++)
		{

            cout << "Enter a numeric grade between 0-100" <<endl;
			cin >> grade;
		}
		
		totalSum = grade + grade;
		findAverage(totalSum, numOfGrades);

		if(grade <= 59) avgGrade = 'F';
		if(grade <= 69 && grade >=60) avgGrade = 'D';
		if(grade <=79 && grade >=70) avgGrade = 'C';
		if(grade <=89 && grade >=80) avgGrade = 'B';
		if(grade<=100 && grade >=90) avgGrade = 'A';

		cout <<avgGrade;
totalSum should be totalSum += grade; And you need to put it inside the for loop.

You also need to change findAverage(totalSum, numOfGrades); to avgGrade = findAverage(totalSum, numOfGrades);

This will cause problems, though, because avgGrade is a float, not a double. So change float totalSum, avgGrade; to double totalSum = 0, avgGrade;
ok, ive corrected what youve said and i now have
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
double findAverage(double totalSum, int numOfGrades){
    return totalSum / numOfGrades;
}


	int main()
	{
		int numOfGrades, grade, i;
		double totalSum = 0, avgGrade;
		cout << "Enter the number of grades" <<endl;
		cin >> numOfGrades;
		for(i=0; i < numOfGrades; i++)
		{

            cout << "Enter a numeric grade between 0-100" <<endl;
			cin >> grade;
		}
		
		totalSum += grade;
		avgGrade = findAverage(totalSum, numOfGrades);

		if(grade <= 59) avgGrade = 'F';
		if(grade <= 69 && grade >=60) avgGrade = 'D';
		if(grade <=79 && grade >=70) avgGrade = 'C';
		if(grade <=89 && grade >=80) avgGrade = 'B';
		if(grade<=100 && grade >=90) avgGrade = 'A';

		cout <<avgGrade;

and it compiles nicely but it gives me a numeric grade and it is not the correct average... am i making a mathmatical mistake now?
No, you're trying to shove a char type into a double.
should i change the last if to an else avgGrade = 'A';
No...you should make a variable of type char and output that based on avgGrade, not on grade. avgGrade is a double not a char, you can not use it to store letters.

As a matter of fact, you don't even need a variable. You can just put a cout statement in the conditionals.
you should also put line 19 inside your for loop to actually add them all up (as previously mentioned).
so i made finalGrade an integer. changed grade to avgGrade in my if statements and avgGrade to final grade... but something is wrong with my math im not getting the correct averages nor is it outputting the letter grade. i also made my last cout statement finalGrade
Pages: 12