473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Chnage the position of element!

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] = i+1;
printf("%d ",a[i]);

}

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

Nov 14 '05 #1
3 1335
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[i] );

/* 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[i] = a[i+1];

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

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

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[i] );

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

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

return 1;
}
Nov 14 '05 #2
On 4 Jan 2005 22:20:44 -0800, "engartte" <na*****@hotmail.com> wrote:
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] = i+1;
printf("%d ",a[i]);

}

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[i] = 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[i]);
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>>
Nov 14 '05 #3
On Wed, 05 Jan 2005 22:24:37 -0500, "Perceptron"
<<undisclosed>@<undisclosed>.<undisclosed>> wrote:
Following is the simple solution for desired output :

int main()
{ snip return 1;
Why return a non-portable value from main??
}

snip
<<Remove the del for email>>
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Igor | last post by:
Is there any way to resort and xml document using xslt based on element position. For example if I have xml like this: <root> <element> 1st thing </element> <element> 2nd thing </element>...
1
by: Eshrath Ali Khan | last post by:
Hi, I have a requirement where I am transforming a XML into a html using an XSL. I need to create a object for the each and every occurrence of an element named "tgroup" in the xml. Also I need...
14
by: Zenobia | last post by:
Hello folks, Is it possible to position an item within a <td> element? For instance see below. The table has multiple rows, one for each database record. Each row has 3 hrefs associated with it...
1
by: J. Potts | last post by:
Just out of curiosity, has anyone run into this problem? Someone where I work put a set of pages together with the following in the style sheet: BODY { position: absolute; margin-top: 0mm;...
3
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following...
1
by: travis_brooks | last post by:
greetings I had a javascript that was reliably telling me the position of an image on a page, and now its not. I was using the script to draw a bounding box that followed the mouse around over...
6
by: axlq | last post by:
I've spent most of the day struggling with what I thought would be a trivial problem. I have a hidden element that appears, outside of the normal flow, when the mouse hovers over an inline...
0
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my Win32 application, I've a panel with a picturebox containg a picture, i want that the scrollbar won't start from position 0, but from 120 (for example), on Load i change the value to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.