# Input to array loops forever [C]

• 11-08-2011
Quixiotic
Input to array loops forever [C]
Alright I'm tackling an assignment and I just started with the input of numbers into an array. It seems to be working well, but what happens is once the for loop should be over, it repeats but not at zero like it should (If I wanted it to, which I don't). I'm confused so here's my code:
Code:

```#include <stdio.h> #define ARRSIZE 19 int main(){  int temp[ARRSIZE];  int press[ARRSIZE];  int i;  printf("\n--------------------------------------------------\n");  printf("  Please enter a temperature and a pressure value.\n");  for(i = 0; i <= ARRSIZE; i++){   printf("%d: ", i+1);   scanf("%d %d", &temp[i], &press[i]);   } /* for() filling arrays with input */  printf("Thank you!"); return 0; } /* main() */```
When it starts the next iteration it starts with i=2 which I'm confused about only slightly less than why it's even looping again in the first place.
• 11-08-2011
stahta01
The code below results in undefined/unallocated space being used.

Code:

`for(i = 0; i <= ARRSIZE; i++){`
change to the use of "<=" should be "<".

Code:

`for(i = 0; i < ARRSIZE; i++){`
The location temp[ARRSIZE] is NOT in temp!

Tim S.
• 11-08-2011
Salem
> for(i = 0; i <= ARRSIZE; i++)
I don't know about the start, but on the last iteration of this loop, you're stepping off the ends of the arrays (and possibly trampling all over your subscript).

At which point, all bets are off.

Use <ARRSIZE, not <=ARRSIZE
• 11-08-2011
Quixiotic
Well thank you all so much, sure did fix the issue. So just that I have a better understanding of the issue, is the final iteration when it reaches 19 that it's going out of the range?
• 11-08-2011
jimblumberg
Arrays start at zero an stop at SIZE - 1. So in your case 0 - 18.

Jim
• 11-08-2011
CommonTater
Quote:

Originally Posted by Quixiotic
Well thank you all so much, sure did fix the issue. So just that I have a better understanding of the issue, is the final iteration when it reaches 19 that it's going out of the range?

A common error for people to make is confusing "the number of elements" with "the number of the element".

This is because we humans mostly start counting at 1... C starts at 0.

As Jim explains if you have a 10 element array you have valid subscripts 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 ... it's ok, count them, there are 10.