Write a C program that rearranges an integer array. The array will be split into
ID: 3755887 • Letter: W
Question
Write a C program that rearranges an integer array. The array will be split into two sets of integers one by one. A new array will be created by append the first set to the second set. For example, an integer array {1, 2, 3, 4, 5, 6}will be split into two sets of integers: 1, 3, 5and 2, 4, 6. Merging the two sets of integers by appending the first set to the second set creates the new array {2, 4, 6, 1, 3, 5}.
Note that the two sets do not necessarily have the same length (when the array has even number of elements). When the input word has odd number of elements the first set will be one element more than the second set. For example, the result of rearranging an integer array {1, 2, 3, 4, 5} will be {2, 4, 1, 3, 5}.
Your program should include the following function:
void rearrange(int *a1, int n, int *a2);
The function should use pointer arithmetic –not subscripting –to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator inthe function.
The rearrange function merges the two sets of integers by appending the first set to the second set and store the result in the second array a2. The function takes an int array parameters a1 and 2 as the first parameter and third parameters and the number of elements of the arrays as the second parameter. Example Input/Output:
Enter the length of the array: 5
Enter the elements of the array: 9 8 5 6 4
Output: 8 6 9 5 4
Explanation / Answer
#include <stdio.h> /* printf */
#include <stdlib.h>
//method to merge left and right arrays
void rearrange(int *a1, int n, int *a2)
{
int r = n/2;
int i=0;
//adding to a2
while(r<n)
{
*(a2+r) = (*(a1+i));
i++;
r++;
}
}
//method to print array
void print(int *a, int n)
{
printf("Output:");
int i=0;
while(i<n)
{
printf("%d ",*(a+i));
i++;
}
printf(" ");
}
int main ()
{
//variable declaration
int n;
//reading input
printf("Enter the length of the array: ");
scanf("%d",&n);///reading number
int l,r;//to store left size and right size
if(n%2==0)//if n is odd
{
l=r=n/2;
}
else
{
l = n/2 +1;
r=n/2;
}
//declaring arrays
int *a1= (int *)malloc(sizeof(int)*l);//left array
int *a2= (int *)malloc(sizeof(int)*n);//right array
//reading array elements
printf("Enter the elements of the array:");
int i=0,j=0,c=0;
while(c<n)
{
if(c%2==0)
{
scanf("%d",(a1+i));//reading to left array
i++;
}
else
{ scanf("%d",(a2+j));//reading to right array
j++;
}
c++;
}
rearrange(a1,n,a2);
print(a2,n);
}
//PLS give a thumbs up if you find this helpful, its helps alot, thanks
/*
output1:
Enter the length of the array: 5
Enter the elements of the array:9 8 5 6 4
Output:8 6 9 5 4
Process exited normally.
Press any key to continue . . .
output2:
Enter the length of the array: 4
Enter the elements of the array:1 2 3 4
Output:2 4 1 3
Process exited normally.
Press any key to continue . . .
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.