Chnage the position of element!

E

engartte

Hi all,
Let consider the following solution:

#include <stdio.h>

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

}

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


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

Of Course,at the follow-up solution I am looking to response
to the following question:
Declare an array a[10],then read the integers into the elements
of the array from keyboard.but,insert the first element of the array
into thwe position specified by a following integer and print the
contents as follows:
1 2 3 4 5 6 7 8 9 10
4
2 3 4 1 5 6 7 8 9 10


but,my solution is not satisfy the condition.If you know some other
on,please show me.
tanx in advnce


engartte
 
P

Perceptron

Following is the simple solution for desired output :

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

/* Read all elements */
for( i = 0 ; i < 10 ; ++i )
scanf( "%d", &a );

/* Read desired position */
scanf( "%d", &n );

if( n > 10 )
n = 10;
if( n < 1 )
n = 1;

/* Shift elements */
temp = a[0];
for( i = 0 ; i < n-1 ; ++i )
a = a[i+1];

/* Store first element at desired position */
a[n-1] = temp;

/* Print array */
for( i = 0 ; i < 10 ; ++i )
printf( "%d ", a );

return 1;
}

If you can provide n before array elements the solution can be optimized
to :

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

/* Read desired position */
scanf( "%d", &n );

if( n > 10 )
n = 10;
if( n < 1 )
n = 1;

/* Read fisrt element */
scanf( "%d", &temp );

/* Read other elements in corresponding positions */
for( i = 1 ; i < 10 ; ++i )
if( i < n )
scanf( "%d", &a[i-1] );
else
scanf( "%d", &a );

/* Store first element in desired position */
a[n-1] = temp;

/* Print array */
for( i = 0 ; i < 10 ; ++i )
printf( "%d ", a );

return 1;
}
 
B

Barry Schwarz

Hi all,
Let consider the following solution:

#include <stdio.h>

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

}

printf("\nType a number:");
scanf("%d", &temp);


Assume the user types in 10.
if(temp>10);

This extra semicolon transforms the if statement to a no-op. It has
no effect (except to possibly consume time). I assume you meant the
next statement to be conditional on the if.
temp = a[0];
for (i=0;i<temp;i++)
a = a[i+1];


temp is 10. When i is 9, this statement invokes undefined behavior
since a[i+1] does not exist.
for(i=0; i<10; i++)
printf("%d ",a);
printf("\n");
return 0;
}

Of Course,at the follow-up solution I am looking to response
to the following question:
Declare an array a[10],then read the integers into the elements


You didn't read integers into the array. You assigned values you
decided to the elements. The user had no choice.
of the array from keyboard.but,insert the first element of the array
into thwe position specified by a following integer and print the

After initializing the array, you did slide a number of elements to
the left but you never filled in the "vacated" element.
contents as follows:
1 2 3 4 5 6 7 8 9 10
4
2 3 4 1 5 6 7 8 9 10


but,my solution is not satisfy the condition.If you know some other
on,please show me.
tanx in advnce



<<Remove the del for email>>
 

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,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top