This is confusing me a little and I want to fully understand it before moving through the rest of the lecture. I usually code alongside David's examples on screen and was trying to understand why this makes sense
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Number of scores: ");
int scores[n];
for(int i = 0; i < n; i++)
{
scores[i] = get_int("Score %i: ", i + 1);
}
printf("Average: TO DO\n");
}
and yet this doesn't (and also produces a run-time error in sandbox):
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Number of scores: ");
int scores[n];
for(int i = 0; i < n; i++)
{
scores[n] = get_int("Score %i: ", i + 1);
}
printf("Average: TO DO\n");
}
The error I got was
runtime error: index 3 out of bounds for type 'int [n]'
I'm confused as to why the one in the loop can't be n? I can't even begin to describe my logic on this...