I solved SoloLearn Problem on VS code and when i entered code on their platform it didnt work.

So i was solving this SoloLearn Ticket office problem with C++. here is problem:
"~You are working on a ticketing system. A ticket costs $10.
The office is running a discount campaign: each group of 5 people is getting a discount, which is determined by the age of the youngest person in the group.
You need to create a program that takes the ages of all 5 people as input and outputs the total price of the tickets.

Sample Input:
55
28
15
38
63

Sample Output:
42.5

The youngest age is 15, so the group gets a 15% discount from the total price, which is $50 - 15% = $42.5 ~"
so i did it myself on visual studio with g++.exe (c++20) GCC 11.2.0. i test it with test inputs from SoloLearn and it worked just fine in visual studio. but it outputs large numbers on SoloLearn Platform. what could it be?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>

int main(){
    using std::cout;
    using std::cin;
    
    int ages[5];
    double rslt;
    int min = ages[0];
    for(int i = 0; i < 5; i++){
        cin>>ages[i];
        if(ages[i] < min){
            min = ages[i];
        }}
    rslt = min;
    rslt /= 100;
    rslt *= 50;
    rslt = 50 - rslt;
    cout<<rslt<<"\n";
    return 0;
}
Last edited on
L7 ages isn't initialised - so at the start of the program could contain any values. L9 then sets min to ages[0] so min could be any value.

Make L9:

 
int min = 200;


ie the start min is the more than any valid min. Hence L12 will be true for the first and subsequent iterations.

L7 should be:

 
int ages[5] {};


To initialise ages to all 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main() {
    using std::cout;
    using std::cin;

    int ages[5] {};
    int min = 200;

    for (int i = 0; i < 5; i++) {
        cin >> ages[i];
        if (ages[i] < min) {
            min = ages[i];
        }
    }

    double rslt = min / 100.0;

    rslt *= 50;
    rslt = 50 - rslt;

    cout << rslt << "\n";
    return 0;
}

Last edited on
thank you!
how do i delete a comment?
Last edited on
first entered number is 1 if its less than 200 it gets replaced by second entered number and so on. if im wrong explain please. lets say smallest entered number 15 it compares to 200 and if its smaller it gets replaced? how does it work exactly ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main(){
    using std::cout;
    using std::cin;

    int ages[5];
    double rslt;
    int min = ages[0];
    for(int i = 0; i < 5; i++){
        cin>>ages[i];
        if(ages[i] < min){
            min = ages[i];
        }}
    cout<<"lowest age number: "<<min<<"\n";
    rslt = min;
    rslt /= 100;
    rslt *= 50;
    rslt = 50 - rslt;
    cout<<"answer: "<<rslt<<"\n";
    return 0;
}
when i replayed with that i realised :D it gets replaced so its no longer 200 im so dumb...
i dont get why exactly 200 tho :/ is it just matter of preference ? it can be 100 or 120 or 180 too right?
Last edited on
seeplus wrote:
the start min is the more than any valid min.

The input is ages of humans. People can be over 100 years old. Some possibly over 120 years too. The 200 looks like something that is larger than any input we expect. So does 180.

However, if the chaps that are mentioned in the first part of Christian Bible do show up, then 200 is not enough. The safest value is std::numeric_limits<int>::max(). See http://www.cplusplus.com/reference/limits/numeric_limits/

The other approach is to have two loops. The first reads in all the values. The second finds the lowest value. Then you don't need separate initial value for min.
Topic archived. No new replies allowed.