0

Hi,

Let's say I have an array of 10 elements. I want user to enter 9 numbers so that they fill arrays 0 to 8 (9 numbers).
Now I shift the arrays +1. so array[0] is now array[1] and so on.
Now I ask user to enter 10th number (fills array 0).

how can I do that??

Here's my code(it doesn't shift arrays and doesn't ask for 10th num)

int main()
{
    int a[10];
    int i;


    printf("\nEnter 9 numbers:\n");


for(i=0;i<9;i++)
{
    printf("%10d numbers remaining\n\n", 9-i);
    scanf("%d", &a[i]);
}


printf( "%s%13s\n", "Element", "Value" );
for(i=0;i<10;i++)
{
    printf("%7d%13d\n",i, a[i]);
}


}
2
Contributors
2
Replies
25
Views
2 Years
Discussion Span
Last Post by Vahid_1
1

Shifting array to the right by one:

for(i=8; i>=0; i--)
{
    a[i+1] = a[i];
}

But instead of filling array from positions 0-8 and than shifting everything 1 place to the right you can fill array from positions 1-9:

for(i=0; i<9; i++)
{
    printf("Enter number into a[%i]:\n", i+1);
    scanf("%i", &a[i+1]);
}

-
-

User entering number 10 into position 0:

printf("Enter 10th number:\n");
scanf("%i", &a[0]);//previous value at a[0] will be overwritten

also you forgot to put return at the end of main
main () { /* code */ return 0; }

Edited by 111100/11000

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.