Write a C++ program that reads an array of 10 integers. The program should then
ID: 3572520 • Letter: W
Question
Write a C++ program that reads an array of 10 integers. The program should then cyclically shift the array n positions to the right (n is given by the user). This means each element of the array should be moved n positions to the right, while the last n elements are moved to the beginning of the array. For example: if the array contains the numbers [ 1 2 3 4 5 6 7 8 9 0 ] Shifting 2 positions to the right should give the array [ 9 0 1 2 3 4 5 6 7 8 ]. Problem 2:Write a C++ program that prompts the user to input 10 integer values. The program should display the minimum and maximum of those values. The program also should display the value that occurs the most and the number of occurrences. Hints: Sort the elements in the array to find the minimum and maximum values.To find the value that occurs the most, use a two-dimensional array to store the number of occurrences for each valuedu 8:33 AM elearning.sharjah.ac.ae Problem 1 Write a C++ program that reads an array of 10 integers. The program should then cyclical shift the arra n positions to the right (n is given by the user). This means each element of the array should be moved n positions to the right, while the last n elements are moved to the beginning of the array For example: if the array contains the numbers [12345 67890] Shifting 2 positions to the right should give the array 1901 2 3 4 5 6 78 Sample input/output Enter the number of vesitions by which the array execution tine 26-216 s "CAUsers 101512Desktop Teaching Coursesu4 Programming NHNNs HN3Q1 r of positions by which the array he shifted array 10 9 14 2 15 2 3G 1s Press any key to continue Problem 2: Write a C++ program that prompts the user to input 10 integer values. The program should display the minimum and maximum of those values. The program also should display the value that occurs the most and the number of occurrences. Hints: Sort the elements in the array to find the minimum and maximum values. To find the value that occurs the most, use a two-dimensional array to store the number of occurrences for each value. Sample input/output
Explanation / Answer
//Tested on Linux,Ubuntu
/*********************program1************/
#include <iostream>
using namespace std;
int const SIZE = 10;
//reversing array
int* reverse(int *number) {
//variabale declaration
int i,j=0;
//integer pointer array creation
int *temp=new int[SIZE];
//reversing code of an array
for(i=SIZE-1;i>=0;i--) {
temp[j]=number[i];
j++;
}
//returning array
return temp;
}
//
void leftRotateOneElement(int arr[])
{
//variable declaration
int i, temp;
temp = arr[0];
//left rotate by one value code
for (i = 0; i < SIZE-1; i++)
arr[i] = arr[i+1];
arr[i] = temp;
}
/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d)
{
//variable declaration
int i;
//left rotation logic
for (i = 0; i < d; i++)
leftRotateOneElement(arr);
}
int main() {
int numbers[SIZE],i,pos;
//inserting element into array
std::cout<<"Enter an array of "<<SIZE<<" integers: "<<std::endl;
for(i=0;i<SIZE;i++) {
cin>>numbers[i];
}
//created integer pointer for storing result
int *result=new int[SIZE];
//prompt for position value
cout<<"Please Enter the number of position by which array will be shifted";
cin>>pos;
//first we reversed the array
//second we did left rotation of array
//Again we reversed the array to get original state
result=reverse(numbers);
leftRotate(result,pos);
result=reverse(result);
//printing array after right shifted
cout<<"The Shifted array is: ";
for(i=0;i<SIZE;i++) {
cout<<result[i]<<" ";
}
cout<<endl;
return 0;
}
/***************program1 output*****************/
lalchand@lalchand:~/Desktop/chegg$ g++ arrayOperation1.cpp
lalchand@lalchand:~/Desktop/chegg$ ./a.out
Enter an array of 10 integers:
1 2 3 4 5 6 7 8 9 0
Please Enter the number of position by which array will be shifted2
The Shifted array is: 9 0 1 2 3 4 5 6 7 8
/**********************program2***************/
#include <iostream>
#include <algorithm>
using namespace std;
int const SIZE = 10;
int main() {
int numbers[SIZE],i,maxOccurence,cnt=0;
//inserting element into array
std::cout<<"Enter an array of "<<SIZE<<" integers: "<<std::endl;
for(i=0;i<SIZE;i++) {
cout<<"Value "<<i<<":";
cin>>numbers[i];
}
//Now we call the sort function
sort(numbers, numbers + SIZE);
//counting occurence logic
for(i=0;i<SIZE-1;i++) {
//count function for counting occurence
int mycount = std::count (numbers, numbers+SIZE, numbers[i]);
if(mycount>cnt) {
cnt=mycount;
maxOccurence=numbers[i];
}
}
cout<<"The Maximum Value- "<<numbers[SIZE-1]<<endl;
cout<<"The Minimum Value- "<<numbers[0]<<endl;
cout<<"The most occurring value is "<<maxOccurence<<endl;
cout<<"It occures "<<cnt<<" times."<<endl;
return 0;
}
/*******************program2 output*******************/
lalchand@lalchand:~/Desktop/chegg$ g++ maxAndMin.cpp
lalchand@lalchand:~/Desktop/chegg$ ./a.out
Enter an array of 10 integers:
Value 0:3
Value 1:10
Value 2:8
Value 3:17
Value 4:25
Value 5:3
Value 6:14
Value 7:8
Value 8:6
Value 9:8
The Maximum Value- 25
The Minimum Value- 3
The most occurring value is 8
It occures 3 times.
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.