Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to enter a series of one-digit non-negative n

ID: 3547669 • Letter: W

Question

Write a program that asks the user to enter a series of one-digit non-negative numbers (you will need to do error checking).

When the user has finished entering number (Note: the user should indicate that they are finished by entering the value 10), print out how many of each number the user entered.

There must be three functions including main.

The main function will read in the values from the user and do validity checking on the input. If the number is in the range 0 to 9 main will call a second function that will count the number.

After all of the numbers have been entered the main function will call a third function to display the results.

You must not use global variables.

Hint: Use an array to hold an accumulator (counter) for each digit 0 through 9.

Note: Do not output a value of a digit if no digits with that value were entered by application user. Note that in the below output the

Explanation / Answer

# include<conio.h>

#include<iostream>

using namespace std;


void CountNum(int num, int arr[])

{

switch(num) {

case 0: arr[0]++; break;

case 1: arr[1]++; break;

case 2: arr[2]++; break;

case 3: arr[3]++; break;

case 4: arr[4]++; break;

case 5: arr[5]++; break;

case 6: arr[6]++; break;

case 7: arr[7]++; break;

case 8: arr[8]++; break;

case 9: arr[9]++; break;

}


}


void DisplayNum(int arr[])

{

for(int i=0; i<10;i++)

{

if (arr[i] >0)

{

cout<<"You entered "<<arr[i]<<" ,"<<i<<"(s)"<<endl;

}

}

}

int main()

{

int n;

int StoreNum[10] = {0,0,0,0,0,0,0,0,0};

int flag=1;


while ( flag)

{

cout<<"Enter a one-digit number or 10 to exit: "<<endl;

cin>>n;

if (n<0 || n>10)

{

cout<<"The value "<<n<<" is not valid."<<endl;

}

else if (n ==10)

{

flag=0;

}

else

{

CountNum(n,StoreNum);

}

}


if (!flag)

{

DisplayNum(StoreNum);

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote