0
Struggling with pointers and DMA
Hey (again), Iâm trying to complete the 36 end of module project for c++, and could someone please explain to me how to insert pointers and why my code doesnât work? #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } int min = ages[0]; for(int i = 0; i < 5; i++) { if(ages[i] < min) min = ages[i]; } float percentage = min; float discount = 50/100 * percentage; float sum = 50 - discount; cout << sum; return 0; }
2 Answers
+ 2
Dividing an integer by another integer in C++ will yield an integer as well, i.e. the decimal part is truncated. Therefore:
50 / 100 = 0
If you expect a decimal result, at least one operand has to be a floating point type, e.g.
50.0 / 100 = 0.5
50 / 100.0 = 0.5
50.0 / 100.0 = 0.5
Everything else should be fine. Why are you worrying about inserting pointers?
0
cheers! i had been stuck on this for quite a while and i assumed that i needed to use pointers and DMA to solve it as it was the last few topics before the project