1. ## 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:
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????

2. Use a temp variable.
Code:
```int arry[5] = { 1, 2, 3, 4, 5 };
{
int temp = arry[0];
arry[0] = arry[1];
arry[1] = temp;.
}```
etc, etc..

3. Or maybe:
Code:
```for(int i = 0; i < N; i++)
i_new_array[i] = i_old_array[N-i]```

4. SlyMaelstrom

i didnot get it
can u explain more

5. What more do you want me to say, OP? You use a temp variable. Meaning to say, the value you're about to overwrite, you store somewhere else so you still have it available to swap with.

If you have two big TVs in two boxes and they have to swap boxes, what do you do? You take one out and you put it somewhere else temporarily until you move the other one into it's box. Then to take it out of the temp space and put it in the other box.

6. Originally Posted by cunnus88
Or maybe:
Code:
```for(int i = 0; i < N; i++)
i_new_array[i] = i_old_array[N-i]```

[0] = 5 - [1] = 3 - [2] = 7 - [3] = 9
N = 3

i_new array[0] = i_new_array[3];

[0] = 9 - [1] = 3 - [2] = 7 - [3] = 9

i_new array[1] = i_new_array[2];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

i_new array[2] = i_new_array[1];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

i_new array[3] = i_new_array[0];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

See? You gotta have that temp variable.

[0] = 5 - [1] = 3 - [2] = 7 - [3] = 9
N = 3

i_new array[0] = i_new_array[3];

[0] = 9 - [1] = 3 - [2] = 7 - [3] = 9

i_new array[1] = i_new_array[2];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

i_new array[2] = i_new_array[1];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

i_new array[3] = i_new_array[0];

[0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

See? You gotta have that temp variable.

i didnt undestand`anything

8. Mmm, sorry if that confused you, it was directed more toward cunnus. Though, it's a good idea to do a run down of the example I gave you and see the results if you're still confused.

9. PHP Code:
``` // Loop through the array all neat... for (int i(0), j(1); i < ArrayLength && j < ArrayLength-i; ++i, ++j) { // Now, we created the temp variable for a very good reason. It stores the current place // in the array.     int temp = array[i]; // Then we let the current place and the second place swap.    array[i] = array[j]; // Then we set the second place to the value in the first place which is kept inside of temp.    array[j] = temp; }  ```

10. in question he says (The swap operation should be done on one array)

if i used

Code:
```for(int i = 0; i < N; i++)
i_new_array[i] = i_old_array[N-i]```
will it be correct

sorry guys i am a begginer

11. No, as Sly explained. Also, that example uses two arrays, so you're professor won't accept it anyway.

12. In regards to citizen's code:

It's generally a good idea to check to make sure your code works before posting it. You can't update both variables at the same time in a single for loop for a swap function to work. You need two nested for loops. Also, we try not to give away complete code to new users that post assignments.

13. You'll need to swap something, but what? In this particular example you'll need to swap:
0 <-> 9, 1 <-> 8, 2 <-> 7, 3 <-> 6, 4 <-> 5.
Can you see a pattern here? Can you do it in a loop? What would be the end condition of the loop? Can you write the swap function so that it would work on arrays of any size?

14. Originally Posted by SlyMaelstrom
In regards to citizen's code:

It's generally a good idea to check to make sure your code works before posting it. You can't update both variables at the same time in a single for loop for a swap function to work. You need two nested for loops. Also, we try not to give away complete code to new users that post assignments.
It wasn't complete anyway as he has to put the numbers backwards rather than just swap the numbers next to each other.

15. i solved it but the problem is that the first element of new array is always 0
Code:
```#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]) ;

}
}```