Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why the array output only the 41946123 number instead of elements in order? What's wrong?

+2 votes
asked Feb 21 by Alex Kole (230 points)
#include <stdio.h>
int main()
    int i, x, num, sum;
    printf ("Enter the size of the array: \n");  scanf("%d", &num);
    printf ("Enter the 1st element: \n");          scanf("%d", &x);
    int A[num];
    A[0]=x; A[1]=x + 10;
   for(i=2; i<num; i++);
     printf("%d", A[num]);
  return 0;

5 Answers

+1 vote
answered Feb 21 by Saleh (460 points)
selected Feb 25 by Alex Kole
Best answer

You need first to get rid of the semicolon at the end of for(int i .....) line. if you put semicolons right after for lines, it will loop but it is not going to show any outputs

Secondly, you can't print array's element at one time, you need a loop to go through all elements in your array

Instead of printf("%d", A[num]) use this

for (int i = 0; i<num;i++) 


    printf("%d ", A[i]);


commented Feb 25 by Alex Kole (230 points)
Yeah, thx man. It was the main problem of solving the task.
0 votes
answered Feb 21 by Ishan Srivastava (140 points)
When you define an array suppose A[int num] then the elements get the indices from 0 to num-1

when you are printing it you have written A[num] that is not right. The last element of the array will have the index num-1 so try using A[num-1] for printing the last element.

if you want to print all the elements try using loops.
+1 vote
answered Feb 22 by Peter Minarik (22,320 points)

Your array A has num elements in it. Indexing in most programming languages is 0-based. So when you try to access A[num] you're indexing outside of the array as the last element's index is num-1. (If this is not clear, try it with a small number, e.g. 3 and index the elements one by one starting from 0.)

+1 vote
answered Feb 22 by DefualtCoder (160 points)

The for loop 

for(i=2; i<num; i++);

has an extra semi-colon at the end. This will only increment i until the condition is met and only the elements A[0] and A[1] are initialized to a valid value. 

Also the printf() function 

printf("%d", A[num]);

with the A[num] is printing out A[5] which the other Answer by Peter Minarik points out needs to be A[num-1] since we are using zero-based arrays. To print out each element we could us a separate for loop like:

for(i=0; i<num; i++);
    printf("%d ", A[i]);
commented Feb 23 by Peter Minarik (22,320 points)
Good point! I missed that there was a semicolon at the end of the for loop.

However, you made the same mistake:

for(i=0; i<num; i++); { printf("%d ", A[i]); }

contains an unwanted semicolon after the for statement.
0 votes
answered Feb 25 by Pale Atanasova (140 points)

 A[num-1] is last

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.