|
-
Feb 25th, 2003, 12:46 AM
#1
Thread Starter
Hyperactive Member
Array Problem....
Ok, I'm learning arrays and I pretty much have the concept of them and how they work.. now my problem is that I dont get how to store things in them...
anyway, I've been working on this problem i'm given and it doesnt look too good:
PHP Code:
#include <stdio.h>
#define GRADES 10
main()
{
int Ngrades[GRADES] = {0}, i, loop = 1;
float total, avg;
while (loop == 1) {
printf("Please enter %d grades\n", GRADES);
for (i=0; i<10; i++) {
scanf("%d", &Ngrades[i]);
printf("Calculating......");
for (i=0; i<10; i++) {
total += Ngrades[GRADES];
}
avg = (float) total/(Ngrades[GRADES]);
printf("The %d numbers entered were: \n", GRADES);
for (i=0; i<10; i++) {
printf("Ngrades[%d] = %d\n", i, Ngrades[i]);
}
printf("\n");
printf("the avg of the %d numbers was: %.1f\n", GRADES, avg);
}
}
return 0;
}
now.. for some reason, instead of putting the 10 numbers in one array (one demention) it puts each number in a new array....
what I need it to do is get the 10 grades from ther user, display those 10 numbers, then take the average of all of them...
then there are other things like calculating the letter grade based on teh average but for now I want to get this straight 
thanks for any help!
-Emo
Last edited by Emo; Feb 25th, 2003 at 04:08 PM.
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 25th, 2003, 12:22 PM
#2
??? In the average calculation:
(Ngrades[GRADES])
I think you mean
GRADES
Also your for-loops should use GRADES as the upper bound.
And
now.. for some reason, instead of putting the 10 numbers in one array (one demention) it puts each number in a new array....
:***:
You only have one array...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 25th, 2003, 04:06 PM
#3
Thread Starter
Hyperactive Member
Originally posted by CornedBee
:***:
You only have one array...
ok but then why do I get this output:
[quote]
Please enter 10 grades
98 45 88 66 52 65 78 87 65 95
Calculating......The 10 numbers entered w
Ngrades[0] = 98
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -76.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 45
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -66.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 88
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -56.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 66
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -46.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 52
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -36.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 65
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -26.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 78
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -16.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 87
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: -6.2
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 65
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: 3.8
Please enter 10 grades
Calculating......The 10 numbers entered w
Ngrades[0] = 95
Ngrades[1] = 0
Ngrades[2] = 0
Ngrades[3] = 0
Ngrades[4] = 0
Ngrades[5] = 0
Ngrades[6] = 0
Ngrades[7] = 0
Ngrades[8] = 0
Ngrades[9] = 0
the avg of the 10 numbers was: 13.8
Please enter 10 grades
[quote]
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 25th, 2003, 06:13 PM
#4
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 25th, 2003, 06:16 PM
#5
if only you had proper indentation...
Code:
#include <stdio.h>
#define GRADES 10
int main()
{
int Ngrades[GRADES] = {0}, i, loop = 1;
float total, avg;
while (loop == 1)
{
printf("Please enter %d grades\n", GRADES);
for (i=0; i<GRADES; i++)
{
scanf("%d", &Ngrades[i]);
printf("Calculating......");
for (i=0; i<GRADES; i++)
{
total += Ngrades[GRADES];
}
avg = (float) total/(float)GRADES;
printf("The %d numbers entered were: \n", GRADES);
for (i=0; i<GRADES; i++)
{
printf("Ngrades[%d] = %d\n", i, Ngrades[i]);
}
printf("\n");
printf("the avg of the %d numbers was: %.1f\n", GRADES, avg);
}
}
return 0;
}
You should now be able to find the error yourself.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 25th, 2003, 08:18 PM
#6
Thread Starter
Hyperactive Member
it's something in the last for loop... hmmm...
dont tell me!! 
lets see..
for (i=0; i<GRADES; i++)
{
printf("Ngrades[%d] = %d\n", i, Ngrades[i]);
}
if I replace teh i<GRADES to 1, it gets rid of the un-needed lines but it's not displaying my numbers right...
maybe in the Ngrades[i]; ...
ok.. I'll keep trying...
thanks for all the help so far.... and by the way, sorry, I havn't indented a lot so I dotn know how to do it correctly..
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 26th, 2003, 06:35 AM
#7
A hint: the problem lies not in a single for-loop but rather in the way they act together.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 27th, 2003, 04:28 PM
#8
Thread Starter
Hyperactive Member
Ok I finally got some work done...
I fixed the array and right now it looks something like:
Please enter 10 grades
15 16 18 17 54 68 48 85 16 98
The 10 numbers entered were:
Ngrades[0] = 15
Ngrades[1] = 16
Ngrades[2] = 18
Ngrades[3] = 17
Ngrades[4] = 54
Ngrades[5] = 68
Ngrades[6] = 48
Ngrades[7] = 85
Ngrades[8] = 16
Ngrades[9] = 98
Calculating......
the avg of the 10 numbers was: -10737374.0
Please enter 10 grades
so all I have left is the avg to be fixed and then the letter grades according to the avg...
Thanks for the help so far guys!
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 27th, 2003, 04:33 PM
#9
Frenzied Member
Take a really close look at this part:
Code:
for (i=0; i<GRADES; i++)
{
total += Ngrades[GRADES];
}
hehe can happen mate
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 27th, 2003, 04:55 PM
#10
Thread Starter
Hyperactive Member
w0000t I'm on a roll tonight
jsut got the avg to work right... but does anybody remember how to use the fflush() command because I need to clear out the buffer after each use.. or the avg keeps adding the old one..
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 27th, 2003, 04:56 PM
#11
Thread Starter
Hyperactive Member
Originally posted by Jop
Take a really close look at this part:
Code:
for (i=0; i<GRADES; i++)
{
total += Ngrades[GRADES];
}
hehe can happen mate
Yeah I figured it out..thanks! I had to set total to 0 then I modified it to this:
for (i=0; i<GRADES; i++)
{
total = total += Ngrades[i];
}
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 27th, 2003, 04:59 PM
#12
Frenzied Member
Code:
total = total += Ngrades[i];
hmm that should either be:
Code:
total = total + nGrades[i];
or
Code:
total += nGrades[i];
actually is read by the compiler as
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 27th, 2003, 05:05 PM
#13
But actually
total = total += Ngrades[i];
will work too, although it's confusing and an unnecessary assignment. It's equivalent to writing
total += Ngrades[i];
total = total;
which undoubtly works
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 27th, 2003, 05:12 PM
#14
Thread Starter
Hyperactive Member
well it works and I'm happy with that.... now can anybody help me with the fflush?
thanks
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 27th, 2003, 05:16 PM
#15
Frenzied Member
This is what MSDN says about it:
Code:
fflushSee Also
Stream I/O Routines | fclose | _flushall | setvbuf
Requirements
Function Required header Compatibility
fflush <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Flushes a stream.
int fflush(
FILE *stream
);
Parameter
stream
Pointer to FILE structure.
Return Value
fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.
Note If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions.
It expects a stream, and since you're using the keyboard in this case (actually you're using the standard input stream) you should pass it the stdin (Standard Input Stream)
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 27th, 2003, 05:21 PM
#16
Thread Starter
Hyperactive Member
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|