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
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.