Program accepts input but does not return result as intended
I'm working on a simple program that accepts scores from 20 games. It then is supposed to determine which one is the high score and return that to the screen. The program compiles without errors (using Code::Blocks), and it accepts all 20 inputs, however when it comes to returning the top score, it always returns 0. This is regardless of what numbers are entered. I'm thinking that either a number isn't being stored in the points2 array, highscore integer, or isn't being returned to the variable number. Any help would be much appreciated.
Thanks,
Code:
#include <stdio.h>
#include <stdlib.h>
#define RANGE 21
int top_score(int[],int);
int main(void)
{
int points[RANGE];
int game = 0;
int number;
char instring [20];
for (game = 1; game <= 20; game++)
{
printf ("Enter points scored in game number %d", game);
gets (instring);
points [game] = atoi (instring);
}
number = top_score(points,game);
printf ("Highest points scored in a game is %f", number);
}
int top_score (int points2[], int game2)
{
int index;
int highscore;
highscore = points2[0];
for (index = 1; index < game2; index++)
{
if (highscore < points2[index])
{
highscore = points2[index];
}
}
return (highscore);
}
Gives output value now...
However, the value is always 2,293,580. This is strange to me because I'm usually only inputting between 1 and 3 for my individual score values. I'm not sure where this large number is coming from. I used the example from v333k to show what was being stored in my array. It shows as going in correctly. So, I'm thinking that something is wrong in my top_score function.
Thanks msh and tabstop for the help. I have the game index starting at 1 because my array is length 21. Starting it at one prompts to user to input the score for Game 1 instead of Game 0. I figured I'm still only taking 20 entries.
Code:
#include <stdio.h>
#include <stdlib.h>
#define RANGE 21
int top_score(int[],int);
int main(void)
{
int points[RANGE];
int game = 0;
int number;
char instring [20];
for (game = 1; game <= 20; game++)
{
printf ("Enter points scored in game number %d", game);
scanf("%d", &points[game]);
printf("Score %d = %d\n", game, points[game]); /*shows what's in my array*/
points [game] = atoi (instring);
number = top_score(points,game);
}
printf ("Highest points scored in a game is %d", number);
}
int top_score (int points2[], int game2)
{
int index;
int highscore;
highscore = points2[0];
for (index = 0; index < game2; index++)
{
if (highscore < points2[index])
{
highscore = points2[index];
}
}
return (highscore);
}