# Thread: Can anyone tell me what's wrong with this program?

1. ## Can anyone tell me what's wrong with this program?

Hi, we have just begun studying arrays and functions, and I have spent the weekend working on this, what is relatively very simple, source.

Can anyone help me fix this? I have been trying to change it to include two functions, findLowest and findHighest. They are supposed to print the highest and lowest of ten grades at the end of the program.

Here's the source:

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

{
float i=0;
float  low=100.0;
for (i=0;i<N;i++)
printf("The lowest score is: %.2f\n", low);

return 0;
}

{
float i=0;
float high = 0.0f;
for(i=0;i<N;i++)
"P4_2.c" 68 lines, 1388 characters
printf("The highest score is: %.2f\n", high);

return 0;
}

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

e);
}
printf("the average score is %.2f\n", average_score); //prints average

findHighest(i);
findLowest(i);
return 0;
}```

Thank you!

2. Is there something wrong with it? If so, what? (Or at least, what do you notice that is incorrect?)

3. Passing an integer to functions that expect an array of floats...not gonna work.

4. where do you see an integer? Everything is defined as being float? (the N?)

Also, I thought integers were converted to floats if needed?

Okay...so the i is the grades being stored in the array, yes?

So does that make the call of the functions at the end correct at least?
What do I need to do, write #define N 10.0f ? I've never seen that done with any of our examples, it's always been just a constant. How does this work?

As far as I can tell, I am passing i, which is float, into an array of floats?

Come on, shed some light, I've spent over 20 hours on this one thing, adding functions, just to get 10 extra points. I have calc III test next week, and chem, and cmp science test. I really need to finish this already. It's the weekend, can't call the professor.

That's not an array parameter! Post your compile errors next time.

Don't use non-void return types if you aren't going to return anything meaningful, or take notice of the return value in the caller.

6. I didn't have float grades(N) earlier, but in another thread a more experienced poster suggested that be my prototype...that's what led to all this confusion I think.

Why isn't it N? This is an array of 10 numbers, and N is 10.

I am wondering, does #define N 10 make N an integer 10?

Then that N integer is being stored into a float array? Is that a problem or will integer upconvert to float in this case?

An off topic question; does anyone use a debugger, are they helpful for simple programs like this? We were told not to use them

7. 1) You need to add the variable lowscore to the list of floats, in main().
2) you need to add highscore to the parameter list for findHighest()
3) you need to add lowscore to the parameter list for findLowest()
(by this I mean in the call to the function, and in the functions parameters list, both)

4) You need to return the highscore (or lowscore for findLowest), from these two functions.

5) The grades array only deals with 1 students grades. So lowscore and highscore have to be called (both of them), after every student's grades has been entered, but before another students grades start being entered, into the grades[] array.
They are both "running" totals, that have to be updated for each student's grades.

That should get you closer.

8. 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

{
float i=0;
float  low=100.0;
for (i=0;i<N;i++)
printf("The lowest score is: %.2f\n", low);

return 0;
}

{
float i=0;
float high = 0.0f;
for(i=0;i<N;i++)
printf("The highest score is: %.2f\n", high);

return 0;
}

int main(void)
{

float grade[i], 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

e);
}
printf("the average score is %.2f\n", average_score); //prints average

findHighest(i);
findLowest(i);
return 0;
}```

Compiler errors:

God Admiral is being so slow. I will post errors later if I can't get this to work
between now and then. I need a break...

Adak (and claudiu) you're my hero

9. Sorry about my 'lil screw-up earlier, LightYear, I hope I did not screw up too much.

Originally Posted by iMalc
That's not an array parameter! Post your compile errors next time.
I was going to say something along these lines before. It looks to me like you are trying to "compose" most of a program and then compile it. That is a bad way to write. You should compile (and test, if possible) every 5-10 lines you write, even if this means having to shim something in temporarily to make it fit together, because that saves more time than trying to demangle 50 lines of untested code. So even if the compiled program doesn't do anything, you should still do this. Compile every 5-10 lines with warning on and pay attention to the error and warnings, which it the point of test running something thru the compiler.

To try and make up for my mistake, I corrected the errors enough in your code (from post #1) to make it compile and run.
Code:
```#include <stdio.h>

#define N 10

{
int i=0;	// NOTE ONE
float  low=100.0;
for (i=0;i<N;i++)
printf("The lowest score is: %.2f\n", low);

return 0;
}

float findHighest(float grades[N])	// NOTE TWO
{
int i=0; //NOTE ONE
float high = 0.0f;
for(i=0;i<N;i++)
printf("The highest score is: %.2f\n", high);

return 0;
}

int main(void)
{

int i;  //NOTE ONE
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 %.1f, this is an %c\n", // NOTE THREE
}

printf("the average score is %.2f\n", average_score); //prints average

return 0;
}```
Here's what was wrong:

NOTE ONE:
If you use i as an array subscript, i must be an integer. There is no such element as "array[2.4]", so floats are not allowed.

NOTE TWO:
We've been thru this one -- () vs. [], it's more or less a typo.

NOTE THREE:
Your conversion specifiers should match your types. You had i as a float, but it need to be an int, so I changed a %f to a %d appropriately here.

NOTE FOUR:
This one is kind of interesting. Why do you think you should be calling those "find" functions with i, instead of the array, grade?

 just glancing thru the recent posts and noticed Adak is now doing the same thing...well, you'll get a few opinions then...

10. I don't even have a clue why i *is* a float. It's an index for your array.

Even so, if you don't know the difference between a float and an ARRAY of floats, I doubt you *deserve* extra credit.

11. Ouch that burns. As someone who is just learning this, let me say you appear tactless and rude. Being a wise ass doesn't do anyone any good does it?

However, I do see that i is only used as an index now, so I can make that an integer? I received conflicting information on that, and being new to this, it confused me as to what exactly is going on in these functions (storing variable x (or y, z anything) into a variable with a different name) and in the arrays (same problem.)

a float is an approximation allowing for the use of decimals. In size, char is smallest, followeed by signed short int, then int, then float, then double, then long double, then unsigned long double. I may have left a couple out.

An array can store floats, but its range has to be an integer, the number of boxes it contains. This is right yea?

The problem I've been having is in the logic of all of this.

Thanks MK, that is what I needed. You see rags, I can print something like he has posted, and keep it in my notes. Helps my learning style.

12. Or you can hand it in as your own work and get the "extra credit" you're looking for. Call me tactless and rude, I don't care. You're a cheater who used the generosity of several people here to write your program for you.

13. That's cool MK27 thank you. I am scrolling up and down, looking at mine, looking at yours.

I am glad I had the logic right mostly. You know when you're just learning something, a small error can influence your judgment and throw you wildly off course. That's where I was before you came on.

That typo () that's a bad habit I'm working on it heh

As for note four; that is something I've been having a problem remembering, but this is only going to be week two of arrays, this is helping me get it down.t

I have been trying to write the whole thing and compile. You make a good point about that, don't really know what to say. I will consider and implement that advice. I had the program running perfect without the functions, but as I went back in and started trying to add functions (which I am just learning), then changed everything from double to float. Each of these steps added errors.
Looking at my text, the chapter on functions has very poor examples. I will have to find more online.

rags to riches you're an asshat. I didn't post anything without first trying it. I have tests in this course that no one can help with. The point of these projects is to learn to program so that you can do it from memory ON the tests.
This is a great resource, and you do a disservice to everyone when you post the way you do.

Why don't you go pull the legs off a spider or something...troll

14. Also, this thread will be useful to anyone compiling something similar, as it clearly illustrates the steps with which an inexperienced newbie programmer might go in walking the path to wisdom, so to speak.

15. There are many ways that this program could be done. This is how I'd do it to handle lowscore, and get the rest of the tweaks it needed, corrected.

findHighest() should be a near-copy of findLowest(), and I leave that undone.

You know that this program will only process one student, right? It handles 10 scores, but that's only enough for 1 student.

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;
for (i=0;i<N;i++) {
}
}

{
float i=0;
float high = 0.0f;
for(i=0;i<N;i++)
printf("The highest score is: %.2f\n", high);

return 0;
}
*/
int main(void)
{
int i;
float lowscore, highscore, average_score, sum, studentname, numraise;
float grade[N] = { 0 };
lowscore = 0.0;
highscore=0.0;
sum=0.0;

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

{
}

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

}
average_score= sum/N;
printf("the average score is %.2f\n", average_score); //prints average

// findHighest();
printf("the lowest score is %.2f  the highest score is ", lowscore);

return 0;
}```
Good luck with your exams, LightYear. Rags means well, just doesn't emphathize atm.