in C++ language (microsoft visual C++ 2010 express) create a program that will a
ID: 665775 • Letter: I
Question
in C++ language (microsoft visual C++ 2010 express)
create a program that will allow the user to enter a up to fifty (50) whole numbers, store these whole numbers in an array, count how many numbers the user entered, output how many whole numbers were entered, and output the numbers in reverse order with 3 numbers appearing per line.
Ihe program should be written assuming the user will enter at least one number. The user should be asked if he/she wants to enter another number or not. You do not have to test that the user entered a whole number, but your program should not allow the user to enter more than 50 numbers and should not be asked if he/she wants to enter another once the array is full. Your program should use appropriate loops and the user should be able to enter either an upper or lower case “Y” to continue, anything other than upper or lower case “y” should stop the loop
you may not use break or continue in a loop (doing so will result in major deductions. Also the loop condition should reflect all the methods the stop the loop.
These are some sample runs
Run 1 :
Enter a whole number. 567
Would you like to enter another number (y/n)? y
Enter a whole number. 34
Would you like to enter another number (y/n)?
Y Enter a whole number. 234
Would you like to enter another number (y/n)? Y
Enter a whole number. 2856
Would you like to enter another number (y/n)? y
Enter a whole number. 73
Would you like to enter another number (y/n)? y
Enter a whole number. 74
Would you like to enter another number (y/n)? y
Enter a whole number. 938 Would you like to enter another number (y/n)? n
7 numbers were entered. The numbers are given below in reverse order 938 74 73 2856 234 34 567
Run 2 :
Enter a whole number. 56
Would you like to enter another number (y/n)? n
1 numbers were entered. The numbers are given below in reverse order 56
Run 3 :
Enter a whole number. 84 Would you like to enter another number (y/n)? y
Enter a whole number. 567
Would you like to enter another number (y/n)? y
Enter a whole number. 983
Would you like to enter another number (y/n)? y
Enter a whole number. 3746
Would you like to enter another number (y/n)? y
Enter a whole number. 25
Would you like to enter another number (y/n)? Y
Enter a whole number. 453
Would you like to enter another number (y/n)? Y
Enter a whole number. 748
Would you like to enter another number (y/n)? y
Enter a whole number. 384
Would you like to enter another number (y/n)? Y
Enter a whole number. 4657
Would you like to enter another number (y/n)? y
Enter a whole number. 44
Would you like to enter another number (y/n)? y
Enter a whole number. 234
Would you like to enter another number (y/n)? y
Enter a whole number. 545
Would you like to enter another number (y/n)? n
12 numbers were entered. The numbers are given below in reverse order
545 234 44
4657 384 748
453 25 3746
983 567 84
Explanation / Answer
#include <iostream>
//#include <iostream.h>
//#include <conio.h>
//#include <stdlib.h>
// #include <string.h>
//#include <ctype.h>
using namespace std;
int main()
{
char CheggResponse = 'y'; // variable to store user response of continue or stop
int i=0, SivaNumb, SivaArray[50]; // variables for whole numbers and array storage
int SivaTotNumbs = 1;
cout << " Please enter a whole number" ; cin >> SivaNumb;
SivaArray[i] = SivaNumb; // get the first number from user and put it in the array
// start the for loop, let it run from 1 to 49
for ( i = 1; i<=49 && (CheggResponse == 'y' || CheggResponse == 'Y') ; i++)
{
// ask whether user wants to continue or to quit
cout << endl << "Would you like enter another number (y/n)?";
cin >> CheggResponse;
CheggResponse = tolower(CheggResponse); // convert the user response to lower case hence both Y and y ok
if (CheggResponse == 'y') { // if user had said y for yes
cout << " Please enter a whole number" ; cin >> SivaNumb; // read in the next number from STDIN
SivaArray[i] = SivaNumb; // receive the number again and put it in to the array
} // end of if
} // end for loop
SivaTotNumbs = i -1 ; // count of how many numbers user had entered
// wondering why I have included both lower case and upper case in the
// for loop like this: (response == 'y' || response == 'Y') ?
// The reason is we are getting cin >> response
// in one line and then we are doing response = tolower(response) in the next line
// with in that gap of few micro seconds between these two statement lines, the
// loop can terminate in case if the user had entered Y (upper case Y)
// but as I have taken care of it by including (response == 'y' || response == 'Y') in the
// for loop, all is well now and the program works ok logically
//cout << " response = " << response ;
// now the array is filled with user entered data
// all we have to do is to simply pint in reverse order with 3 numbers per line
// hence we will introduce a variable to count up to three
int SivaCountToThree = 1;
cout << endl << SivaTotNumbs << " numbers were entered ";
cout << endl << "The numbers are given below in reverse order; " << endl;
for (i = SivaTotNumbs; i >= 0; i--)
{
cout << " " << SivaArray[i] << " "; SivaCountToThree++;
if ( SivaCountToThree == 4 )
{ cout << endl ;
SivaCountToThree = 1; // add a new line once in every three numbers
} // end if
} // end for loop
return 0;
}
As usual, when I pasted the output screen, it said Answer cannot be longer than 65,000 characters. Hence I had to type the answers from a sample run below:
Please enter a whole number 1
Would you like to enter another number (y/n)? y
Please enter a whole number 2
Would you like to enter another number (y/n)? y
Please enter a whole number 3
Would you like to enter another number (y/n)? y
Please enter a whole number 4
Would you like to enter another number (y/n)? y
Please enter a whole number 5
Would you like to enter another number (y/n)? y
Please enter a whole number 6
Would you like to enter another number (y/n)? y
Please enter a whole number 7
Would you like to enter another number (y/n)? y
Please enter a whole number 7
Would you like to enter another number (y/n)? y
Please enter a whole number 9
Would you like to enter another number (y/n)? y
Please enter a whole number 10
Would you like to enter another number (y/n)? y
Please enter a whole number 11
Would you like to enter another number (y/n)? y
etc up to 23 numbers
23 numbers were entered
The numbers are given below in reverse order:
657 789 5
67 324 3247
324 32 234
15 14 13
12 11 10
9 7 7
6 5 4
3 2 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.