alon davidi alon davidi - 5 months ago 79
C Question

How do I change the array Input Order in C

I need to input five numbers and then a positive or negative. The order will be changed like the number.

For example, if three (3) is the number then:

1 2 3 4 5 6 7 8 9 10

will become:

4 5 6 7 8 9 10 1 2 3

Please do not use pointers. I have made the following code and would like to be able to improve on it. Can I use a mathematical formula using the modulus operator

%
? If so, how would I be able to do it.

Thanks, this is my code.

#include<stdio.h>

#define n 10
int main(void)
{
int num[n] = { 0 }, assist[n] = { 0 }, i = 0, j = 0, variable = 0, k = 0;

for (i = 0; i < n; i++)
{
printf("please enter index. %d\n", i );
scanf("%d", &num[i]);
}
printf("please enter the circle number\n");
scanf("%d", &variable);
printf("\n\n");

if (variable >= 0)
{
for (i = variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}

for (i = 0, j = k; i < variable, j < n; i++, j++)//to assist//
{
assist[j] = num[i];
}
}

if(variable < 0)
{
for (i = n + variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < n + variable, j < n; i++, j++)
{
assist[j] = num[i];
}
}


for (i = 0; i < n; i++)//output//
{
printf("%d\n", assist[i]);
}

return 0;
}

Answer Source

Something like this. I did not compile; any small mistakes are for you.

#include<stdio.h>
#define N 10

int main(void)
{
    int numbers[N];
    int offset;

    for (int i = 0; i < N; i++)
    {
        printf("please enter number %d: ", i);
        scanf("%d", &num[i]);
    }

    printf("\nplease enter the circle number: ");
    scanf("%d", &offset);
    printf("\n\n");

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

    printf("\n");
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download