I need some explaining.

Hey guys. Good evening.
So i've been trying to do a line of code for about a hour now but I don't seem to get it.

3 <-- how many lines
1 12.50 2
3 10.50 1
2 7.50 3
^...^...^
r ...s... k

I need to find the lowest sum of the list shown above.The anwswer is 3 10.50 1
The first one means the type of flower it is. The second one is the flowers price
The last one is how many of the flowers are there.

The exercise asks me to get the lowest sum of the diffrent types of flowers. Which is 3 10.50 1.

I've tried this and many more things. ( ind = indicator of what type of flower)
if ( lowest > s1) // s1 = s * k;
{
lowest =s1;
ind1 = s1;
ind1 = i;

I would appreciate some help. I just need this to be explained
Last edited on
for all the entries
find the smallest of 2nd*3rd
looks like you are close...
something like (not real code)
double lowest = first_2times3;
for( i = second; i < total_lines; i++)
{
tmp = this_entry2ndtimes3rd;
if(tmp < lowest)
save i or tmp or both whatever you needed.
}
The exercise asks me to get the lowest sum of the diffrent types of flowers. Which is 3 10.50 1.

That is not the lowest sum. The "sums" in your example data are:
25.00
10.50
22.50

The
3 10.50 1

is the entry that has the lowest sum.

If you need the r s k of the entry that has lowest sum, then you have to store the r s k somehow, not just the (s*k).
When posting code, please use code tags so that the code is readable:


[code]
code goes here
[/code]


1
2
3
4
5
if ( lowest > s1) // s1 = s * k;
{
    lowest =s1;
    ind1 = s1;
    ind1 = i;


Both L4 and L5 set ind1 - with L5 overwriting the value set in L4.

What is lowest initialised to? Often in these situations it's set to be larger than the possible value - or to the largest possible for its type. If lowest is already set to a value lower than the found lowest, then the if condition will never be true!

See http://www.cplusplus.com/reference/limits/numeric_limits/

Not far to go now ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//blah blah

    int r_min{0};
    double sum{0};
    double sum_min{1000};  // USE std:: ... numeric MAX for double as above??
    
    for(int i = 0; i < NO_LINES; i++)
    {
        sum = s[i] * k[i];
        if( sum < sum_min)
        {
            // blah blah
            // r_min = blah blah
        }
    }

    // output answers 
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
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

struct Flower
{
   int type;
   double price;
   int number;
};

istream & operator >> ( istream &in, Flower &flower ){ return in >> flower.type >> flower.price >> flower.number; }
ostream & operator << ( ostream &out, const Flower &flower ){ return out << flower.type << " " << flower.price << " " << flower.number; }
bool operator < ( const Flower &Bill, const Flower &Ben ){ return Bill.price * Bill.number < Ben.price * Ben.number; }

int main()
{
   Flower flower, cheapest;
   ifstream in( "input.txt" );
   int N;
   in >> N >> cheapest;
   for ( int i = 1; i < N; i++ )
   {
      in >> flower;
      cheapest = min( flower, cheapest );
   }
   cout << cheapest << '\n';
}


3 10.5 1

Last edited on
Topic archived. No new replies allowed.