# Thread: help with a simple programe :)

1. ## help with a simple programe :)

hi everybody
here is the question

write a program using functios to find out the average of three marks(m1,m2,m3)of n number of students , the function should read the 3 marks and calculate the average and anther function to take the average and print the grade depending on the following category .
the main call the two functions

60-70 pass
71-80 good
81-90 v.good
91-100 excellent

this is what i've wrote so far
Code:
```#include <stdio.h>
#include <stdlib.h>
double avg (int m1 ,int m2, int m3);
void cate (void);
int main()
{
int m1,m2,m3 ;
printf("Enter the first mark\n");
scanf("%d",&m1);
printf("Enter the second mark\n");
scanf("%d",&m2);
printf("enter the third mark\n");
scanf("%d",&m3);
printf("the average is %f\n",avg(m1,m2,m3));
cate();
return 0;
}
double avg (int m1 ,int m2,int m3)
{
return((m1+m2+m3)/3);
}
void cate (void)
{
double avg ();
if ( avg >=60 && avg <=70){
printf("pass");
}
else if ( avg >=71 &&  avg  <= 80){
printf ("good");
}
else if ( avg  >=81 &&  avg <= 90){
printf("v.good");
}
else
printf("excellent");
}```
the problem is that i keep taking this messege

in function 'cate'
warning: a comparison between a pointer and integer (line 25)
warning: a comparison between a pointer and integer (line 25)
warning: a comparison between a pointer and integer (line 28)
warning: a comparison between a pointer and integer (line 28)
warning: a comparison between a pointer and integer (line 29)

2. Well, the avg declaration in cate() is wrong. Are you trying to call the function avg()? If so that takes three parameters, you defined it that way. Are you trying to have a variable take the return value of that function? I recommend scrapping the avg() function and just passing m1,m2,m3 as parameters to cate(). Then compute the average there in some avg variable and use that.

3. Because you have the following line in your cate function:
Code:
`double avg ();`
Which makes avg effectively a pointer to the function avg above. Try passing the result of avg into cate and using that to produce your output:
Code:
```int main(void)
{
double a;
...
a = avg(m1, m2, m3);
printf("the average is %f\n",a);
cate(a);
return 0;
}```

4. thanks for help
but still there a problem , that
it keeps giving me excellent for every average
is there any error in if statments

btw , this is the new code
tell me if it's right please
Code:
```#include <stdio.h>
#include <stdlib.h>
double avg (int m1 ,int m2, int m3);
void cate (void);
int main()
{
int m1,m2,m3 ;
double a;
printf("Enter the first mark\n");
scanf("%d",&m1);
printf("Enter the second mark\n");
scanf("%d",&m2);
printf("enter the third mark\n");
scanf("%d",&m3);
a = avg(m1,m2,m3);
printf("the average is %f\n",a);
cate();
return 0;
}
double avg (int m1 ,int m2,int m3)
{
return((m1+m2+m3)/3);
}
void cate (void)
{
double avg;

if ( avg >=60 && avg <=70){
printf("pass");
}
else if ( avg >=71 &&  avg  <= 80){
printf ("good");
}
else if ( avg >=81 &&  avg <= 90){
printf("v.good");
}
else
printf("excellent");
}```

5. You don't pass a into cate like I suggested, so the avg variable in there is uninitialized and has who knows what value in it.

6. when i pass (a)into cate functions ; many errors ocurrs :S

7. As opposed to the many errors you have now.

You need to pass the average number into the cate function; that's not an option. You will need to fix whatever errors you have when you do that (i.e., make the function prototype/declaration correct).

8. Code:
```void cate ( double );
...
cate ( a );
...
void cate ( double avg)
{
if ( avg >=60 && avg <=70){
...```

9. ## The whole program should be able to be read aloud, just like a smoothly flowing story

When I read the functions in the example program, I'm not seeing that they are reading right. Functions are just like a formula for an algebra problem when you're working word problems.

I suggest reading functions and writing functions in a pattern similar to this:

When declaring that a function exists, read as:
Code:
`" . . . answer type formula name (answer type variable 1, answer type variable 2)`
When getting prepared to call a function, read the need for that function as:
• formula to be used
• value to go in to the formula
• value to come out of the formula
• e.g. formula-name (x,y)

When calling a function, read the code as:
Code:
`" . . . formula-name (apply to variable 1, apply to variable 2) . . . "`
When defining or describing a function, read the code as:
Code:
```" . . . answer type formula-name (variable 1, variable 2){
declare
work variable 1 against some math
return an answer to fit variable 2
}
. . ."```
Notice that in and around the function, the names would be so that "apply to variable 1" and "variable 1" would be similar, but not the same; this would help keep the reading of the program smoother.

In the example program, I see that the functions are there, but I don't read the code as though they are a part of the program. The whole program should be able to be read aloud as a set of instructions.

If you cannot read the program aloud so that it makes sense, and it's breaking, then that spot where things stop flowing: that's the kink in the logic.

I apologize if that was confusing, but that is how I think about it.

10. Code:
`return((m1+m2+m3)/3);`
... this won't return the correct result since all your variables are ints. Use ((m1+m2+m3)/3.0.