Solve it....

Pages: 12
A sales person leaves his home every Monday and returns every Friday. He travels by company car. Each day on the road the sales person records the amount of gasoline put into the car. Given the starting odometer reading (that is, the odometer reading before her leaves on Monday) and the ending odometer reading (the odometer reading after he returns on Friday), design an algorithm and computer program to find the average miles per gallon. (Take the gasoline unit as gallon)
Last edited on
What code have you got so far?
The algorithm you're trying to design is:

(number of miles driven) / (amount of fuel put into car)
Here is pseudocode that you could possibly use to solve this problem.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Declare variables of double type: Odometer Reading before and after, gasoline,
sumOfGasoline (Initialized to 0), averageMilesPerGallon

Prompt user to enter the odometer reading before they leave on Monday
and store it in odometerBefore

Prompt user to enter the odometer reading after they return on Friday
and store it in odometerAfter

for ( Monday Through Friday )
    Prompt user to enter the amount of gasoline put into the car each day and store in gasoline
    Add gasoline to sumOfGasoline

averageMilesPerGallon = ( odometerAfter - odometerBefore ) / sumOfGasoline


Is this what you were looking for?



EDIT: I don't know what C++ Concepts you have covered so far. But if your class has not covered loops yet, you can simply write 5 statements to get the amount of gasoline for each day. But of course, with some kind of loop, it is much easier.
Last edited on
we have covered loops and please write here a well formated code of this problem
Show me what you have so far. It's fine even if it doesn't compile. If you can show me your best attempt at least, I will see what I can do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 double odometerReadingBefore;
        double odometerReadingAfter;
        double averageMilesperGallon;
        int gasolineDay1;
        int gasolineDay2;
        int gasolineDay3;
        int gasolineDay4;
        int gasolineDay5;
        int sumOfGasoline;

        cout << "Please enter the odometer reading before :: ";
        cin  >> odometerReadingBefore;
        cout << "Please enter the odometer reading after  :: ";
        cin  >> odometerReadingAfter;
        cout << "Please enter the amount of gasoline used at first day  :: ";
        cin  >> gasolineDay1;
        cout << "Please enter the amount of gasoline used at second day :: ";
        cin  >> gasolineDay2;
        cout << "Please enter the amount of gasoline used at third day  :: ";
        cin  >> gasolineDay3;
        cout << "Please enter the amount of gasoline used at fourth day :: ";
        cin  >> gasolineDay4;
        cout << "Please enter the amount of gasoline used at fifth day  :: ";
        cin  >> gasolineDay5;

        sumOfGasoline = gasolineDay1 + gasolineDay2 + gasolineDay3 + gasolineDay4 + gasolineDay5;
        averageMilesperGallon = (odometerReadingAfter - odometerReadingBefore)/sumOfGasoline;
        cout << "Average miles per gallon is :: " << averageMilesperGallon;



Last edited on
Fair Enough.

The reason why I recommended double type instead of int is because some cars have decimals for odometer. For example, my car has 25346.4 Miles

Same for the amount of gasoline in gallon. I think it'd be better to have that as double as well. After all, gas stations express the amount of gasoline in decimals.

Your code looks correct as far as I can tell.

However, if you want to convert it to for-loop, you can do the following
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double sumOfGasoline = 0;		// Must be initialized to 0

double gasoline;

for ( int iter = 1 ; iter <= 5 ; iter ++ ) {
	cout << "Enter the amout of gasoline used on day " << iter << ": ";

	cin >> gasoline;

	sumOfGasoline += gasoline;

}


averageMilesperGallon = ( odometerReadingAfter - odometerReadingBefore ) / sumOfGasoline;

cout << "Average Miles Per Gallon is " << averageMilesperGallon;


Also, you can forget about creating the variable, averageMilesperGallon and instead do the following:

 
cout << "Average Miles Per Gallon is " << ( odometerReadingAfter - odometerReadingBefore ) / sumOfGasoline;

But, what's the harm in having one additional variable..

Hope that helps.
Last edited on
thanx for helping me bro....i got your point and i have some more problem that i did not understand its formula can you help me? i will send you problems one after one
To make a profit, the prices of an item sold in a furniture store are marked up by 60%. Design an algorithm and computer program to find the selling price of an item sold at the furniture store. What information do you need to find selling price?


please help me how can i make this program ??
I will give you some hints first.

The store wants to make a profit with 60% Mark-up.

Let's say that I am selling a chair. The original price of the chair is $100, which means, I paid $100 to obtain this chair.

Now, I am selling this chair to a customer and for me to make profit, I need to increase the price.

I want 60% markup, so I do the following:

100 + 100 * .60 = $160.

So, I sell the chair for $160, that way, I make $60, which is 60% of the original price of the chair.

Information You will Need:
Price of the Item

Read over the question and my comment and see if you can come up with the algorithm / code.
thnx now i can solve this problem now help me to solve another one



Suppose that the cost of sending an international fax is calculated as follows: Service charges for using fax machine is 3.00 Rs; 0.20 Rs per page for the first 10 pages; and 0.10 Rs for each additional page. Design an algorithm that asks the user to enter the number of pages to be faxed. The algorithm then uses the number of pages to be faxed to calculate the amount of number of pages faxed.


i did not understand anything to do please help me to solve this problem

Using the fax machine incurs 3.00 Rs, so no matter how many pages you fax, you will pay 3.00 Rs if you fax at least 1 page.

Note that the price is 0.20 Rs per page for the first 10 pages, but after the 10 pages, the cost reduces to 0.10 for each additional page.



Algorithm will be something like this:

If user is faxing at least 1 page, then the service charge of 3.00 Rs is added to the cost.

Then, using some sort of a loop, the program will start charging for each page - 0.20 Rs for the first 10 pages and 0.10 for any number of pages after the first 10 pages.

You will probably need conditional statements for the first 10 pages.


Last edited on
int item;
int percentage;
cout << "Please enter the amount of item ";
cin >> item;

percentage = item + item * .60;
cout << "your profit is :: " <<percentage;

i did this but how can i find selling price>????
how can i solve it using loop because i have dim concepts about loop
In my example above, $160 WAS the selling price.

So in your program, "percentage" variable would actually be the selling price.

Also, you want to name your variable so that it is self-explanatory and reflects the value that it stores.

For example, instead of "item", use something like "priceOfItem"

It's better to have a longer variable name and actually understand what that variable is exactly for than having something like a, b, c and later in the program, you forget what a is supposed to store.

You might write the program for yourself right now, but later, you will write the codes for whoever you will be working for and other people will read your code and they must be able to understand it. I'd definitely practice naming variables and also using comments when necessary.

Also, the value that percentage stores in your program isn't actually percentage. 60% or .60 is the percentage. So I would change the name of "percentage" to
 
double sellingPrice;


Also remember that prices are almost certainly double, not int. Change the variable types to double and if you want to go further beyond, use <iomanip> to show only 2 decimal places in the standard output.

Last edited on
double priceOfItem;
double sellingPrice;
cout << "Please enter the amount of item ";
cin >> priceOfItem;

sellingPrice= priceOfItem + priceOfItem * .60;
cout << "original price :: " << priceOfItem<<endl;
cout << "profit = " << sellingPrice;

point out my mistakes
I reread the question and the question does not ask for the actual profit, so you can probably remove that. It only wants the selling price. So instead of "Profit = ", change it to "Selling Price: "

Change .60 to 0.60

User <> under Format to Tag your code next time so it's more readable

Like This
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    double priceOfItem;

    cout << "Please enter the price of item: ";
    cin >> priceOfItem;

    double sellingPrice = priceOfItem + priceOfItem * 0.60;

    cout << setprecision ( 2 ) << fixed << showpoint; 
    cout << "Original Price: " << priceOfItem << endl;
    cout << "Selling Price: " << sellingPrice << endl;

    return 0;
}


Here, i showed you how to use <iomanip> library to show 2 decimal places in your output.
Last edited on
thanx now help me to solve next problem that i posted before
I posted the algorithm for the fax machine problem. Were you able to come up with the code?
Pages: 12