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]);
}


}
Member Avatar for 111100/11000

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; }

It works!

Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.