Write a program that calculates how much a person would earn over a period of time if his or her salary is one penny the first day and two pennies the second day, and continues to double each day. The program should ask the user for the number of days.
The output earnings should be displayed in a dollar amount, not the number of pennies.
Input Validation: Do not accept a number less than 1 for the number of days worked.
Basically, the output displays the correct answer mathematically it just does not add them together. I am not sure on what to do to fix that issue.
//Declare Variables
int numDays = 1;
double money = 0.01;
double totalPay;
//Initialize or input i.e. set variable values
cin>>numDays;
//Map inputs -> outputs
while (numDays < 1)
{
cout<<"Enter a positive value ONLY!\n";
cin>>numDays;
}
for (int i = 1; i <= numDays; i++)
{
cout<<"Pay = $"<<money;
money *=2;
}
//Exit stage right or left!
return 0;
Expected Output
Pay·=·$0.03
My Output
Pay·=·$0.01Pay·=·$0.02
//System Libraries
#include <iostream>//Input/Output Library
#include <iomanip>
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
//Function Prototypes
//Execution Begins Here!
int main(int argc, char** argv) {
//Set the random number seed
//Declare Variables
int numDays = 1;
double totalPay;
double dayPay;
double money = 1;
//Initialize or input i.e. set variable values
cin>>numDays;
//Map inputs -> outputs
while (numDays < 1)
{
cout<<"Enter a positive value ONLY!\n";
cin>>numDays;
}
for(int i = 1; i <= numDays; i++)
{
dayPay = money / 100;
totalPay += dayPay;
money *=2;
}
cout<<"Pay = $"<<fixed<<setprecision(2)<<totalPay;
//Exit stage right or left!
return 0;
}
Related
I used Dev c++ 5.5.3 to write some programs. one of the programs is about getting some numbers (integer) until zero and then prints max, min , avg. In my computer everything is fine. in someone else computer, it does not show the right average and it shows very strange numbers 4.612521 e+8 and like this. I define a variable avg and calculate the value and then print it. The other one calculate the average directly when calling cout. can someone check This programs:
Program 1 which doesn't show the right answer:
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int n;
int max , min , count = 0 , s;
float avg;
cout<<"This program gets some numbers until zero and then prints maximum, minimum and average of them"<<endl<<"Please enter a number : ";
cin>> n;
max = n;
min = n;
while (n!= 0)
{
count ++;
s+=n;
if (n>max)
{
max = n;
}
if (n<min)
{
min = n;
}
cout<< "Please enter a number : ";
cin>>n;
}
cout<<"ZERO DETECTED!\n\n"<<endl;
cout<<"Maximum: "<<max<<endl;
cout<<"Minimum: "<<min<<endl;
avg = (float) s/count ;
cout<<"Average: "<<avg<<endl; // NOTE NOTE NOTE NOTE
return 0;
}
Program 2 which shows the right answer:
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int n;
int max , min , count = 0 , s;
float avg;
cout<<"This program gets some numbers until zero and then prints maximum, minimum and average of them"<<endl<<"Please enter a number : ";
cin>> n;
max = n;
min = n;
while (n!= 0)
{
count ++;
s+=n;
if (n>max)
{
max = n;
}
if (n<min)
{
min = n;
}
cout<< "Please enter a number : ";
cin>>n;
}
cout<<"ZERO DETECTED!\n\n"<<endl;
cout<<"Maximum: "<<max<<endl;
cout<<"Minimum: "<<min<<endl;
//avg = (float) s/count ;
cout<<"Average: "<<(float) s/count<<endl; //NOTE NOTE NOTE NOTE
return 0;
}
Both programs have undefined behaviour because s is not initialised and has not been assigned a value before you try to read from it for the first time:
int max , min , count = 0 , s;
[...]
s+=n;
All behaviour you have seen and the fact that it apparently "worked" on your computer and did "not work" on someone else's were more or less random occurrences.
Here's an easy fix:
int max , min , count = 0 , s = 0;
Note that your compiler should have warned you about the uninitialized variable. If not, then perhaps you should choose a higher warning level.
Also note that there are a lot of other flaws in your program, for example the use of using namespace std, that you declare multiple variables on the same line or that some of your variable names are not very descriptive.
Hello I am attempting to write a program and I seem to be stick. This program is in the end supposed to display two variants of a single 10 by 10 mine sweeper board. One of which where if there is a mine in that location it displays an asterisk(*) and if there is no mine it displays a period. The second variant also needs to display an asterisk where the mines are located on the board. But instead of a period it has to display the number of mines located 1 space away in any direction. I can't seem to think of how to easily write a program that will add up each of the mines located around each point. I also can't figure out why my program asks the initial question twice even if it meets the criteria for stopping the loop the first time around.
/*
Program: minesweeper(sort of)
The intention of this program is to display essentially two mine sweeper game boards one of which displays a *
where all of the mines would be located and a . where no mines are located. Then the second display
should display a * where all of the mines are and then display a number based on how many mines surround that space.
*/
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
double get_probability();
int plant_mines(bool mineCells[10][10], double prob);
int print(bool mineCells[10][10]);
int count_mines(bool mineCells[10][10], int mineCounters[10][10]);
int main()
{
//used to hold true if there is a mine located at said location or false if there is no mine
bool mineCells[10][10];
//used to hold a value of -5 if the corrosponding value on mineCells is true and if the corrosponding value on
//minecells is false it should add up and hold the value of how many mines are surroudning it.
int mineCounters[10][10];
get_probability();
double prob = get_probability();
plant_mines(mineCells, prob);
print(mineCells);
int stop;
cin >> stop;
return 0;
}
//asks the user for a number between 0 and 1 to use as the probability of mines apearing laster on.
double get_probability()
{
double prob;
bool repeat;
do
{
cout << "Please enter a number between 0 and 1: ";
cin >> prob;
//should execute once asking the user for a input between 0 and 1 and if the input is not between those two it shoud
//then continue to repeat untill the permaiters set in the question are met.
if (prob >= 0 && prob <= 1)
{
break;
}
else
{
repeat = true;
}
}while (repeat = true);
return prob;
}
//takes the probability given by the user and then uses it to generate that percentage of mines on the field
int plant_mines(bool mineCells[10][10], double prob)
{
srand((unsigned int)time(NULL));
for (int count = 0; count < 10; count++)
{
for (int counter = 0; counter < 10; counter++)
{
//generates a random number between 0 and 1 and sets it equal to the variable random
double random = static_cast<double>(rand()) / RAND_MAX;
//is the variable random is less than or equal to the probability then the array is set to true meaning
//there is a mine located at that position
if (random <= prob)
{
mineCells[count][counter] = true;
}
//iff the random number is greater than the user input than there is no mine located at said position
else
{
mineCells[count][counter] = false;
}
}
}
return mineCells[10][10];
}
//Should count up the mines surronding the location to be output later on
//(the mines surrounding the location do include all mines 1 space diagonal, vertical and horizontal from said location)
int count_mines(bool mineCells[10][10], int mineCounters[10][10])
{
for (int count = 0; count < 10; count++)
{
for (int counter = 0; counter < 10; counter++)
{
if (mineCells[count][counter] == 1)
{
mineCounters[count][counter] = 0;
}
else
{
}
}
cout << endl;
}
return 0;
}
//displays a * where ever a mine is locate and a . where ever a mine is not located
int print(bool mineCells[10][10])
{
for (int count = 0; count < 10; count++)
{
for (int counter = 0; counter < 10; counter++)
{
if (mineCells[count][counter] == 1)
{
cout << "*";
}
else
{
cout << ".";
}
}
cout << endl;
}
return 0;
}
My Basic Algorithm:
Ask for input money amount; Rolls two 6-sided dice; if they add up to 7, add 4 to money amount; else, subtract 1 from money amount; loop until moneyamount<0; loop game user says n when prompted to play again.
/*
*File: hw3
*Author: Nathaniel Goodhue
*
*Created on: 9/15/15
*Description: Game of lucky sevens
*
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
srand (time(NULL));
double moneyAmount;
int winValue = 7;
int numRolls = 0;
char playAgain = 'y';
while(playAgain == 'y')
{
cout<<"Enter the amount of money you are playing with: $";
cin>>moneyAmount;
while(moneyAmount>0)
{
int roll1= (rand()%6)+1;
int roll2 = (rand()%6)+1;
if(roll1+roll2 == winValue)
{
moneyAmount+=4;
numRolls++;
}
else
{
moneyAmount-=1;
numRolls++;
}
}
cout<<"It took "<<numRolls<<" roll(s) to lose all of your money"<<endl;
// cout<<"Your maximum amount of money was $" <<maxAmount<<" after "<<maxRolls<<" roll(s)"<<endl;
cout<<"Play again? y/n"<<endl;
cin>>playAgain;
if(playAgain == 'y')
{
cout<<"Enter the amount of money you are playing with: $";
cin>>moneyAmount;
numRolls = 0;
}
else
{
break;
}
}
return 0;
}
Above is my current code. It works as intended. What I am stuck on is that I need to be able to implement this line of code right after money drops below 0:
cout<<"Your maximum amount of money was $" <<maxAmount<<" after "<<maxRolls<<" roll(s)"<<endl;
I need to find out when there was the most money and after how many rolls that it appeared. The maxAmount variable would be the max amount of money achieved, and the maxRolls variable would be the number of rolls when maxAmount was reached.
This is pretty simple to add to your code. What you can do is check if the amount of money they have is greater than the max amount of money. If it is then set max to current and record the number of turns it took to get that value.
int maxAmount = moneyAmount, maxRolls = 0;
while(moneyAmount > 0)
{
int roll1 = (rand() % 6) + 1;
int roll2 = (rand() % 6) + 1;
numRolls++;
if(roll1 + roll2 == winValue)
moneyAmount += 4;
else
moneyAmount -= 1;
if (moneyAmount > maxAmount)
{
// the current amount of money is greater than the max so set max to current and get the number of rolls
maxAmount = moneyAmount;
maxRolls = numRolls;
}
}
The directions are: Design and code a program that asks the user how many numbers from which to determine an average. Then prompt the user for the integer values and sum them to a total. Display the sum of the numbers and the calculated average with appropriate accompanying text. The average should be shown with 1 decimal place. Repeat the process until the user enters zero (0) as the number of values to be averaged. You may use either a "while" loop or a "do…while" loop for the main program loop.
Use one function to read and sum the values and another function to display the sum and average. Use a "for" loop to read and sum the values.
The for loop doesn't seem to be executing, but I can't figure out why.
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int sumNums(int amount, int, int, int);
void displaySum(int sum, int avg);
main()
{
int amount = 0;
cout <<"How many numbers do you wish to average? ";
cin >> amount;
int avg = 0, sum = 0;
while (amount != 0)
{
for (int lim = 0; lim == amount; lim++)
{
int number = 0;
cout <<"Enter a value"<< endl;
cin >> number;
sumNums(amount, number, sum, avg);
displaySum (sum, avg);
}
}
}
int sumNums (int amount, int number, int sum, int avg)
{
sum = sum + number;
avg = sum / amount;
return sum, avg;
}
void displaySum (int sum, int avg)
{
cout <<"The sum is "<< sum <<" and the average is "<< avg << endl;
}
for (int lim = 0; lim == amount; lim++)
Here you set lim to 0 and the code runs only if amount is not 0. In your for you execute only if lim equals amount which never happens.
Whatever your condition is, it must evaluate to true for every iteration that you want to do.
Most probably, you will want to execute until lim equals amount so that means that you want it to execute for every iteration where lim is lower than amount.
for(int lim = 0; i < amount; lim++)
for (int lim = 0; lim == amount; lim++) // so wrong...
change to
for (int lim = 0; lim < amount; lim++)
Given a type Money that is a structured type with two int fields, dollars and cents. Assume that an array named monthlySales with 12 elements, each of type Money has been declared and initialized.
Assume that a Money-variable yearlySales has also been declared. Write the necessary code that traverses the monthlySales-array and adds it all up and stores the resulting total in yearlySales. Be sure make sure that yearlySales ends up with a valid value, i.e. a value of cents that is less than 100.
Now i'm not asking for the answer but, i'm asking how do i approach it. simply because i'm not sure how to address the question like how to code it. I have understand the first paragraph of the question respectively. here is my snippet of code. now im just stuck on how to compute it. i just need a bit of guidance. Thanks!
the code i have so far, accesses the array I have of 12 elements and assigns them random numbers of dollars and cents respectively.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
struct Money
{
int dollars,cents;
};
int main()
{
Money monthlySales[12], yearlySales;
for (int i = 0; i < 12; i++)
{
monthlySales[i].cents =rand()%99;
monthlySales[i].dollars =rand();
}
return 0;
}
Write the necessary code that traverses the monthlySalesarray and
adds it all up and stores the resulting total in yearlySales. Be sure
make sure that yearlySales ends up with a valid value , i.e. a value
of cents that is less than 100.
Money monthlySales[12], yearlySales;
yearlySales.cents = 0;
yearlySales.dollars = 0;
for (int i = 0; i < 12; i++)
{
yearlySales.cents += monthlySales[i].cents; // Add up the cents
yearlySales.dollars += monthlySales[i].dollars; // Add up the dollars
yearlySales.dollars += yearlySales.cents / 100; // If cents > 100, increase dollars appropriately.
yearlySales.cents = yearlySales.cents % 100; // If cents > 100, set it to the remainder.
}
//to compute Sum
for (int i = 0; i < 12; i++)
{
yearlySales.cents +=monthlySales[i].cents;//keeps adding yearlySales cents for each month
yearlySales.dollars +=monthlySales[i].dollars;//keeps adding yearlySales dollars
}
//if cents 100 convert it into dollars eg:720cents is convereted to 7$ 20 cents and 7 dollars is added to yearly dollars
if(yearlySales.cents > =100)
{
yearlySales.dollars+=yearlySales.cents/100;
yearlySales.cents=yearlySales.cents%100;
}
This works too!
float dollar = 0;
float cent = 0;
for (int i = 0; i < 12; i++) {
dollar += monthlySales[i].dollars;
cent += monthlySales[i].cents;
do {
if (cent > 100 ) {
dollar += 1;
cent -= 100;
}
}while (cent > 100);
}
yearlySales.dollars = dollar;
yearlySales.cents = cent;