Question

In: Computer Science

A. Write a C++ with a menu (using switch) that asks the user to select one...

A. Write a C++ with a menu (using switch) that asks the user to select one of the following choices to the user:

1. Options ‘S’ or ‘s’

2. Option ‘T’ or ‘t

3. Options ‘P’ or ‘p’

4. print in the Matrix format

B. When 1 is selected, prompt the user to enter the starting value st (int value). Use a single FOR loop to count numbers from 1 to st. When the loop is finished, find the average of those numbers.

C. When 2 is selected, prompt the user for a number num (int). Use a single WHILE loop to calculate the sum of the odd numbers between 1 and num.

D. When 3 is selected. Use a single DO-WHILE loop to promt the user to enter random (int) values and stop whenver the first negative value is entered. Then tell the user how many positive value has been entered.

E. When 4 is selected. Use NESTED FOR loops to output all the even numbers from 1 to 80 in 5 rows and 8 columns.

NB. If the user does not select one of these 4 options print " invalid selection".

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;
//option function
void option()
{
   //prompt message for the user
   cout<<"================================="<<endl;
   cout<<"1. Options 'S' or 's' "<<endl;
   cout<<"2. Options 'T' or 't' "<<endl;
   cout<<"3. Options 'P' or 'p' "<<endl;
   cout<<"4. Print in the Matrix format"<<endl;
   cout<<"================================="<<endl;
}
//main function
int main()
{
   //cjoice variable
   char choice;
   //call the option function
   option();
   //take input from the user
   cin>>choice;
   //switch statement
   switch(choice)
   {
       //if the case is 'S'. 's' or 1
       case 'S':
       case 's':
       case '1':
       {
           //initialize the st varibale and count varibale
           int st,count=0;
           //prompt to user
           cout<<"Enter the starting value : ";
           //take input from the user
           cin>>st;
           //for loop till st from 1
           for(int i=1;i<=st;i++)
               //increment 1 in count
               count++;
           //average of the first n natural number
           float average = float(st+1)/2;
           //output for the user
           cout<<"Average = "<<average<<endl;
           //then break
           break;
       }
       //if the case is 'T' ,'t' or 2
          case 'T':
       case 't':
       case '2':
       {
           //initialse the varibal num and sum_odd
           int num,sum_odd = 0;
           //message for the user
           cout<<"Enter a number : ";
           //take input from the user
           cin>>num;
//           while loop till num is not 0
           while(num>0)
           {
               //if number is odd
               if(num%2==1)
               //then add to sum_odd varaible
               sum_odd = sum_odd+num;
               //decrement in num
               num--;
           }
           //output for the user
           cout<<"Sum of odd "<<sum_odd<<endl;
           break;
       }
       //if the case is 'P' ,'p' or 3
       case 'P':
       case 'p':
       case '3':
       {   //initialize the variable
           int random;
           //count as 0
            int   count = 0;
            //do while loop
           do
           {
               //prompt for the user
               cout<<"Enter random number : ";
               //take input from the user
               cin>>random;
               //if number is positive then increment 1 in count
               if(random>0)
                   count++;
           }
           //terminating condition
           while(random>0);
           //output for the user
           cout<<"Total positive number "<<count<<endl;
           break;
       }
       //for case 4
       case '4':
       {
           //initial k
           int k = 2;
           //for all row
           for(int i=1;i<=5;i++)
           {
               //for all column
               for(int j=1;j<=8;j++)
               {
                   //output for user
                   cout<<setw(3)<<k;
                   //move to next even
                   k = k+2;
               }
               cout<<endl;
           }
           break;
       }
       //default case
       default:
           //message for the user
           cout<<"Invalid selection";
   }
}


If you have any problem then let me know in comment box and if like then please rate or give a big thumbs up thank you.


Related Solutions

USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Using C language: Write a program that asks the user for the size of an array,...
Using C language: Write a program that asks the user for the size of an array, then reads a number of integer values (from user input) into the array. Write a function to print out the array and call it to print the array out after all values are read in. Write a function to implement Insertion Sort and run it on the data array to sort the array. Write another function to implement Selection Sort and run it on...
1- Write a c++ program that asks the user to enter his/her name using one variable...
1- Write a c++ program that asks the user to enter his/her name using one variable only. The name must be then printed out in capital letter with the family name first and the last name second.
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
Write a C# program using repl.it that asks the user for three integers and prints the...
Write a C# program using repl.it that asks the user for three integers and prints the average value as a double. Example calculation (3 + 6 + 7) / 3 = 5.33333333333333 Given the data above, the output should be similar to: The average for the values entered is: 5.33333333333333 As always comments in your code are expected (what why how etc… ) Submit your repl.it link to the assignment dropbox text submission area.
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
Write a C# program using Repl.it that asks the user for a numeric grade between 0...
Write a C# program using Repl.it that asks the user for a numeric grade between 0 and 100, and displays a letter grade using the following conversion table: Numeric grade   Letter grade 90 < grade <= 100 A 80 < grade <= 90 B 70 < grade <= 80 C 60< grade <= 70 D grade <= 0.0 F Don’t forget the comments in your code. The output should be similar to: Enter the test score: 87 The letter grade...
Write a C ++ program that asks the user for the speed of a vehicle (in...
Write a C ++ program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------- 1           40 2           80...
Write a c++ program that asks the user to choose his preferred shape, and asks then...
Write a c++ program that asks the user to choose his preferred shape, and asks then for two numbers (positive but not necessary integers) in order to calculate the shape's area. The possible shapes are: Triangle, Ring and Rectangle. If the user chose a different shape, you will warn him and wait for a valid shape. The area of a circle is πr12-r22 (suppose π= 3.4 ). The area of a triangle is base*height/2. The area of a rectangle is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT