Float problem | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Float problem

I have a problem with float valuesūüė≠ūüė≠ūüė≠ ‚Äč‚Äč... sometimes it returns me hex values ‚Äč‚Äčother times not. Can anyone help me please? thank you very much

5th Feb 2022, 8:17 PM
Giuseppe Giusti
9 Réponses
+ 5
<ages> is an unitialized local variable which may contain garbage value. So assigning <ages>[ 0 ] to <min> is risky. Also, <ages> is an array of `int` so updates to <min> (a `float` type) isn't all too safe without type casting, because of different ways of how those types store values in memory. What was the task again?
6th Feb 2022, 12:00 AM
Ipang
+ 4
Some of us would like to know what language you had in mind. We are also lacking knowledge of how the code was instructed to format the output. Kindly share the code so we may properly recommend solutions.
5th Feb 2022, 9:53 PM
Brian
Brian - avatar
+ 2
sorry the values ‚Äč‚Äčin scientific notation not in hexūü§¶‚Äć‚ôāÔłŹ
5th Feb 2022, 8:23 PM
Giuseppe Giusti
+ 2
Giuseppe Giusti, Look at this, I added a bit of comment explaining the code flow. #include <iostream> using namespace std; int main() { int ages[5], min; float price = 50.0; cin >> ages[0]; // read first person's age min = ages[0]; // assume it as <min> for (int i = 1; i < 5; ++i) // read ages for remaining 4 persons { cin >> ages[i]; if (min > ages[i]) { min = ages[i]; } } float sum = price - price * min / 100.0; // calculate discount cout << sum; return 0; }
6th Feb 2022, 3:14 AM
Ipang
+ 1
Sorry the language is c++ #include <iostream> using namespace std; int main() { int ages[5]; float price = 50; float min = ages[0]; float sum = 0.0; for (int i = 0; i < 5; ++i) { cin >> ages[i]; if (min > ages[i]) { min = ages[i];} } //your code goes here sum = price - price * min / 100; cout << sum; return 0; }
5th Feb 2022, 10:45 PM
Giuseppe Giusti
+ 1
Ipang thank you very much. now I understandūüôŹ
6th Feb 2022, 3:09 PM
Giuseppe Giusti
0
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
6th Feb 2022, 12:12 AM
Giuseppe Giusti
- 1
It's the code project "ticket office" exercise of the learn c ++ section
6th Feb 2022, 12:11 AM
Giuseppe Giusti
- 2
Float is working just like int
6th Feb 2022, 5:05 PM
dan peled
dan peled - avatar