1

It just creates random values

I tried using a separate value for the variable of the array and I also don't know why it starts counting at element 6.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
int array[3][5];

for (i = 0; i<5; i++);
    {
    printf("Input a whole number for row 1 element %d:\n", i+1);
    scanf("%d", &array[0][i]);
    }

printf("Row 1 elements:\n");
for(i = 0; i<5; i++)
{
    printf("%d\n", array[0][i]);
}

return 0;
}

Ouput:

> Input a whole number for row 1 element 6: 4 Row 1 elements: 0 0 0 0
> 1897665488
> 
> Process returned 0 (0x0)   execution time : 1.969 s Press any key to
> continue.
4
  • 1
    I'm curious about the semicolon at the end of this line: for (i = 0; i<5; i++); What if you deleted that semicolon? Commented Mar 26, 2022 at 12:17
  • look, you have 2 dimensional array - i.e. tensor. For better understanding, if you already ok with data type conception. Array of arrays. i.e. each variable of such array - is another array. What does it means int array[3][5]; - array of 3 elements, each element is another array of 5 ints. So to iterate over the tensor you'll need two level loop. Commented Mar 26, 2022 at 12:33
  • for(size_t i=0; i < 3; i++) { cprintf("|%d", array[i][0]); for(size_t j=1; j < 5; j++) { cprintf(", %d", array[i][j]); } cprintf(" |\n");} Commented Mar 26, 2022 at 12:34
  • 1
    @VictorGubin: The function cprintf is not part of the ISO C standard library, but is a platform-specific function. Note that the question is not tagged with any particular platform. Commented Mar 26, 2022 at 13:32

1 Answer 1

1

It starts counting from 6 because the line for (i = 0; i < 5; i++);, is iterating (incrementing) i 5 times so, i becomes 5, then you print i + 1 to stdout.

So, basically your call to printf() and scanf() functions were never a part of any sort of loop.

NOTE: Adding a semi-colon ;, after any loop means that there is no body for the loop. Basically it's an empty loop. It can be useful for finding the length of a string, and so on.

Some tips:

  • Also instead to using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
  • use int main(void) { }, instead of int main() { }
  • always check whether scanf() input was successful or not

Correct Code

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i;
    int array[3][5];

    for (i = 0; i < 5; i++)
    {
        printf("Input a whole number for row 1 element %d:\n", i + 1);
        if (scanf("%d", &array[0][i]) != 1)
        {
            perror("bad input: only numbers are acceptable\n");
            return EXIT_FAILURE;
        }
    }

    printf("Row 1 elements:\n");
    for (i = 0; i < 5; i++)
    {
        printf("%d\n", array[0][i]);
    }

    return EXIT_SUCCESS;
}

Output:

Input a whole number for row 1 element 1:
1
Input a whole number for row 1 element 2:
2
Input a whole number for row 1 element 3:
3
Input a whole number for row 1 element 4:
5
Input a whole number for row 1 element 5:
7
Row 1 elements:
1
2
3
5
7
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.