How about this solution

J

John

Hi,
I am also new with C.I am studying the C in course.
I performed this solution just as Practice to me.
I found this Problem among messages from this page on Dec.27.
The Problem says:

Declare an array a[10], insert the first element of the array into the
position specified by a following integer and print the contents.
output Example:
1 2 3 4 5 6 7 8 9 10
4
2 3 4 1 5 6 7 8 9 10

I tried .the result showed the error in output and it print the
different output and not the same as said.
then what would be the best way to do that?what is the most proper
alternative way to pay for this problem?
Thanks for all.

My Proposed solution is as follows:

#include <stdio.h>

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


for(i=0; i<10; i++){
scanf(" %d", &a);
}

putchar('\n');
for(i=0; i<10; i++){
printf("%d ",a);
}
putchar('\n');


x = a[0];a[0]= a[1]; a[1]=a[2];
a[2]=a[3];a[3]=x;

for(i=0; i<10; i++){
printf("%d ",a);
}
putchar('\n');
return 0;
}
 
J

Jon Yi \(???\)

Declare an array a[10], insert the first element of the array into the
position specified by a following integer and print the contents.
output Example:
1 2 3 4 5 6 7 8 9 10
4
2 3 4 1 5 6 7 8 9 10

I tried .the result showed the error in output and it print the
different output and not the same as said.
then what would be the best way to do that?what is the most proper
alternative way to pay for this problem?
Cash in an envelop is the best. I will take Paypal. The amount is up to you.
for(i=0; i<10; i++){
scanf(" %d", &a); --- for testing, just do a = i;
}
x = a[0];a[0]= a[1]; a[1]=a[2];
a[2]=a[3];a[3]=x; ---- > assuming the input was 4 is not good. Get it from

the user, then use a loop to copy. Flexibility is what you want.
Here is my version that seems to do what you want.
#include <stdio.h>

int main ()
{
int a[10], x, temp;
int i;
for(i=0; i<10; i++){
a = i;
printf("%d ",a);
}

printf("\nEnter a number:");
scanf("%d", &x);
if(x>=10) return 1;
temp = a[0];
for (i=0;i<x;i++)
a = a[i+1];
a[x-1] = temp;

for(i=0; i<10; i++)
printf("%d ",a);
printf("\n");
return 0;
}
 
J

John

Thanks Jon Yi,
But I was asked on starting from 1 and not zero.
as you put:
int i;
for(i=0; i<10; i++){
a = i;
printf("%d ",a);

it bring the output from 0 to 9.
now,I am asking if i put like this:

a=i +1

then,the out put will show:
1 2 3 4 5 6 7 8 9 10
what do you think about?Is it a correct way or no?

Cheers!
John

}
 
L

Lawrence Kirby

Thanks Jon Yi,
But I was asked on starting from 1 and not zero.
as you put:
int i;
for(i=0; i<10; i++){
a = i;
printf("%d ",a);

it bring the output from 0 to 9.
now,I am asking if i put like this:

a=i +1

then,the out put will show:
1 2 3 4 5 6 7 8 9 10
what do you think about?Is it a correct way or no?


Yes, that's fine. If you need to read the numbers in you can still do it
as you did originally.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,157
Messages
2,570,879
Members
47,413
Latest member
KeiraLight

Latest Threads

Top