# Thread: Help with functions usage on project plz

1. ## Help with functions usage on project plz

Hi, I was hoping somebody could help me with this program I wrote the other day, I need to modify it to include functions to find the highest, and lowest values of test scores.

I have begun to insert the functions, but I'm not sure what to do next. My program already calculates the highest grade, but for an extra ten points, I need it to do it with a function.

Because the if then for highest score already exists, I was wondering if I could take it out and put it into the function for highest, for starters. Then write a similar one for lowest?
How do I call the values at the end, what goes inside the parameters?

Code:
```//This programs prompts the user for the grades of ten tests, then prints the av
erage score, the high score, and a list of scores associated with each student a
nd test, including a letter grade.

#include <stdio.h>

float findLowest(float[], int)
{
//  statements
//return Lowest;
}

float findHighest(float[], int)
{
//statements
//return Highest;
}

#define N 10

int main(void)

{

float sum, numraise, studentname, i;
highscore=0.0;
sum=0.0;

for (i=0; i<N; i++)

{
if (highscore < grade[i])    // Move this to function? Modify how?

}

average_score= sum/10;

for (i = 0;i < 10; i++)
{
else

printf("Student %f's grade is %.1lf, this is an %c\n", i+1, grade[i], lettergra
de);
}

printf("The highest score is %.1lf\n", highscore); //Prints high score
printf("the average score is %.2lf\n", average_score); //prints average

printf("The lowest score is: %.2f", lowest());
printf("The highest score is: %.2f", highest());

return 0;
}```

2. Because the for loop for highest score already exists, I was wondering if I could take it out and put it into the function for highest, for starters. Then write a similar one for lowest?
In short - yes. Just put your loops in the functions and fix whatever compiler errors show up.

EDIT: For future reference, you ought to try this kind of stuff on your own before asking about it. Don't worry about messing up your code - that's what Undo is for.

3. Thank you. I spent 11 hours on this last night and we just started functions. So if I had known what to try, then I would have tried it. Thx

The examples in my book are quite ambiguous.

I can't just put my for loops into the functions. The first for loop causes scanf to scan the numbers into the array, what will happen if that part is outside main?

4. Clue (here's a function):
Code:
```float findHighest(float grades[], int len) {
int i;
float high = 0.0f
for(i=0;i<len;i++)
return high;
}```
Here's how you call it:
Code:
`highest = findHighest(grades,N);`

5. Code:
```//This programs prompts the user for the grades of ten tests, then prints the av
erage score, the high score, and a list of scores associated with each student a
nd test, including a letter grade.

#include <stdio.h>

{
int i, int n=10;

float  low=100.0;
for (i=0;i<n;i++)

return low;
}

{
int i, int n=10;
float high = 0.0;
for(i=0;i<n;i++)

return high;
}

#define N 10

int main(void)

{

int i, numraise;
float sum, studentname;
highscore=0.0;
sum=0.0;
average_score= sum/10;

for (i=0; i<N; i++)

{

}

for (i = 0;i < 10; i++)
{
else

printf("Student %d's grade is %.1lf, this is an %c\n", i+1, grade[i], lettergra
de);
}

//printf("The highest score is %.1lf\n", highscore); //Prints high score
printf("the average score is %.2lf\n", average_score); //prints average

printf("The lowest score is: %.2f", findHighest(grades, n));
printf("The highest score is: %.2f", findLowest(grades, n));
return 0;
}```

Almost done, gonna compile

6. Okay can anyone see what is wrong with the above still, I cannot see it wtf...?

Says syntax error in function findlowest

Compiler gives the following errors:

Code:
```P4_2.c: In function `findLowest':
P4_2.c:8: error: syntax error before "int"
P4_2.c:11: error: subscripted value is neither array nor pointer
P4_2.c:11: error: subscripted value is neither array nor pointer
P4_2.c: In function `findHighest':
P4_2.c:18: error: syntax error before "int"
P4_2.c:21: error: subscripted value is neither array nor pointer
P4_2.c:21: error: subscripted value is neither array nor pointer
P4_2.c: In function `main':
P4_2.c:72: error: `grades' undeclared (first use in this function)
P4_2.c:72: error: (Each undeclared identifier is reported only once
P4_2.c:72: error: for each function it appears in.)
P4_2.c:72: error: `n' undeclared (first use in this function)```

7. This is a problem:
Code:
```float findLowest(float grades[], int n)
{
int i, int n=10;```
You have now declared (or tried to declare, since the second one "int" var comma "int" again is a syntax error) more than once.

n is a parameter to the function. It is declared in the (parameters). Also, in this case that parameter is a supplied value: why do you want to redefine it arbitrarily as 10?

8. rewrote it as this:

Code:
```float findLowest(float grades(), int n)
{
int i;
float  low=100.0;
for (i=0;i<10;i++)

return low;
}

{
int i;
float high = 0.0f;
for(i=0;i<10;i++)

return high;
}```
This gives the following error:
Code:
```P4_2.c: In function `findLowest':
P4_2.c:11: error: subscripted value is neither array nor pointer
P4_2.c:11: error: subscripted value is neither array nor pointer
P4_2.c: In function `findHighest':
P4_2.c:21: error: subscripted value is neither array nor pointer
P4_2.c:21: error: subscripted value is neither array nor pointer
P4_2.c: In function `main':
P4_2.c:72: error: `grades' undeclared (first use in this function)
P4_2.c:72: error: (Each undeclared identifier is reported only once
P4_2.c:72: error: for each function it appears in.)
P4_2.c:72: error: `n' undeclared (first use in this function)```

I just wanted n to be 10 because I thought that would cause i to loop 10 times... but I see that different values need to be stored into n?

9. Code:
`float findLowest(float grades(), int n)`
Whoops! Those are not square. Empty square brackets just indicates this is an array of some size (actually it's a pointer, blah blah). Nb, "subscript" notation refers to this:

"[i]" is the subscript. I think the "subscripted value" would be grades.

Originally Posted by LightYear
I just wanted n to be 10 because I thought that would cause i to loop 10 times... but I see that different values need to be stored into n?
Yes, it would cause the loop to iterate 10 times, but you want the loop to iterate N times -- as in, N from main() -- right? That is the length of the array pointed to by the other parameter (grades). And you submitted both of them.

To clarify:
Code:
```void eg(int X) {
printf("The number is %d\n", X);
}```
If I call this function with eg(666), what number do you think will be printed out?
Now, if I insert a new line:
Code:
```void eg(int X) {
X = 10;
printf("The number is %d\n", X);
}```
What will be different?

10. I would think the first one would print 666 or whatever you put there, and the second one, always 10?

Now, in the bottom where I call the functions, the later parameter should be n and not N right?

11. Originally Posted by LightYear
I would think the first one would print 666 or whatever you put there, and the second one, always 10?
Yep. Will grades always have a length of 10? If so, then you don't even need a parameter for this, you could just set it inside the function.

Now, in the bottom where I call the functions, the later parameter should be N right?
Yep too.* Remember: parameters passed from one function to another (such as from main() to findHighest()) do not have to have the same name, they just need to be of the same type. So N from main() could be called len, or n, or whatever you want, in findHighest(). As long as they are both ints, the value will be passed thru unchanged.

* so your edit, which crossed my post is incorrect...hopefully this makes sense

12. Code:
```//This programs prompts the user for the grades of ten tests, then prints the av
erage score, the high score, and a list of scores associated with each student a
nd test, including a letter grade.

#include <stdio.h>
#define N 10

{
int i;
float  low=100.0;
for (i=0;i<N;i++)

return low;
}
}

{
int i;
float high = 0.0f;
for(i=0;i<N;i++)

return high;
}

int main(void)
{

int i, numraise;
float sum, studentname;
highscore=0.0;
sum=0.0;
average_score= sum/10;

for (i=0; i<N; i++)

{

}

for (i = 0;i < 10; i++)
{
printf("Student %d's grade is %.1lf, this is an %c\n", i+1, grade[i], lettergra
de);
}
printf("the average score is %.2lf\n", average_score); //prints average

printf("The highest score is: %.2f\n", findHighest(grades, N));
printf("The lowest score is: %.2f\n", findLowest(grades, N));

return 0;
}```

Here's what I have now, see what I'm asking?

compiler says i is undeclared???

13. Originally Posted by LightYear
Here's what I have now, see what I'm asking?
I understand what you are asking. Read my last post again, about variable names and passing parameters. They are not passed "by name", they are passed by value. The name is just a label for the programmer, in fact it does not exist in the compiled program. "grades" could also have a different name in findHighest(). The choice is yours.

You want to pass (the value of) N, so you refer to it by name (N).

Now, you have a unrelated problem here:
Code:
```int main(void)
{
N has not been defined at this point! If you want (or have) to do this, you will need to use a "dynamic" array, which means using malloc().
Code:
```int main (void) {
int N;

// get a value for N from user

If you use malloc, you must also use free() when you are done with that variable (in this case, at the end):
Code:
`free(grades);`
Also, as I said, parameter names do not need to match but the types should. You declare grades an array of double, but your functions accept floats. Choose one or the other and be consistent (floats are fine here).

14. N has not been defined at this point!
Well actually there's a #define N 10 at the top of the file. I'm pretty sure gcc (probably msvc too) supports declaring an array with a preprocessor macro, as an extension.

EDIT: as far as declaring arrays go, it looks like variables are fair game too

15. Thank you I will try and get it right. We can't use malloc because we haven't studied it yet; I need to do it without malloc.

N is in the #define is that ok?

the compiler says i is undeclared, can somebody compile this and tell me what's up, I changed all the variables to floating point because that has range sufficient and double is overkill like you said and they need to be same type to transfer etc...

Can someone throw this in the toaster and tell me what the deal is? I know this is darn close to right

Code:
```//This programs prompts the user for the grades of ten tests, then prints the av
erage score, the high score, and a list of scores associated with each student a
nd test, including a letter grade.

#include <stdio.h>
#define N 10

{
int i=0;
float  low=100.0;
for (i=0;i<N;i++)

return low;
}

{
int i=0;
float high = 0.0f;
for(i=0;i<N;i++)

return high;
}

int main(void)
{

float grade[N], highscore, average_score, sum, studentname, i, numraise;
highscore=0.0;
sum=0.0;
average_score= sum/10;

for (i=0; i<N; i++)

{

}

for (i = 0;i < 10; i++)
{
else