Answer to Question #263894 in C for Sam

Question #263894

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.

1
Expert's answer
2021-11-10T06:34:19-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS