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????

Recommended Answers

All 8 Replies

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.

> 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.

>arr = arr + arr[9-i] ;
>arr[9-i] = arr - arr[9-i] ;
>arr = arr - 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.

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]) ;
       }
}

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.

> 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

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);
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.