954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

1-D array

Write a C-Program that swaps the elements of 1-D array (10 elements) :
Example:
If the given array is:
5 8 9 2 3 1 11 17 43 6
The new array will be:
6 43 17 11 1 3 2 9 8 5
Your program should consist of the following functions:
1. Function READ to read the elements of the array
2. Function Display to print the elements of the new array
3. Function SWAP to swap the elements of the array
4. main function to call the previous functions
Note: in swap function, do not use an intermediate array for swapping, i.e. do not
declare a new array and fill it with the elements of the original array starting from the
last element. The swap operation should be done on one array

The size of array must be N (Entered by user)

my question is how can i swap the elements of the array????

jack999
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

hi
looking at ur requierment, u can write in swap funtion like

for(int i=0; i<=4; ++i) {
     arr[i] = arr[i] + arr[9-i] ;
     arr[9-i] = arr[i] - arr[9-i] ;
     arr[i] = arr[i] - arr[9-i]
}

hope this should work for u.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

> looking at ur requierment, u can write in swap funtion like
What is wrong with using a temporary variable?

Not to mention the possibility of data corruption due to whatever way you machine handles arithmetic overflow and underflow.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

>arr[i] = arr[i] + arr[9-i] ;
>arr[9-i] = arr[i] - arr[9-i] ;
>arr[i] = arr[i] - arr[9-i]
That's clever and braindead at the same time. Unless of course you didn't discover that solution, in which case it's just braindead.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

i solved it but the problem is that the first element of new array is always 0

#include<stdio.h>
main( )
{
int i,n,j ;
float arr[5], arr_new[5]   ;
int temp;
 
for (i=0 ; i<5 ; ++i) 
      {
printf ("Enter the %dth element of array :", i) ;
scanf ( "%f", &arr[i] ) ;
       }
for (i=0 ; i<5 ; ++i) 
      {
printf ("the value of element #%d is %f \n", i,arr[i]) ;
       }
for( i = 0, j = 5; i<5; i++,j--)
{
temp = arr[i];
arr[i] = arr_new[j];
arr_new[j] = temp;
}
 
 for (i=0 ; i<5 ; ++i) 
      {
printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;
       }
}
jack999
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

in the for loop that swaps the values, you need to initialize j as 4 and not 5. otherwise it starts by accessing an array element that has not been initialized.

CaliVagabond
Light Poster
35 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

> for( i = 0, j = 5; i<5; i++,j--)
The last element of the array is at index 4, not index 5

for ( i = 0 ; i < 5 ; i++ ) // going forwards
for ( i = 4 ; i >= 0 ; i-- ) // going backwards

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

guys i really got confused when u tryed yo solve iy with functions

#include<stdio.h>
void read(float arr[],int n)
{
int i ;
for (i=0 ; i<n ; ++i) 
      {
printf ("Enter the %dth element of array :", i) ;
scanf ( "%f", &arr[i] ) ;
       }
void swp(float arr[],float arr_new[],int n)
{
int i,j, temp;
for( i = 0, j = n-1; i<n; i++,j--)
{
temp = arr[i];
arr[i] = arr_new[j];
arr_new[j] = temp;
}
}
void display(float arr_new[]);
{
int i;
for (i=0 ; i<n ; ++i)
      {
printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;
       }
}
void main()
{
float a[],b[], c[];
int m;
printf ("Enter the size of array :") ;
scanf("%d",&m);
read(a,m);
swap(a,b,m);
display(c);
}
jack999
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

you only need a single array as a parameter to swap(), and only use a single array within swap.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You