This exercise is similar to Exercise 1 Part C, except now you will rotate the el
ID: 3813953 • Letter: T
Question
This exercise is similar to Exercise 1 Part C, except now you will rotate the elements of array A in the other direction. Once again, assume the numbers are stored in a 1-D array named A that can contain up to N elements. Your program needs to shift each element in the array one position to the right. The original right-most element should be inserted into the first array location. Assume the array is indexed from 1 to N, and that the array has already been initialized with some values. Example for N = 5: original array A (before rotation): Processed array A (after rotating to the right):Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
//Function to rotate array
void rotateArray(int A[])
{
int x, temp;
//Last index position of the array is stored in temp
temp = A[MAX - 1];
//Loops from last to first
for(x = MAX - 1; x > 0; x--)
{
//Shifts one position right
A[x] = A[x - 1];
}//End of loop
//Stores the temp data in first position
A[0] = temp;
}//End of function
//To display Array contents
void displayArray(int A[])
{
int x;
printf(" Index: ");
for(x = 0; x < MAX; x++)
{
printf("%3d ", (x + 1));
}
printf(" Values: ");
for(x = 0; x < MAX; x++)
{
printf("| %d | ", A[x]);
}
printf(" ");
}//End of function
//To display menu and return choice entered by the user
int menu()
{
int ch;
printf(" 1 To Display");
printf(" 2 To Rotate Right ");
printf(" 3 To Exit ");
printf(" Enter your choice: ");
//Accepts choice
scanf("%d", &ch);
//Return choice
return ch;
}//End of function
//Main function
int main()
{
//Creates an array
int A[MAX] = {4, 7, 3, 0, 8};
printf(" Original Array ");
displayArray(A);
//Loops till user choice
do
{
switch(menu())
{
case 1:
printf(" Processed Array (After rotating to the right: ) ");
displayArray(A);
break;
case 2:
rotateArray(A);
break;
case 3:
exit(0);
default:
printf(" Invalid Choice ");
}//End of switch
}while(1);//End of while
}//End of main
Output:
Original Array
Index: 1 2 3 4 5
Values: | 4 | | 7 | | 3 | | 0 | | 8 |
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 2
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 1
Processed Array (After rotating to the right: )
Index: 1 2 3 4 5
Values: | 8 | | 4 | | 7 | | 3 | | 0 |
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 2
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 1
Processed Array (After rotating to the right: )
Index: 1 2 3 4 5
Values: | 0 | | 8 | | 4 | | 7 | | 3 |
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 2
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 1
Processed Array (After rotating to the right: )
Index: 1 2 3 4 5
Values: | 3 | | 0 | | 8 | | 4 | | 7 |
1 To Display
2 To Rotate Right
3 To Exit
Enter your choice: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.