Exercise #2: Shifting Array Elements Write a program that uses an array of size
ID: 3702843 • Letter: E
Question
Exercise #2: Shifting Array Elements Write a program that uses an array of size eight (8) and then prompts the user to input eight integers for the array. The program should do the following 1. Print the numbers in the output given in the sample Input/Output 2 The user is prompted to enter an integer value k the program will then shift the array elements kpositions to the right, while the last Kelements are moved to the beginning of the array For example: if we have an array [1 23 4567 81 shifting 2 positions to the right should give the array [7 8123456] Sample Input/Output ns entered nunbers arei 1 2 3 S 7 8 1 values for an Array Enter the nunber of shifts: 3 Exercise #3: Strings' operations Write a C+ program that prompts the user to enter a sentence, the program then does the following 1. Prints the total characters in the sentence. 2. Prints the word count of the sentence, assuming only one space between each tow words Prints total vowel (a, e. i o, u) in the sentence 4. Prints the vowel frequency as shown in the sample below 5. Prints the sentence in reverse. 6. Prints the total number of characters other than the alphabets (Note: space is a character) 7. Prints the sentence in a way so that the case of each character is changed Le. from lower to upper and vice versa Sample input/outpu Enter eft niketo play ootball. o. ado a strins s to play foot Character Count 49 ord Count:9 Uove1 Count :13 Jowe1 Prequeney fleg dna innet llabtoof yalp ot sekil A .D Count 12 hange Case R aLI LIKES TO PLAY FOOTBALL TIS. RND COLFExplanation / Answer
/* Both answer one by one included comment for understaing if doubt comment please*/
#include <iostream>
using namespace std;
void shiftOnePlace(int arr[], int size)
{
int temp;
int i;
temp = arr[size - 1];
for(i = size - 1; i > 0; i--){
arr[i] = arr[i-1];
}
arr[0] = temp;
}
int main ()
{
int array[8];
cout<<"Enter integer values for array of size 8 ";
for(int i = 0; i < 8;i++){
cin>>array[i];
}
cout<<"Your entered values are : [ ";
for(int i = 0; i < 8;i++){
cout<<array[i]<<" ";
}
cout<<"] ";
int shiftTimes;
cout<<"Enter the number of shifts : ";
cin>>shiftTimes;
/*end of input output*/
/* shift one by one till shift times*/
for(int i = 0; i < shiftTimes;i++){
shiftOnePlace(array, 8);
}
/* output */
cout<<" [ ";
for(int i = 0; i < 8;i++){
cout<<array[i]<<" ";
}
cout<<"] ";
}
#include <iostream>
using namespace std;
int main()
{
string line;
int wordCount, charCount, vowelsCount, nonAlphaCount;
wordCount = charCount = vowelsCount = nonAlphaCount = 0;
string reverse;
string changeCase;
int aCount, eCount, iCount, oCount, uCount;
aCount = eCount = iCount = oCount = uCount = 0;
cout << "Enter a string: ";
getline(cin, line);
/* make everting equal to line for later changing*/
changeCase = reverse = line;
int strLength = line.length();
/* use full for iterating in reverse order*/
int reverseIndex = strLength - 1;
/*counting and other operation*/
for(int i = 0; i < strLength; ++i)
{
if( line[i] == ' '){
++wordCount;
}
if(! isalpha(line[i])){
++nonAlphaCount;
}
if(line[i]=='a' || line[i]=='A' ){
++vowelsCount;
++aCount;
}else if (line[i]=='e' || line[i]=='E'){
++vowelsCount;
++eCount;
}else if(line[i]=='i' || line[i]=='I'){
++vowelsCount;
++iCount;
}else if(line[i]=='o' || line[i]=='O'){
++vowelsCount;
++oCount;
}else if(line[i]=='u' || line[i]=='U'){
++vowelsCount;
++uCount;
}
/* changing case of letter*/
if ( isupper(line[i]) ){
changeCase[i] = tolower(line[i]);
}
if ( islower(line[i]) ){
changeCase[i] = toupper(line[i]);
}
/*reversing the string here reverseIndex is the current index
in reverse order */
reverse[reverseIndex--] = line[i];
}
cout<<"Character Count : "<<strLength<<endl;
cout<<"Word Count : "<<wordCount<<endl;
cout<<"Vowels Count : "<<vowelsCount<<endl;
/* showing the vowels in certain way*/
cout<<"Wovels frequency "<<endl;
cout<<"a e i o u"<<endl;
cout<<aCount<<endl;
cout<<" "<<eCount<<endl;
cout<<" "<<iCount<<endl;
cout<<" "<<oCount<<endl;
cout<<" "<<uCount<<endl;
cout<<"Revesres : "<<reverse<<endl;
cout<<"Non Alphabhet Count : "<<nonAlphaCount<<endl;
cout<<"Change Case String is "<<changeCase<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.