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 shits all elements of an integer array by one to the right and
move the last element into the first position. For example, 1 4 9 16 25 would be
transformed into 25 1 4 9 16.
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.
#include <stdio.h>
#define N 5
void DisplayArrayReverse(int arr[], int size)
{
for (int i=size-1; i>=0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}
void ShiftArrayRight(int arr[], int size)
{
int last = arr[size-1];
for (int i=size-1; i>0; i--) {
arr[i] = arr[i-1];
}
arr[0] = last;
}
int main() {
int arr[N] = {1, 4, 9, 16, 25};
printf("Inital array in reverse order: ");
DisplayArrayReverse(arr, N);
ShiftArrayRight(arr, N);
printf("Shfifted array in reverse order: ");
DisplayArrayReverse(arr, N);
return 0;
}
Comments
Leave a comment