user5818725
user5818725

Reputation:

Looping over array element produces strange value on last value of array counter

Right so i have this exercise i'm supposed to complete: Exercise Specification

And i'm having difficulty making sure the numbers in the array match up with the counter. The main issue is that on the last counter (49) there seems to be a number that isn't in the array. As you can see here: Output of current program

I am fairly new to C programming so I can be sure I'm doing something wrong, i'm just not sure what. Also how would I add the stars to match the lottery array like on the Spec.

Here is the current state of my code:

    int main()
{
        //char star;
        int i;
        int lottery[49] = { 23,16,18,19,26,13,22,    /*  1 ..  7 */
                            20,14,22,18,21,15,17,    /*  8 .. 14 */
                            24,15,18,20,13,14,20,    /* 15 .. 21 */
                            18,22,20,16,19,11,20,    /* 22 .. 28 */
                            16,28,22,20,15,17,17,    /* 29 .. 35 */
                            21,21,19,20,14,22,25,    /* 36 .. 42 */
                            19,17,26,18,20,23,12 };  /* 43 .. 49 */                         

        for (i = 0; i <= 49; i++)                   
        printf(" %2d  (%d)  |\n", i, lottery[i]);


    return 0;
}

Note: I've declared a variable for the stars already, but i'm just not sure how to implement it to the array to meet the specification of the exercise.

Upvotes: 0

Views: 93

Answers (4)

Bert
Bert

Reputation: 2244

Change your code to this:

for (i = 0; i < 49; i++)
printf(" %2d  (%d)  |\n", i+1, lottery[i]);

This way you only run through the loop 49 times (remember arrays start at 0), and the i+1 in the printf will fix the off by one error.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

C arrays have 0-based indexing. For example, in your particular case, an array having 49 elements, you can have a valid access for index 0 to 48, inclusive. You should change

 for (i = 0; i <= 49; i++)

to

for (i = 0; i < 49; i++)

Otherwise, you'll be off-by-one and thereby accessing out of bound memory. This invokes undefined behavior.

That said, int main() should be int main(void) to conform to the standard.

Upvotes: 2

Snild Dolkow
Snild Dolkow

Reputation: 6876

In C (and many, but not all, other programming languages), arrays are 0-indexed, meaning that they start counting the index at 0.

This means that if you have an array of N elements, the last valid index is N-1.

In your case, you have 49 elements, but your loop condition is i <= 49, which means that you will try to read index 49 from your array. Since that's outside of the valid range, you're reading whatever value happens to be placed after your array in memory.

Upvotes: 1

Giorgi Moniava
Giorgi Moniava

Reputation: 28685

for (i = 0; i <= 49; i++)

Array access out of bounds. When you have array like this:

int lottery[49];

you can only access elements with index [0,48]. Otherwise this is undefined behaviour and all bets are off. The bad thing is compiler might not warn you or report error in such cases.

Change to:

for (i=0; i<49; i++)

Upvotes: 5

Related Questions