I set the variable i
to 0 as an initial condition of my for
loop. I know for a fact that at the end of the loop, i >= 1
(and I will manipulate my input such that this holds true). I then subtract 1 from i
and THEN use the pow()
function. At worst, it would have (0,0)
which it can handle and would output 1.
When I run my code, however, the program outputs nothing. No exit codes, nothing. When I went into the debugger (my first time there, mind you), I saw pow(2,-1)
hidden amid the complex stuff I didn't understand.
Does the computer still check for i = 0
even after manipulation?
Below is the function I'm talking about. I'm trying to write the input in base 2, but such that I can always change the base later.
#include <iostream>
#include <vector>
using namespace std;
vector<int> base(int x)
{
int i;
int mult;
vector<int> res;
while (true) {
for (i = 0; pow(2, i) < x; i++) {}
i = i - 1;
for (mult = 1; mult * pow(2, i) < x; mult++) {}
mult = mult - 1;
res.push_back(mult);
x = x - (mult * pow(2, i));
if (x == 0) {
break;
}
}
return res;
}
int main()
{
cout << base(90)[0];
}