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

void main()
{
int a[400],n,i;
printf("enter the no of elements: ");
scanf("%d",&n);
printf("enter the no: ");
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
n=n+1;
printf("enter the no: ");
scanf("%d",a[n]);
for(i=0;i<n;i++)
{
    printf("%d",a[i]);
}

}

i inserted elements after that i again inserted its not working.it shows 0 errors but it gets inserted when i try to display compiler stops working see the figure below
2
Contributors
1
Reply
4
Views
4 Years
Discussion Span
Last Post by sfuo
0

On line 16 you need scanf("%d", &a[n-1]);

It was not working because scanf() takes in a pointer to the variable in which you want to write to.

The reason why you need to write to n-1 is because if you have an array that has a length of 5 the indicies that you should be accessing for that range are 0, 1, 2, 3, 4 (note that you do not access 5).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.