i gets -

Alright I have done some coding before, and never an into a problem like this.
This is pretty simple code for an array, but I can't get it to not run an infananite loop. What am I doing wrong?

x = 10;
plays[x];
sum = 0;
for(i=0; i < x; i++){
plays[i] = i-x;
sum = abs(plays[i]) + sum;
printf("\nI: %d Plays: %d Sum: %lg\n", i, plays[i], sum);
}
Last edited on
Is this your code?
like that it is never going to be compiled, because you did not declare all those variables.
if you did, check if "x" is const , an array may only have a const number of elements (at least if you declare it that way).
I suggest (not tested)

1
2
3
4
5
6
7
8
9
10
const unsigned int x = 10;
float plays[x];
float sum = 0;

for(unsigned int i=0; i < x; i++)
{
  plays[i] = i-x;
  sum = abs(plays[i]) + sum;
  printf("\nI: %i Plays: %f Sum: %f\n", i, plays[i], sum);
}


See I thought that was it too, but I ran it in both Visual studios and kwrite and got the same problem with both. The only difference I got was with x it could have been either 5 or 10 for kwrite. I can't figure out why this would do that. Also I am not in C99 for the project and can't declare like that I don't think.
Thank you Pete The unsigned was the part I missed, but why would I need that.
It is because the compiler has to make sure that arrays do not have a negative amount of elements.

e.g.
int foo[-2]; //this is obviously nonsense
Topic archived. No new replies allowed.