Program concept Flashcards
Create a program that will display Hello World. As illustrated in class.
Do not forget to use good comments
include <iostream></iostream>
//Jessica Ruvalcaba
//08.28.2023
//Desc. this program is to introduce c++ and compilers
using namespace std;
int main()
{
cout «_space;“Hello World!\n”;
}
Write a complete program that will accept two integer values from the keyboard and perform and display the results from following opertations.
Add; Subtract; Multiply, divide
Make sure you include very good comments.
include <iostream></iostream>
//Jessica Ruvalcaba
//09.04.2023
//Desc. prog. to add,subtract and mulptiplie and divide two numbers. With using the format of declare/populate/process/display
using namespace std;
int main()
{
//declared
int number1;
int number2;
int total;
//populate
cout«“enter value 1: “;
cin»number1;
cout«“enter value 2: “;
cin»number2;
//process
total= number1+number2;
cout«total«
“=”«number1«”+”«number2«“\n”;
total= number1-number2;
cout«total«
“=”«number1«”-“«number2«“\n”;
total= number1number2;
cout«total«
“=”«number1«“”«number2«“\n”;
total= number1/number2;
cout«total«
“=”«number1«”/”«number2«“\n”;
//display
cout«_space;“\nAdd, Sub, Mult, Divide\n”;
cout«”————————–\n\n”;
}
GrossPay.cpp
The director of human resources asked you to write a program that will calculate an employee’s grossPay. Write a C++ program featuring the following requirements:
Accept the hourlyRate from the keyboard
Accept the hoursWorked from the keyboard
Calculates the grossPay for the employee
Formula: grossPay = hourlyRate * hoursWorked
Display the Hourly Rate: , Hours Worked: , and Gross Pay: , on the screen (make sure it is formatted well .
Don’t forget to use good comments.
Submission
include <iostream></iostream>
//Jessica Ruvalcaba
//09.06.2023
//Desc. prog. to calculate an employee’s gross pay.
using namespace std;
int main()
{
//declared
double hourlyrate;
double hoursworked;
double grosspay;
//populate
cout«“enter hourly rate: “;
cin»hourlyrate;
cout«“enter hours worked : “;
cin»hoursworked;
//process
grosspay= hourlyrate*hoursworked;
//display
cout«“grosspay “«grosspay;
}
MinimumMaximumNumber.cpp
Write a program that asks the user to enter two numbers. The program should use the conditional operator to determine which number is smaller and which is the larger, or are they equal.
include <iostream></iostream>
//Jessica Ruvalcaba
//9.13.2023
//Desc. program using if else statements to find out which number of the two is greater or if they are equal to each other
using namespace std;
int main()
{
//declared
int num1;
int num2;
//populate
cout«“enter number 1: “;
cin»num1;
cout«“enter number 2: “;
cin»num2;
//process&display
if(num1>num2)
{
cout«“Number 1 is greater\n”;
}
else if(num1<num2)
{
cout«“Number 2 is greater\n”;
}
else
{
cout«_space;“Numbers are =\n”;
}
cout«_space;“Thanks for using my program\n”;
cout«_space;“have a great day\n\n\n”;
}
DisplayLetterGrade.cpp
Write a program that will display a letter grade when a score is entered. Use the following
A 90%
B. 80%
C. 70%
D 60%
F. 50%
Use if-else if-else statement.
include <iostream></iostream>
//Jessica Ruvalcaba
//9.14.2023
//Desc. program using if else statements to display letter grade
using namespace std;
int main()
{
//declared
int score;
//populate
cout«“enter quiz score: “;
cin»score;
//process&display
if(score>=90)
{
cout«“A\n”;
}
else if(score>=80) { cout<<"b\n"; } else if(score>=70) { cout<<"c\n"; } else if(score>=60) { cout<<"d\n"; } else { cout<<"F\n"; } cout<< "Thanks for using my program\n"; cout<< "have a great day\n\n\n"; }
A bank asked you to create a software application (program) that will determine if a applicant is eligible for a car loan. The criteria is the applicant must be a recent graduate, and have a job. Your program will tell the loan officer if the applicant is granted the car loan or denied.
//Jessica Ruvalcaba
//9.24.2023
//Desc. program is to see if an applicant is eligible for a car loan.
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;</ctime></cstdlib></iostream>
int main()
{
unsigned seed=time(0);
srand(seed);
//declare
int recentGraduate;
int job;
//populte/process
cout«“Are you a recent graduate?(1:yes, 0:No):”;
cin»recentGraduate;
if(recentGraduate==1)
{
cout«“Do you have a job?(1:yes,0:No): “;
cin»job;
}
else{
cout«“you are not eligible for a loan”;
}
//display
if(job==1)
cout«“You are eligible for a loan”;
if(job==0)
cout«“You are not eligible for a loan”;
}
cointToss.cpp
Hi Everyone, I am excited about the progress you are making and had fun yesterday talking about the rand() function with loops. I realize programming feels a little scary still, keep practicing, compiling, making sure your programs are up to standard and it will come together.
This assignment mimics tossing a coin. 0 for heads and 1 for tails. The computer will generate a 1 or 0 when using the modulus operator of 2. Then you are to make your “call” 1 for heads, two for tails, and keep track of how many wins for the computers and how many wins for the user. Once you have played best out of seven, you should display the results.
NOTE: make sure the user only enters a 0 or a 1 for their guess
NOTE: make sure the program runs until the game is over.
//Jessica Ruvalcaba
//9.20.2023
//Desc. program is to play seven rounds of heads and tails and to see who won the computer or you based on your results
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;</ctime></cstdlib></iostream>
int main()
{
unsigned seed=time(0);
srand(seed);
//display
int coinToss;
int userGuess;
int score=0;
char option = ‘y’;
while(option==’y’||option==’y’)
{
int i=0;
while (i<7)
{
//populte
coinToss=rand()%2;
cout«“Enter 0 heads, 1 tails: “;
cin»userGuess;
//process/display
while(userGuess<0|| userGuess >1)
{
cout«“Invalid Choice:O for heads, 1 tails: “;
cin»userGuess;
}
if(coinToss==userGuess)
{
cout«“Lucky, you win!\n\n”;
score ++;//score =score+1
}
else
{
cout«“Looser\n\n”;
}
i++;//i=i+1
}//close while game play
cout«“User Score: “«_space;score«“\n\n” “computer score:”«(7-score)«“\n\n”;
score=0;//reset for next game
cout«“Continue Y/N: “;
cin»option;
}//close the while continutation
}//clse main
MathTutor.cpp
MathTutor.cpp
Write a program that can be used as a math tutor for a young student. The program should display two random numbers to be added, such as
247
+ 129
__________
The program should then pause while the student works on the problem. When the student is ready to check the answer, he or she can press a key and the program will display the correct solutions:
247
+ 129
_______
376
Using the while-loop() , Have the program run until the user is finished.
Please don’t forget to make very good comments
//Jessica Ruvalcaba
//9.26.2023
//Desc. program is to be a math tutor for a young student.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;</ctime></cstdlib></iostream>
int main() {
int random_num1;
int random_num2;
int solution;
cout << "" << endl << "Two numbers will appear on your screen below." << endl << "Please add them togther and then check the answer." << endl << "" << endl; // generate random numbers less than 1000 srand(time(0)); random_num1 = rand() % 999; random_num2 = rand() % 999; solution = random_num1 + random_num2; // print the math problem and message cout << " " << random_num1 << endl; cout << "+ " << random_num2 << endl; cout << "-----" << endl; cout << "Press ENTER or RETURN on your keyboard to show the solution." << endl; // pause program while the user works on the problem // and print solution when the user presses enter or return cin.get(); cout << "The answer is " << solution << endl << "" << endl; return 0; }
RockPaperScissor.cpp
Write a program that lest the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows.
When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, than the computer has chosen rock. If the number is 2 then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (don’t display the computer’s choice yet.
The user enters his or her choice of “rock”, “paper”, or “scissors” at the key-board. (you can use a menu if you prefer)
The computer’s choice is displayed
A winner is selected according the to following rules:
If one player chooses rock and the computer chooses scissors, than rock wins (the rock smashes the scissors.)
If one player chooses scissors and the computerr chooses paper, then scissors win. (scissors cut paper)
If one player chooses paper and the computer chooses rock, then paper wins (paper wraps rock)
If both players make the same choice, the game must be played again to determine the winner.
Be sure to keep track of the number of wins for the player and computer.
Program should run until the player is finished with the game.
When players is finished running the program, it should display the total wins and determine the winner.
//October 2,2023
//Jessica Ruvalcaba
//Desc. to play games of rock paper scissor with computer
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
// starting main function
int main(){
char ch;
// set up my variables for the scores
int win = 0;
int tie = 0;
int lose = 0;
// start of game loop, the loop will run untill ch == n
do{
int choice;
cout << "--------------------------------------" << endl;
cout << "-- Lets play Rock, Paper, Scissors! --" << endl;
cout << "--------------------------------------" << endl;
// Ask the player to choose Rock, Paper, Scissors
cout << "Press 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;
cin >> choice;
// gets a random number between 1 and 3 and tell the player what was chosen
int ai = rand() % 3 + 1;
cout << "The computer chose: " << ai << endl;
// starts possible outcome sequence in rock paper scissors there are 9 possible out comes 3 wins 3 ties and 3 losses.
if(choice == 1 && ai == 1){
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if(choice ==1 && ai== 2){
cout << "Rock is covered by Paper the computer wins!." << endl;
lose++;
}
else if(choice == 1 && ai == 3){
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if(choice == 2 && ai == 1){
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if(choice == 2 && ai == 2){
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if(choice == 2 && ai == 3){
cout << "Paper is cut by Scissors the computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 1){
cout << "Scissors are crushed by Rock computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 2){
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if(choice == 3 && ai == 3){
cout << "Scissors meet Scissors its a tie!" << endl;
tie++;
}
// this is what happens if the player doesn't hit 1, 2 or 3
else{
cout << "You didn't select 1, 2, or 3" << endl;
}
// displays your score so far and asks if you want to play again
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" << endl;
cin >> ch;
system("CLS");
}while(ch == 'Y' || ch == 'y');</cstdlib></time.h></cmath></iostream>
Stock Profit: The profit from the sale of a stock can be calculated as follows:
Profit = ((NS * SP) – SC) – ((NS * PP) + PC)
Where
NS is the number of shares,
SP is the sale price per share,
SC is the sale commission paid,
PP is the purchase price per share, and
PC is the purchase commission paid.
If the calculation yields a positive value, then the sale of the stock resulted in a profit. If the calculation yields a negative number, then the sale resulted in a loss.
Write a program (using appropriate functions) that accepts as arguments the number of shares, the purchase price per share, the purchase commission paid, the sale price per share, and the sale commission paid. The function should return the profit (or loss) from the sale of stock.
//Jessica Ruvalcaba
//10.01.2023
//Desc. program is to return the profit or loss from the sale of stock
//profit formula:((NS * SP) – SC) – ((NS * PP) + PC)
#include <iostream>
#include <iomanip>
using namespace std;</iomanip></iostream>
// Function prototype
double stockProfitLoss(int, double, double, double, double);
int main()
{
int NumOfShares;
double ProfitLoss,
PurchPrice,
PurchComm,
SalePrice,
SaleComm;
cout << "\nCaculate the profit (or loss) from the sale of stock.\n" << "What is the number of shares? "; cin >> NumOfShares; cout << "What is the purchase price per share? "; cin >> PurchPrice; cout << "What is the purchase commission paid? "; cin >> PurchComm; cout << "What is the sale price per share? "; cin >> SalePrice; cout << "What is the sale commission paid? "; cin >> SaleComm; //profit loss ProfitLoss = stockProfitLoss(NumOfShares, PurchPrice, PurchComm, SalePrice, SaleComm); cout << "The profit / loss from the sale of stock is $"; cout << fixed << showpoint << setprecision(2); cout << ProfitLoss << endl << endl; return 0; } double stockProfitLoss(int NumOfShares, double PurchPrice, double PurchComm, double SalePrice, double SaleComm) { return ((NumOfShares * SalePrice) - SaleComm) - ((NumOfShares * PurchPrice) \+ PurchComm); }
DailySales.cpp
You are the owner of a candy store, and you want to know how much is sold on any given day of the week. You Write a program that will create an array of size 5 and gather the following information.
1) Using arrays and functions: The program will ask the user to enter the sales for monday, tuesday, wednesday, thrusday and friday.
2) Using the array you created, call a Function: Determine and display which day has the highest sales, which day has the lowest sales.
3) Usingthe array you created, call a Function: Determine and display the total and average of all sales
Write this program to run until the End-User has finished using it.
Do not forget to use good comments.
include<iostream></iostream>
//Jessica Ruvalcaba
//10/06/2023
//Writing an array to know how much is sold each day from a candy store from monday to friday.Caculating the highest sales day,lowest sale day,average sales and total of all sales.
using namespace std;
//function for taking input
void input(int arr[])
{
//taking input daywise cout<<"Enter the sales of monday: "; cin>>arr[0]; cout<<"Enter the sales of tuesday: "; cin>>arr[1]; cout<<"Enter the sales of wednesday: "; cin>>arr[2]; cout<<"Enter the sales of thrusday: "; cin>>arr[3]; cout<<"Enter the sales of friday: "; cin>>arr[4]; cout<<endl; } //function to Determine and display which day has the highest sales, which day has the lowest sales void highestAndLowest(int arr[]) { int high=0,low=0; //Determining highest and lowest for(int i=1;i<5;i++) { if(arr[i]>arr[high]) { high=i; } if(arr[i]<arr[low]) { low=i; } } //displaying highest if(high==0) { cout<<"highest sales day is monday, sale: "<<arr[0]; } if(high==1) { cout<<"highest sales day is tuesday, sale: "<<arr[1]<<endl; } if(high==2) { cout<<"highest sales day is wednesday, sale: "<<arr[2]<<endl; } if(high==3) { cout<<"highest sales day is thrusday, sale: "<<arr[3]<<endl; } if(high==4) { cout<<"highest sales day is friday, sale: "<<arr[4]<<endl; } //displaying lowest if(low==0) { cout<<"Lowest sales day is monday,sale: "<<arr[0]; } if(low==1) { cout<<"Lowest sales day is tuesday,sale: "<<arr[1]<<endl; } if(low==2) { cout<<"Lowest sales day is wednesday,sale: "<<arr[2]<<endl; } if(low==3) { cout<<"Lowest sales day is thrusday,sale: "<<arr[3]<<endl; } if(low==4) { cout<<"Lowest sales day is friday,sale: "<<arr[4]<<endl; } cout<<endl; } // function to Determine and display the total and average of all sales void totalAndAverage(int arr[]) { int total=0; double average; //calculating total for(int i=0;i<5;i++) { total+=arr[i]; } //calculating average average=(double) total/5; //printing total and average cout<<"Total of all sales is :"<<total<<endl; cout<<"Average of all sales is : "<<average<<endl; } //driver main function int main() { int key; do{ //declaring array int arr[5]; input(arr); highestAndLowest(arr); totalAndAverage(arr); cout<<"\nPress 1 if you wanted to use program again else press any key: "; cin>>key; }while(key==1); cout<<"\nThank you! for using program"; }