Need help

Oct 16, 2018 at 2:44pm
I have a question hope I can explain it.

Let's say I have 3 cars right. And I'm using for loop. I need to find which car uses least fuel for most driven distance. And I just don't know how to make it that in for loop if car uses less fuel and drives most driven distance it would say that first, second or third. I tried to do like best = i; But it doesn't assign to the correct i. I know it's hard to explain....

Example

1st car - Uses 30l fuel and drives 100km
2nd car - 30l 120km
3rd car - 30l 80km

It would make sense that the 2nd is the answer but in my for loop i doesn't assign to the 2nd car.

1
2
3
4
5
6
          for (i = 1; i <= n; i++) { // The n is how many cars were tested

          cout << "Type " << i << " Car used fuel and driven distance: \n";
          cout << "Fuel: "; cin >> fuel;
          cout << "Distance: "; cin >> distance;
}


And basicly I have no idea how to continue it so it would assign and check if the previous car was better or not. All I know that is my if should be something like
if (fuel < ??? (don't know what to type) && distance > (don't know what to type as well)

best = i;
Last edited on Oct 16, 2018 at 2:49pm
Oct 16, 2018 at 2:52pm
How do I compare second car fuel and distance with first one?
Oct 16, 2018 at 3:02pm
Create a double best_km_per_litre and an int best_i. You can initialise both as 0.

As you go through the loop, calculate km_per_litre for that car. If it is bigger than best_km_per_litre then update the latter to the new value and update best_i to i. If it is not bigger then you don't have to update. At any time you are comparing with all previous cars, not just the immediately preceding one.

After finishing looping just print out best_km_per_litre (best fuel efficiency) and best_i (number of best car).

That's it, unless multiple cars have the same "best fuel efficiency", in which case you would have to store car details in an array and traverse it twice.
Last edited on Oct 16, 2018 at 3:20pm
Oct 16, 2018 at 3:28pm

#include <iostream>

using namespace std;

int main()

{
int n, i, km_per_litre, best_i, best_km_per_litre;

best_km_per_litre = 0;
best_i = 0;

cout << "Iveskite su kiek skirtingu automobiliu buvo atlikta tyrimu: \n";
cin >> n;


for (i = 1; i <= n; i++) {

cout << "Iveskite " << i << " automobilio sanaudas ir nuvaziuota atstuma: \n";
cout << "Sanaudos: "; cin >> best_km_per_litre;
cout << "Atstumas: "; cin >> km_per_litre;

if (km_per_litre > best_km_per_litre) {

best_i = i;
}

}

cout << "Geriausias automobilis: " << best_i;

return 0;



Ignore some parts it's written in my native language for cout. I still don't understand. It doesn't work as it should be.
Last edited on Oct 16, 2018 at 3:36pm
Oct 16, 2018 at 3:36pm
Please put your code in code tags (the first item, <>, on the formatting menu to the right.

Please ensure that it is the whole code, and is properly indented and formatted: no missing braces, or braces in the wrong place. Also, remove variables that you don't use (sanaudos and atstumas).

You are correctly updating best_i. You need to update best_km_per_litre at the same point.

Make this change and then let's see the code again. Remember those code tags!
Oct 16, 2018 at 3:42pm
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
33
34
35
36
  #include <iostream>

using namespace std;

int main()

{
    int n, i, km_per_litre, best_i;
    double best_km_per_litre;

    best_km_per_litre = 0;
    best_i = 0;

    cout << "Iveskite su kiek skirtingu automobiliu buvo atlikta tyrimu: \n";
    cin >> n;


    for (i = 1; i <= n; i++) {

        cout << "Type " << i << " Vehicle used fuel and driven distance: \n";
        cout << "Fuel: "; cin >> best_km_per_litre;
        cout << "Distance: "; cin >> km_per_litre;

        if (km_per_litre > best_km_per_litre) {

            best_i = i;
            best_km_per_litre = km_per_litre;
        }

    }

    cout << "Geriausias automobilis: " << best_i;

    return 0;

}


I get best_i as a 0. I think something is still wrong in my loop.
Last edited on Oct 16, 2018 at 3:44pm
Oct 16, 2018 at 3:55pm
You should enter litre and km, not what you are doing at present.
Then CALCULATE km_per_litre = km / litre;

Note that n, i and best_i are (correctly) int.
km, litre, km_per_litre and best_km_per_litre are all double.
Last edited on Oct 16, 2018 at 4:04pm
Oct 16, 2018 at 3:56pm
Experiment with n different cars. Everyone in the empty tank was charged with a certain amount of fuel after the car drove until the fuel stalled. Type each cars fuel consumption and mileage of each car.

I think I needed to use while loop ? because "The car drove until the fuel stalled".
Oct 16, 2018 at 3:58pm
No, a for loop is fine. Do NOT use a while loop here.
Please make the changes I have just suggested.
(Sorry, the system crashed out as I was posting - had to retype it twice).
Last edited on Oct 16, 2018 at 4:00pm
Oct 16, 2018 at 4:15pm
I don't understand. I'm completely lost. am I suppose to calculate them in loop or not?
Oct 16, 2018 at 4:19pm
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
33
34
35
36
37
  #include <iostream>

using namespace std;

int main()

{
    int n, i, best_i;
    double best_km_per_litre, km, litre, km_per_litre;

    best_km_per_litre = 0;
    best_i = 0;

    cout << "Iveskite su kiek skirtingu automobiliu buvo atlikta tyrimu: \n";
    cin >> n;

    for (i = 1; i <= n; i++) {

        cout << "Iveskite " << i << " automobilio sanaudas ir nuvaziuota atstuma: \n";
        cout << "Fuel: "; cin >> litre;
        cout << "Distance: "; cin >> km;

        if (km_per_litre > best_km_per_litre) {

            km_per_litre = km / litre;
            best_km_per_litre = km_per_litre;
            best_i = i;
        }

    }

    cout << "Geriausias automobilis: " << best_i;

    return 0;

}


This is what I have so far
Oct 16, 2018 at 4:21pm
Oh it supposed to be out of loop. I finally did it. Thank you very much sir.
Topic archived. No new replies allowed.