Maths problem

Hello,
I can't figure out why this code isn't working as I think it should do.
I want to load variables to the heap while displaying the percentage loaded.
Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

#define MAX_VARIABLES 100

int main() {
    int* pointers[MAX_VARIABLES + 1];
    
    cout << "Press any key to start loading resources" << endl;
    cin.get();

    for (int i = 1; i <= MAX_VARIABLES; i++) {
        pointers[i] = new int;
        cout << (i/MAX_VARIABLES)*100 << "% completed" << endl;
    }
    cout << "Press any key to unload them..." << endl;
    cin.get();
    
    for (int i = 1; i <= MAX_VARIABLES; i++) {
        delete pointers[i];
        cout << (i/MAX_VARIABLES)*100 << "% completed" << endl;
    }
    cout << "Press any key to exit..." << endl;
    cin.get();
    
    return 0;
}


The problem is in the loop while the cpu is calculating the percentage,
I get the following output:
0% completed
0% completed
0% completed
0% completed
0% completed
0% completed
(...)
100% completed

Any help will be appreciated
Last edited on
In C++, an integer divided by an integer will give you an integer.

So, what is 1/100? Zero.
What is 2/100? Zero.
What is 3/100? Zero.
What is 4/100? Zero.

If you want i/MAX_VARIABLES to give you a non-integer answer, one of them will have to be a non-integer.
Thank you very much!!
Solved it replacing
 
cout << (i/MAX_VARIABLES)*100 << "% completed" << endl;


by

1
2
float percentage = (((float)i / (float)MAX_VARIABLES) * 100);
cout << percentage << "% completed" << endl;

Topic archived. No new replies allowed.