Write a program that
1. Contains a function that displays the content of an integer array starting from the upper bound on a single line.
2. Contains a function that swaps the first and last elements of an integer array. For
example, 1 4 9 16 25 would be transformed into 25 4 9 16 1.
3. Test the functions in (1) and (2) with the aid of an initialized array of your choice in the main function. Display the array before and after the shifting of the elements.
void Display(int *arr, int n)
{
int i;
printf("\n\tArray before modification: ");
for (i = 0; i < n; ++i) printf("%d, ",*(arr+i));
}
void SwapArrayEnds(int *arr, int n)
{
int x;
x = *(arr+n-1);
*(arr+n-1) = *(arr+0);
*(arr+0) = x;
}
int main()
{
int ArraySize;
int Array[] = {25, 4, 9, 16, 1};
int i;
int userNum;
ArraySize = sizeof(Array)/sizeof(Array[0]);
Display(&Array[0],ArraySize);
SwapArrayEnds(&Array[0], ArraySize);
printf("\n\tArray after modification : ");
for (i = 0; i < ArraySize; ++i)
{
printf("%d, ",Array[i]);
}
return 0;
}
Comments
Leave a comment