-1

I keep geeting 0 as an output, although the code is written correctly, does anyone knows the problem? I should get the price after discount, buy somehow I keep getting 0 ad an output. Here is the code:

#include <iostream>
using namespace std;
int main() {
    int ages[5];
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        cin>>ages[i];
    }
    //your code goes here
    int youn = ages[0];
    for(int i = 0; i < 5; i++){

        
         if(ages[i] < youn){
            youn = ages[i];
        } 
        sum += 10;
    }
    int discount = (youn/100)*sum;
    sum -= discount;
    cout<<sum;
    return 0;
}

Here is the image of the problem

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    Try `(youn/100.)*sum` instead. – πάντα ῥεῖ Mar 23 '23 at 19:14
  • 3
    `int/int = int`, which means your answer is rounded, likely because it's between 0 and 1. – B Remmelzwaal Mar 23 '23 at 19:15
  • Related/duplicate: https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division – πάντα ῥεῖ Mar 23 '23 at 19:18
  • https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Marek R Mar 23 '23 at 19:20
  • 5
    *although the code is written correctly* -- If it was written correctly, you would have the correct answers. Saying "the code is written correctly" sounds like a very sneaky way to get others to debug your code, when you should be debugging the code. – PaulMcKenzie Mar 23 '23 at 19:39
  • `int discount = (youn/100)*sum;` -- You should have outputted what each part of that calculation was giving you. If you had done that, you would have seen that `youn/100` was giving the incorrect value. – PaulMcKenzie Mar 23 '23 at 19:43
  • I suggest making `sum` into a `double`, then `auto discount = sum * youn / 100;` would do the right thing and the final result would be correct. You also do not need to reinvent the wheel. Searching for the minimum element in arrays is one of the many things people do so often that it's included in the standard C++ library. [Example](https://godbolt.org/z/E8nhWdnK3) – Ted Lyngmo Mar 23 '23 at 19:48
  • Thanks all, this was really the problem, and what I meant by "Although the code is written correctly", is that it is correct in terms of syntax, not logic. – Mohammad Awni Alkhatib Mar 23 '23 at 20:29
  • *Although the code is written correctly* -- Advice -- You should refrain from saying things like this, even if you mean "the syntax is ok". It means absolutely nothing -- every program that runs has no syntax errors. Having no syntax errors has nothing to do with whether the program is logically correct. – PaulMcKenzie Mar 23 '23 at 20:42
  • I get your point, thank you. I wanted you to focus more on the logical error. – Mohammad Awni Alkhatib Mar 23 '23 at 22:16

1 Answers1

1

(youn/100)*sum is integer division by 100 the result will be 0 if youn is smaller than 100. You can either add 100. like πάντα ῥεῖ said or you could define youn as float and assign it like this

float youn = ages[0]*1.0f;