Swapping of arrays
In this article, let’s code for the arrays swap program.
Exercise
Swapping of 2 arrays:
Write a function that accepts 2 arrays and swaps them out print the updated arrays after swapping.
Your program should input the array elements from the user.
Solution:
#include<stdio.h> #include<stdint.h> void wait_for_user_input(void); int main(void) { int32_t nItem1, nItem2; printf("Array swapping program\n"); printf("Enter no of elements of Array-1 and Array-2:"); scanf("%d %d",&nItem1,&nItem2); if((nItem1 < 0) || (nItem2 < 0)) { printf("Number of elements cannot be negative\n"); wait_for_user_input(); return 0; } int32_t array1[nItem1]; int32_t array2[nItem2]; for(uint32_t i = 0; i < nItem1 ; i++ ) { printf("Enter %d element of array1:",i); scanf("%d",&array1[i]); } for(uint32_t i = 0; i < nItem2 ; i++ ) { printf("Enter %d element of array2:",i); scanf("%d",&array2[i]); } printf("Contents of arrays before swap \n"); display_array(array1,nItem1); printf("\n"); display_array(array2,nItem2); printf("\n"); return 0; } void display_array(int32_t *pArray1,uint32_t nitem) { for(uint32_t i = 0 ; i < nitem ; i++) { printf("%4d ",pArray1[i]); } } void swap_arrays(int32_t *array1, int32_t *array2, uint32_t nitem1, uint32_t nitem2) { }
First, I create two variables nItem1 and nItem2 of type int32_t.
After that, add a printf statement here.
printf(“Array swapping program\n”);
Then let’s ask the user to enter the number of elements of Array-1 and Array-2.
And then let’s scan for the user data, so use “%d %d” and we read two numbers using one scanf. Let’s read that into these variables, so I would give nItem1 and nItem2.
After that, now let’s compare those numbers. Those numbers should not be less than zero.
If ((nItem1<0) || (nItem2<0))
Then you have to print “number of elements cannot be negative”.
So, then you have to wait for the user input, and then you have to return. That’s it. You can’t continue.
Otherwise, you have to create the arrays now.
int32_t array1[nItem1];
int32_t array2[nItem2];
Please note that this variable length array you can only use after the C99 standard onwards.
Now let’s ask the user to enter the elements for these two arrays.
First, let’s do it for array-1. Let’s use a for loop. So, for loop is the most favored looping technique which is used with arrays.
Now let’s create an iterator variable uint32 i=0; i<nItem1, i++.
Let’s ask the user to enter the %d element of array-1. So, we have to print “Enter %d element of array-1:” and the argument for this format specifier is ‘i’ (line 28).
And after that, you have to use scanf.
scanf(“%d”, &array[i]);
You have to store that into an array, &array1[i]. And remember that scanf takes the address of the variable. That’s why you have to give ampersand(&).
Now let’s again repeat this code for array-2
For that, I used the display_array function. And for this display array function, pass the base address of the array and the number of items in the array.
void display_array(int32_t *pArray1, uint32_t nitem)
Let me call it pArray1 because this has to be a pointer variable. After all, you are passing a pointer. And here let’s create one more variable to receive the length or number of items. I would call it a nitem.
Let’s create a for loop because we have to print the nitems.
for(uint32_t i=0; i<nitem; i++)
Then let’s print that in one row → printf(“%4d ”, pArray1[i]);
We can use the width specifiers ‘%4d’, so the output looks nice. I would use \t or you can give 2 white spaces after %4d.
Instead of using pArray1[i] you can also use *(pArray+i). So, both are identical.
And after that let’s give the prototype of the display_array function.
I call the display_array function to display array1 and array2. And let’s give a new line(\n) between two arrays, as shown in above code snippet.
Output:
First, we enter a negative number. So, I enter -9 9. It prints “Number of elements cannot be negative”, as shown in Figure 1. That’s correct.
Enter the number of elements of Array-1 and Array-2 I add 3 and 4.
Enter 0, 1, and 2 elements of array1 I add 5, 6, 7. Enter 0, 1, 2, 3 elements of array2 I add -999, 87, 67, 67. So, it displays the contents of the array before the swap, as shown in Figure 2.
After that, let’s write one more function to swap the arrays. So, let me call it swap_arrays.
What are the arguments you have to take the base addresses of two arrays?
void swap_arrays(int32_t *array1, int32_t *array2, uint32_t nItem1, uint32_t nItem2) //(as shown in above)
This should be your function and I implement this function in the following article.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1