Description

Programs to upload: p15.cpp, p16.cpp, p17.cpp, p18.cpp, p19.cpp

Assignments (word or text files) to submit: Binary.docx, Hex.docx

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 15:

Write a program that generates a random number between 10 to 25.

The program then shows the random number generated, and informs the user if that number is odd or even.

Hint:

1) if (num%2 == 0) …then num is Even

2) Example of how to make a random number:

#include <iostream> // need this for cout, cin
#include <time.h>   // need this for time(NULL)
#include <stdlib.h> // need this for srand(iseed)
using namespace std;

int main()
{
   int iseed = time(NULL); // the system time (big number)
   srand(iseed); // make a list of random numbers (based on iseed)
   int min = 10;
   int max = 20;
   // make a random number between 10 to 20
   int randomNum = min + rand()%(max-min+1);
   cout << "Random number b/w 10 to 20: " << randomNum << endl;
   return 0;
}
/* Test Run:
Random number b/w 10 to 20: 19
Process finished with exit code 0
*/

=== Sample Run =====

Test Run 1:

Generating a random number between 10 to 25: 20

The random number 20 is Even.

Test Run 2:

Generating a random number between 10 to 25: 15

The random number 15 is Odd.

Save and submit it as p15.cpp

Program 16:

Write a C++ program, which declares a char variable letterOrNumber.

Ask the user to input any character from the keyboard into that letterOrNumber.

Inform the user whether they entered:

– an upper or lower case letter, 

– a number, 

– something else.

Hint: 

– Use the ASCII table.

https://theasciicode.com.ar/ascii-codes.txt

ascii (1).gif

– Example:  int(letterOrNumber), cast a character into an integer of the ASCII table:

#include <iostream>
using namespace std;

int main()
{
   char letterOrNumber = 'A';          // single quotes ' ' for char
   cout << letterOrNumber << endl;     // shows A
   cout << int(letterOrNumber) << endl;// shows 65(ascii value of A)
   cout << char(65) <<endl;            // shows A (casting 65 into char)

   cout << "Please enter a number: ";  // prompt
   cin >> letterOrNumber;              // input
   // numbers 0-9 have ascii values 48 - 57
   if ( int(letterOrNumber) >= 48 && int(letterOrNumber) <= 57)
       cout << letterOrNumber << " is a number n";
   return 0;
}
/*
A
65
A
Please enter a number: 6
6 is a number

Process finished with exit code 0
*/

=== Sample Runs === 

Please enter a number, letter, or something else: k

What you entered is a lower case letter.

Please enter a number, letter, or something else: K

What you entered is an upper case letter.

Please enter a number, letter, or something else: 1

What you entered is a number.

Please enter a number, letter, or something else: *

What you entered is neither a number or a letter.

Save and submit it as p16.cpp

Program 17:

Write a program to simulate rock-paper-scissors game.
Each players enters ‘R’, ‘P’, ‘S’ or 1, 2, 3 for Rock, Paper, Scissors.
The program then shows the winner on the basis of:

  • – Paper covers Rock
  • – Rock breack Scissors
  • – Scissors cut Paper
  • – Tie

Test your program multiple times to makes sure it works! Submit all 4 of your tests as a comment.

Hint:

#include <iostream>
using namespace std;

int main()
{
   // p16 hint:
   int p1=2, p2=1;//user enters these (1-rock, 2-paper, 3-scissors)
   int rock = 1, paper = 2, scissors = 3;

   // 3 ways for p1 to win:
   if ( p1 == rock && p2 == scissors )
       cout << "p1 wins, rock breaks scissors n" << endl;
   if (p1 == paper && p2 == rock )
       cout << "p1 wins, paper covers rock n";
   // one more way for p1 to win: scissors cut paper, you do this:

   // 3 ways for p2 to win (use the example for p1):

   // 3 ways for p1 and p2 to tie (use example for p1):

   return 0;
}
/*
p1 wins, paper covers rock

Process finished with exit code 0
*/

Sample Runs:

Enter Rock(1), Paper(2) or Scissors(3)
Player 1: 1
Player 2: 3
Result: Player 1 wins, Rock breaks Scissors!

Enter Rock(1), Paper(2) or Scissors(3)
Player 1: 3
Player 2: 2
Result: Player 1 wins, Scissors cut paper!

Enter Rock(1), Paper(2) or Scissors(3)
Player 1: 2
Player 2: 1
Result: Player 1 wins, Paper covers rock!

Enter Rock(1), Paper(2) or Scissors(3)
Player 1: 3
Player 2: 3
Result: Tie!

Save and submit it as p17.cpp

Program 18:

Write a program to convert any given number of total cents (under 100) into the correct number of: quarters, dimes, nickels, pennies.

Hint, use integer division.

Sample Run:

Please enter the number of cents: 66
# of Quarters: 2 x 25c = 50 cents total
# of Dimes: 1 x 10c = 10 cents total
# of Nickels: 1 x 5c = 5 cents total
# of Pennies: 1 x 1c = 1 cents total

Sample Program:

#include <iostream>
using namespace std;

int main()
{
   // p17 hint
   int totalCoins, q, d, n, p;

   cout << "Please enter total cents: ";
   cin >> totalCoins;
   // Number of Quarters
   q = totalCoins / 25;
   // only show how many quarters if there are any:
   if (q > 0)
       cout << q << " quarters x25c is " <<q*25<< " cents";
   totalCoins = totalCoins - q*25;//subtract quarters from total coins
   
   return 0;
}
/*
Please enter total cents: 76
3 quarters x25c is 75 cents
Process finished with exit code 0
*/

Program 18:

Write a program to convert any given number of total cents (under 100) into the correct number of: quarters, dimes, nickels, pennies.

Hint, use integer division.

Sample Run:

Please enter the number of cents: 66
# of Quarters: 2 x 25c = 50 cents total
# of Dimes: 1 x 10c = 10 cents total
# of Nickels: 1 x 5c = 5 cents total
# of Pennies: 1 x 1c = 1 cents total

Sample Program:

#include <iostream>
using namespace std;

int main()
{
   // p17 hint
   int totalCoins, q, d, n, p;

   cout << "Please enter total cents: ";
   cin >> totalCoins;
   // Number of Quarters
   q = totalCoins / 25;
   // only show how many quarters if there are any:
   if (q > 0)
       cout << q << " quarters x25c is " <<q*25<< " cents";
   totalCoins = totalCoins - q*25;//subtract quarters from total coins
   
   return 0;
}
/*
Please enter total cents: 76
3 quarters x25c is 75 cents
Process finished with exit code 0
*/

Program 19:

Write a program that asks the user for day and month of a birthday.
The program then tells the Zodiac signs that will be compatible with that birthday.

Zodiac Signs:
Constellation English Name    Dates
-Aries The Ram Mar. 21–Apr. 19
-Taurus The Bull Apr. 20–May 20
-Gemini The Twins May 21–June 21
-Cancer The Crab June 22–July 22
-Leo The Lion July 23–Aug. 22
-Virgo The Virgin Aug. 23–Sept. 22
-Libra The Balance        Sept. 23–Oct. 23
-Scorpio The Scorpion        Oct. 24–Nov. 21
-Sagittarius The Archer Nov. 22–Dec. 21
-Capricorn The Goat Dec. 22–Jan. 19
-Aquarius The Water Bearer        Jan. 20–Feb. 18
-Pisces The Fishes        Feb. 19–Mar. 20

Compatible Zodiac signs:
- Fire    (Aries, Leo, Sagittarius)
- Earth (Taurus, Virgo, Capricorn)
- Air        (Gemini, Libra, Aquarius)
- Water (Cancer, Scorpio, Pisces)

===== SAMPLE RUN =====
Please enter day of birth: 18
Please enter month of birth: 5
A birthday of 05 / 18 had the zodiac "Taurus"
Taurus is compatible with: Taurus, Virgo, Capricorn.

Sample Program:

#include <iostream>
using namespace std;

int main()
{
   // p18 hint
   int month = 3; // user enters this, March is 3, etc.
   int day = 22;  // user enters this

   // the date ranges for "Aries" are: March 21 - April 19
   if ( (month==3 && day>=21 && day<=31) || (month==4 && day>=1 && day<=19) )
   {
       cout << "You are "Aries""