This homework assignment is about loops. The intention is for you to use a while
ID: 3665868 • Letter: T
Question
This homework assignment is about loops. The intention is for you to use a while loop, but the problem can also be solved with a for loop.
The Problem Statement
You and your roommates go through a lot of orange juice. You've noticed that your roommate goes through more orange juice than you do. And when they run out, they will take some of yours.
You have been charging your roommate every time they take some of your orange juice. But it feels a bit silly collecting 50 or 75 cents at a time from them. You’ve come up with a brilliant plan! Instead of charging them every time they take your orange juice, you’ll collect money from them after they have taken 10 dollars’ worth of juice.
Program Setup
Write a program to mimic this process. Ask the user to enter the size (in ounces) of the juice containers you buy, as well as the price of those containers (in dollars). Then prompt the user to enter how many times the roommate took juice. Finally, read the amount the roommate took each time. Every time the total value of the juice equals or exceeds $10, print out “Your roommate owes you $10.00.” After all the numbers are entered, if the roommate owes any money, print out the value owed.
Sample Run
What is the weight (in oz.) of the original container of OJ?
64
What is the cost of the original container of OJ in dollars?
3.79
How many times did your roommate take your juice?
10
How much juice did your roommate take this time (in oz.)?
30
How much juice did your roommate take this time (in oz.)?
34
How much juice did your roommate take this time (in oz.)?
24
How much juice did your roommate take this time (in oz.)?
40
How much juice did your roommate take this time (in oz.)? 64
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
18
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
20
How much juice did your roommate take this time (in oz.)?
20
Your roommate owes you $2.38.
Specification Homework assignments are either correct or incorrect. You must match these prompts and output formatting including punctuation.
What is the weight (in oz.) of the original container of OJ?
What is the cost of the original container of OJ in dollars?
How many times did your roommate take your juice?
How much juice did your roommate take this time (in oz.)?
Your roommate owes you $X.XX.
Submission Ensure that you get the same answers for the same input values when you run your program. Then, check a few other combinations of input.
Make sure you code is easy to read and that you include a header comment and comments throughout your main function.
Finally, attach your .c source file to this assignment and submit it.
Explanation / Answer
//header file
#include <stdio.h>
// initialize main function
int main()
{
// Declare variables
int rm_counter, n;
double oj_weight, cont_cost, peroz_cost, total_cost = 0, oz_taken;
// Asking input from user
printf("What is the weight (in oz.) of the original container of OJ? ");
scanf("%lf", &oj_weight);
printf(" What is the cost of the original container of OJ (in dollars)? ");
scanf("%lf", &cont_cost);
// calculate per orange cost
peroz_cost = cont_cost/oj_weight;
printf(" How many times did your roommate take your juice? ");
scanf("%d", &rm_counter);
// checking the condition using for Loop
for(n=0; rm_counter-n != 0; n++)
{
// asking input from user
printf(" How much juice did your roommate take this time (in oz.)? ");
scanf("%lf", &oz_taken);
// calculate total cost value
total_cost = total_cost + oz_taken*peroz_cost;
// checking the condition using if loop
// if the total cost is greater than or equal to 10 then it executes loop
if (total_cost >= 10)
{
printf(" Your roommate owes you $10.00. ");
total_cost = total_cost - 10;
continue;// continue the loop
}
else
{
continue;
}
}
// display output
printf(" Your roommate owes you %.02lf.", total_cost);
}
Sample output
What is the weight (in oz.) of the original container of OJ?
64
What is the cost of the original container of OJ in dollars?
3.79
How many times did your roommate take your juice?
10
How much juice did your roommate take this time (in oz.)?
30
How much juice did your roommate take this time (in oz.)?
34
How much juice did your roommate take this time (in oz.)?
24
How much juice did your roommate take this time (in oz.)?
40
How much juice did your roommate take this time (in oz.)? 64
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
64
How much juice did your roommate take this time (in oz.)?
18
Your roommate owes you $10.00.
How much juice did your roommate take this time (in oz.)?
20
How much juice did your roommate take this time (in oz.)?
20
Your roommate owes you $2.38.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.