Description

Programs to upload:  p7.cpp, p8.cpp, p9.cpp, p10.cpp, p11.cpp, p12.cpp

Once each program is working, copy/paste the program test run (output) at the bottom of your code as a multiline comment, as shown in the p1 program! 

To complete this weeks assignments, you will need to watch the lecture video before you get started. Type up the example programs that I show. I show hints for the programs bellow toward the end of the lecture video.

Program 7:

Type up the below Sample Program (including all comments!).

(Visually Impaired Students: please refer to p7.cpp  sample program)

//  Your name:
//  Program Name: p7.cpp
//  Date:  
//  Description: Selection sample program
#include
using namespace std;
int main()
{
   int Age; //DECLARING
   cout << "Please enter age: ";// PROMPT
   cin >> Age; // USER ENTERS INTEGER AGE VALUE

   // IF, ELSE IF CHAIN
   if (Age < 0)
   {   // an IF gets checked ALWAYS to see if condition is TRUE/FALSE
       // if condition is TRUE, cout reads "Age is LESS than zero, invalid age"
       cout << "Age is LESS than";     // 1st statement
       cout << " zero, invalid age ";   // 2nd statement
   }

   else if (Age < 18)  //ELSE IF get check ONLY when previous IF is FALSE
   {    //if TRUE, reads "Age is LESS than 18"
       cout << "Age is LESS than 18";
   }

   else if (Age == 20) //gets check only when the PREVIOUS ELSE IF is FALSE
   //{  curly brackets are optional if there is only 1 statement
       cout << "Age is EQUAL to 20 ";   // One statement only
   //}

   else if (Age != 21) // gets checked only when previous ELSE IF is FALSE
       cout << "Age is NOT EQUAL to 21 ";

   else    //happens automatically when previous ELSE IF is FALSE
       cout << "Age must be (not < 0),(not < 18), not == 20) and (not !=21)";

   //OR (||): One of the conditions must be true for IF to happen
   // if (condition 1) || (condition 2)
   if ( (Age < 0) || (Age > 123))
       cout << "Age is either less than 0 or greater than 123";

   // AND (&&): BOTH of the conditions must be TRUE for the IF to happen
   // if (condition 1) || (condition 2)
   if ( (Age > 18)&& (Age < 21) )
       cout << "Age is BOTH (greater than 18) AND (less than 21)" <<endl;

   //NESTED IF's, an IF inside another IF
   if ( Age > 18)
       if ( Age < 21)
       {
           cout << "Age is BOTH (greater than 18)";
           cout << " AND (less than 21)" << endl;
       }

   return 0;   // this is where the program ends
}
/* === OUTPUT ===

Please enter age: -10
Age is LESS than zero, invalid age Age is either less than 0 or greater than 123
Please enter age: 165
Age is NOT EQUAL to 21 Age is either less than 0 or greater than 123

Please enter age: 15
Age is LESS than 18

Please enter age: 20
Age is EQUAL to 20 Age is BOTH (greater than 18) AND (less than 21)
Age is BOTH (greater than 18) AND (less than 21)

Please enter age: 21
Age must be (not < 0),(not < 18), not == 20) and (not !=21)
*/

Program 8:

Write the following program to compute a person’s height and print out a message. The user will input feet and inches. The program will convert that to inches, then print a message, based on the total inches. Be sure to have your program output match mine!

If the total inches is greater than 72, the message should be something like, “You’re tall.” If the total inches is between 5′ and 6′, a second message should appear. If the total inches is less than 60, a third message should appear.

===  Sample Program Run  === :

Please enter feet: 6
Please enter inches: 1
Your height is 6 feet + 1 inches = 73 inches
You are tall!

OR

Please enter feet: 4
Please enter inches: 2
Your height is 4 feet + 2 inches = 50 inches
You are vertically challenged!

OR

Please enter feet: 5
Please enter inches: 4
Your height is 5 feet + 4 inches = 64 inches
You are in between 5' and 6'.

Program 9:

Write a program which asks the user for a student’s grade as a percent, and then returns their letter grade.

A is 90% or above

B is between 80% and 90%

C is between 70% and 80%

D is between 60% and 70%

F is under 60%

Be sure to Test you program for all cases (A,B,C,D,F)!!!

Sample Run:

Please enter a grade in percent: 75

You have a “C”

====

Please enter a grade in percent: 85

You have a “B”

====

Please enter a grade in percent: 95

You have a “A”

====

Please enter a grade in percent: 65

You have a “D”

====

Please enter a grade in percent: 55

You have a “F”

Save and submit it as p9.cpp

Program 10:

Write a program which asks the user to enter the width and height of a rectangle, and then calculates and shows the area and perimeter of the shape rounded to 1 decimal. Validate user input, make sure that an error message shows if either width or height are <= 0

HINTs: To write this program think first about the steps that your program will have to go through, the “algorithm”

Algorithm ( include this in your comments at the top of the program):

  1. INPUT: Ask the user to enter float variables Width and Hight of a rectangle.
  2. PROCESSING: Compute the Area and Perimeter of the rectangle.
  3. OUTPUT: Show the Area and Perimeter of the rectangle rounded to 1 decimal. 

Test your programs with the numbers below, and make sure that your program runs (shows numbers rounded ) exactly as shown below:

Test Run 1:
Please enter height : 3.11
Please enter width : 4.2
Area = 3.11 * 4.2 = 13.1
Perimeter = 2 * (3.11 + 4.2) = 14.6
Test Run 2:
Please enter height : -1
Please enter width : 4.2
Invalid input, both values must be positive

Rounding Hint:

cout.setf(ios::fixed);    // To specify fixed point notation
cout.setf(ios::showpoint);// Specify that the decimal point will always be shown
cout.precision(1);        // To specify that ONE decimal place will be shown

More Examples:

< Example how to round a number to 2 and 3 decimal digits >

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   double pi = 3.141313;
   cout << fixed << setprecision(3) << pi << endl;
   cout << fixed << setprecision(2) << pi << endl;
   return  0;
}
/* Test Run:
3.141
3.14
Process finished with exit code 0
*/

< Example how to round using printf() C language >

// C way to show output and round floats
#include <iostream>  // need this for printf()
#include <iomanip>   // need this for cout <<  & cin >>
using namespace std; // need this for cout <<  & cin >>

int main()
{
   float num = 3.14159;
   float num2 = 3.14159;
   float num3;
   cout << "Please enter num3: ";
   cin >> num3;
   // show the numbers rounded to 1,2,3 decimals
   printf("num = %.1f , num2 = %.2f num3 = %.3f", num, num2, num3 );
   //  1 decimal ^^, 2 decimals ^^,3 decimals^^
   return  0;
}
/* Test Run:
Please enter num3: 1.23456
num = 3.1 , num2 = 3.14 num3 = 1.235
Process finished with exit code 0
*/

Program 11:

Write a program to determine if the user can vote. The user can vote if they are over 18 years old, are citizen, and are registered.

The program will ask the user a series of questions – age, citizenship and registration.

The user will receive a message as to whether or not s/he may vote — yes, no (with one or all of the reason(s) – too young, not a citizen, hasn’t registered to vote).

Hint: In order to vote, you must be over 18 “and” be a citizen “and” be registered to vote, 

TEST YOUR PROGRAM FOR ALL SAMPLE RUNS SHOWN BELOW:

(20% of grade for testing)

====== Test run 1:

Program to determine if you can vote

How old are you?   20

Are you a U.S. citizen?   yes

Have you registered to vote?  yes

Congratulations you can vote.

====== Test run 2:

Program to determine if you can vote

How old are you?   20

Are you a U.S. citizen?   yes

Have you registered to vote?  no

You cannot vote.

-You must register to vote.

====== Test run 3:

Program to determine if you can vote

How old are you?   20

Are you a U.S. citizen?   no

Have you registered to vote?  no

You cannot vote.

-You must be a citizen to vote.

-You must register to vote.

====== Test run 4:

Program to determine if you can vote

How old are you?   17

Are you a U.S. citizen?   no

Have you registered to vote?  no

You cannot vote.

-You must be over 18 to vote.

-You must be a citizen to vote.

-You must register to vote.

Save and submit it as p11.cpp

Hint:

#include <iostream>
using namespace std;
int main()
{
   int age;
   string citizen, registered;

   cout << "Program to determine if you can vote.n";
   cout << "How old are you? ";
   cin >> age;
   cout << "Are you a U.S citizen? (yes/no):";
   cin >> citizen;
   cout << "Have you registered to vote? (yes/no):";
   cin >> registered;

   if ( age >= 18 && citizen =="yes" && registered =="yes")
   {
       cout << "Congratulations you can vote.n";
   }
   else
   {
       if ( age < 18 )
           cout << " You must be over 18 to vote.n";
       // add 2 more if's, if not citizen, if not registered
   }
   return 0;
}

Program 12:

Write a program that allows the user to enter a number of quarters, dimes and nickels and then outputs the monetary value of the coins in cents. Validate the user input, if any of the values entered are negative – show an error message.

Your program must run exactly as you see below (play with  cout formating your output). 

Test Run 1:

Please enter quarters: 2
Please enter dimes: 3
Please enter nickels: 1
You have= 2 * (25) + 3 * (10) + 1 * (5) = 85 cents

Test Run 2:

Please enter quarters: -2
Please enter dimes: 3
Please enter nickels: 1
Input values must be positive!

Save and submit it as p12.cpp