0

Can anybody give me a suggestion wht is mistake in mine code

#include <iostream> using namespace std; int main() { int age[5]; int min=age[0]; float total=50.0; float discount; float actual; for(int i=0; i<5; i++){ cin>>age[i]; if(age[i]<min){ min=age[i]; } } discount =total*min/100; actual =total-discount; cout<<actual; } https://www.sololearn.com/discuss/2710640/?ref=app

2nd Mar 2021, 12:16 PM
Param Khatri
Param Khatri - avatar
2 Answers
+ 1
age[ 0 ] will be an undefined value until you write into it, which is why initializing "min" with it is undefined behaviour and will likely lead to wrong results. Choosing a sufficiently large constant instead should be enough, e.g. the max value an int can store: int min = ( ( 1u << 31 ) - 1 ); You could also request the first value seperately beforehand and use that to initialize "min", then run the loop only for the remaining four elements.
2nd Mar 2021, 12:50 PM
Shadow
Shadow - avatar
2nd Mar 2021, 2:54 PM
Ketzha Agni Firstantyo Nugroho
Ketzha Agni Firstantyo Nugroho - avatar