Help using functions to calculate and print grade

Hello,
I'm trying to do two functions, the first one should take the 3 grades as arguments, calculate the overall grade, and return the grade; the second one sould take in the 3 grades and the overall and print it.

With what I have so far, the first part looks like it's working as it should be, but the second function is what I'm having trouble with. The grade is not showing the right number (i.e. given the grades 100, 55, 85 for homework, midterm, and final respectively, it's giving me 33 instead of 81/82).

is it a problem with the functions, the call functions, or did I just miss something else?

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
#include <stdio.h>

int AllGrades(int homework, int midterm, int final);
int gradeOutput();

int main()
{
	int homework, midterm, final;
    printf("Input the homework, midterm, and final grade:");
	scanf("%d %d %d", &homework, &midterm, &final);
	
	printf("Final exam was %d out of 100 \n", final);
	printf("Midterm exam was %d out of 75 \n", midterm);
	printf("Homework assignments were %d out of 120 \n", homework);
	
	AllGrades(homework, midterm, final);
	
	gradeOutput();
	
	
	return 0;
}

int AllGrades(int homework, int midterm, int final)
{
	int grade;
	
	if (homework <= 120) {
		homework = (homework * 50) / 120;
	}
	else {
		printf("Input error\n");
		return 0;
	}

	if (midterm <= 75) {
		midterm = (midterm * 20) / 75;
	}
	else {
		printf("Input error\n");
		return 0;
	}

	if (final <= 100){
		final = (final * 30) / 100;
	}
	else {
		printf("Input error\n");
		return 0;
	}
	
	
	return 0;
	
}

int gradeOutput()
{
	int homework, midterm, final;
	int grade = homework + midterm + final;
	

	printf("Grade is %d\n", grade);
	
	return 0;
}
Hi,

your gradeOutput() function is faulty, it initializes new variables and without assigning them it adds it,

you have to take arguments like you did with AllGrades()

somewhat like this

1
2
3
4
5
6
7
8
9
10
int gradeOutput(int homework, int midterm, int final)
{
	
	int grade = homework + midterm + final;
	

	printf("Grade is %d\n", grade);
	
	return 0;
}


obviously you also have to change your function call

gradeOutput(homework, midterm, final);


Hope it helps


PS: welcome to cplusplus.com

Last edited on
It helped a little, thanks! It's not recognizing the weighted grades though? (The calculations from AllGrades() It's adding the inputted numbers instead.
change the function argument to call by reference

int AllGrades(int *homework, int *midterm, int *final);

call by value won't help in what you are trying to achieve
I got it now, thank you very much!
welcome :)
Topic archived. No new replies allowed.