Suppose you and a friend both work at the local grocery store. The boss gives you two choices of how you will be paid. Here are the choices:
Choice 1: The boss will pay you 1 cent the first day, 2 cents the second day, 4 cents the third day, 8 cents the fourth day, and so on. Each day your salary will be double of your previous day's salary.
Choice 2: The boss will pay you $50.00 per day.
Since you want to make the most money, you decide to write a C program that calculates the number of days you need to work for Choice 1 to be better than Choice 2. Suppose you take Choice 1 and your friend takes Choice 2. Write the necessary C code to print out the number of days you need to work in order for your total salary to surpass your friend's total salary. Like all programs, it is necessary to solve the steps of formulating the problem and describe the algorithm.
Problem Specification:
The Algorithm:
The C Program:
int main (void) {
/* declarations */
int num_days = 0;
/* total number of days necessary to
earn more money by doubling salary */
int today_price = 1;
/* in cents, today's pay for the doubling
salary */
int friend_price = 5000; /* in cents,
the day's pay for the friend */
int my_total = 0;
/* in cents, my total salary */
int friend_total = 0;
/* in cents, my friend's total salary */
while (my_total <= friend_total) {
my_total = my_total + today_price;
num_days = num_days + 1;
today_price = today_price * 2;
/* prepare for tomorrow's pay */
friend_total = friend_total + 5000;
/* make sure friend gets paid */
}
printf("The number of days is: %d\n", num_days);
/* return successfully */
return 0;
}
Solution using dollars:
int main (void) {
/* declarations */
int num_days = 0;
/* total number of days necessary to
earn more money by doubling salary */
double today_price = .01; /* in dollars, today's
pay for the doubling
salary */
double friend_price = 50.0; /* in dollars, the day's pay
for the friend */
double my_total = 0.0; /*
in dollars, my total salary */
double friend_total = 0.0; /* in dollars, my friend's
total salary */
while (my_total <= friend_total) {
my_total = my_total + today_price;
num_days = num_days + 1;
today_price = today_price * 2.0;
/* prepare for tomorrow's pay */
friend_total = friend_total + 50.0; /*
make sure friend gets paid */
}
printf("The number of days is: %d\n", num_days);
/* return successfully */
return 0;
}