-
Array question
Hello there-
I'm drawing a blank here with this problem: create an array of grades based on user input, add up the scores and return a letter grade. Anyhoo...what I can't seem to figure out is what the best way to create this array. I would like the user to be able to add each score one at a time ("87", "78", "100", "97"...) until the Enter key is pressed.
Any help is extremely appreciated!!!
-
well, here is simple algorithm
Code:
integer scorearray;
do
{
prompt user to enter score
get val
scoresarray <- val
}while val not equal to '\n';
find the sum of all the scoresarray
grade <-- findgrade(send the sum);
output grade
u will have to think about the findgrade function
hope this help you
ssharish2005
-
Presumably it's an assignment. Does the assignment mention a maximum number? In which case, create an array of N grades, then have it not allow the user enter more than N values.
If it truly has to be of arbitrary length, you could use a pointer and realloc, or ask the user how many grades they will enter, then malloc to that amount.
Finally, you could use a linked list or something, but that's somewhat overkill for this.
-
Thank you both.
This is what I had in the meantime - I think I'm on the right track.
Code:
do
{
printf ("\nPlease enter assignments scores, one at a time. Press Enter key when done:");
scanf ( "%i", aScore);
} while ('\n'!=getchar());
-
Code:
int grades[100]={0}, i=0;
while(scanf("%d",&grades[i]) == 1 && i++<100);
could be one solution...