swap havles of an array ( C Language) Please need these requirements in the code
ID: 3690887 • Letter: S
Question
swap havles of an array ( C Language)
Please need these requirements in the code ( you could use pointers) :
1-Make sure to implement function mirror() following the below signature. You are not allowed to use array notation, i.e. the symbols '[' and ']' are banned within mirror().
2-Your main() will make use of the mirror() function, but will pass in the right index ranges, which have been determined computationally. The input array can be hard-coded.
3-If the array has an odd number of elements, we want to assume that the second (not quite) half of the array contains an odd number of elements. For example, if you were supposed to swap this array: [1,2,3,4,5], the result would be [3,4,5,1,2].
4- Make sure to thoroughly comment and properly indent your code.
Here is the function to code :
Problem Imagine that you have an array of integer values. Your goal is to swap the two halves of the array. The approach is to first "morror" the two halves separately and then mirror the whole array. Mirror means to reverse the elements in an array or partial array, so 1 2 34563 would yield 2 half arrarys (3 2 1 and (654] you then mirror 3 2 16 5 4) to get (4 56 1 2 3). I ask you to follow this approach and implement a function, which is doing the mirroring part with the following prototype void mirror(int array, int from index, int to index where the two indices define the part of the array which is supposed to be mirrored, in C notation (i.e. the first element of the array is at index zero).Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void mirror(int *a,int from_index,int to_index)
{
int i=0,j=0,k=0,m=0,l=0,n=0,o=0; //initialization of the variables..
//dynamic declaration of the three arrays...this way we dont have to use the array notation for accessing the elements.
int *arr1=(int *)malloc(sizeof(int));
int *arr2=(int *)malloc(sizeof(int));
int *arr3=(int *)malloc(sizeof(int));
//following two for loops are for reversing the each half part of the main array
for(i=(to_index/2)-1;i>=0;i--)
{
*(arr1+k)=*(a+i);
k++;
}
for(j=to_index;j>=(to_index/2)-1;j--)
{
*(arr2+m)=*(a+j);
m++;
}
//following two for loops are use for storing the half array reveresly into the third array..
for(l=m-2;l>0;l--)
{
*(arr3+n)=*(arr2+l);
n++;
}
for(l=k-1;l>=0;l--)
{
*(arr3+n)=*(arr1+l);
n++;
}
printf(" ");
//print the final array
for(l=0;l<n;l++)
{
printf(" %d",*(arr3+l));
}
}
//main function
int main()
{
//take the static array
int a[]={1,2,3,4,5};
//call the mirror function
//here first argument is the name of the array
//second argument is the starting index of the array which is 0 in this case
//third argument is the last index of the array...which we get using the following formula
mirror(a,0,sizeof(a)/sizeof(a[0]));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.