+ 1
CPP code project . How did you solve #ticket_office project on C++ .
I used nested if , is there a better way to do it ? C++ 35 code project . Ticket office https://code.sololearn.com/coyTWPukjJtO/?ref=app
8 Respuestas
+ 4
Elias ,
instead of using the if ... construct to find the minimum number, you can sort the number array and then take index[0] to pick the minimum nunber.
don't forget to include: #include <bits/stdc++.h>
here a link how it could be done:
https://www.geeksforgeeks.org/sort-c-stl/
+ 2
Elias
Assign first value of the array in a variable as a min value then compare this min value with next value until you don't get min value.
So you can do like this:
int minage = ages[0];
Now compare this with next values inside for loop.
for(int i = 0; i < 5; i++) {
if (minage > ages[i]) {
minage = ages[i];
}
}
cout << 50 - (50.0 * minage / 100);
---------Here is another way without loop-------
int n = sizeof (ages) / sizeof(ages[0]);
int &min = *min_element(ages, ages + n);
cout << 50 - 50 * (min / 100.00);
You need to import this header file for this solution:
#include <bits/stdc++.h>
+ 2
Thank u so much A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ and Lothar . It's a lot of help
+ 2
Elias [101]
I am sure you did some mistakes.
Try this and tell if there is any issue
int ages[5];
for (int i = 0; i < 5; ++i) {
cin >> ages[i];
}
int minage = ages[0];
for(int i = 0; i < 5; i++) {
if (minage > ages[i]) {
minage = ages[i];
}
}
cout << 50 - (50.0 * minage / 100);
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
Yes you are right
Works fine now👍 thank u again
u got ur self a follower hh
+ 1
Elias
You have written Hard Code if else conditions if there are more than 10 inputs then what?
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ the first method it runs perfect and the output is also correct but there is a warning massage "ages[0] is used uninitialized in this function"
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ you are right that's why I am asking for a better solution
Maybe we could use while loop for the nested if🤔.