hello I'm working on an assessment where I have to implement Russian arithmetic into a programme outputting the result of each step as it is completed. what I am wondering is why the for loop is not outputting the desired result. the for loop should take number2 and keep doubling it a certain amount of times ( the amount of times it takes to half number 1 until it reaches 1) but the answer outputted is not correct.
here is what the code looks like. is there something wrong with the code within the for loop, am I missing something?
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
int number1, number2;
int counter = 0;
cout << "please input the first number you want to multiply" << endl;
cin >> number1;
cout << "please input the second number you want to multiply" << endl;
cin >> number2;
// this set of code is taking the first number and repeatedly halving until 1 and then storing the number of times the loop needed to run
vector <int> halving;
int tempnumber1{number1};
do
{
halving.push_back(tempnumber1);
counter++; // this counts how many times the loop needed to run to be used in the second step
tempnumber1/=2;
}
while // causes the loop to stop when number reaches 1
(tempnumber1>=1);
cout << "the number of times it takes to half " << number1 << " to get to 1 is " << counter << endl; // displays how many times the loop ran before reaching 1
int tempnumber2{number2};
vector <int> doubling;
int tempnumber3;
for (int c=0; c > counter; c++) // this doubles number2 by the amount of times it took to halve number1 until it reached 1 using a counter
{
tempnumber3 = tempnumber2+tempnumber2;
}
cout << " the result of doubling " << number2 << " by the amount of halves it took to reach 1 in the first stage is " << tempnumber3 << endl;
}
the comments are in there as a requirement of the assessment
Problem 1. counter is wrong because the loop runs once more when tempnumber1 is exactly equal to 1. Subtract 1 from it once you finish looping.
Problem 2. c < counter, not c > counter in line 40.
Problem 3. Line 43 simply keeps setting tempnumber3 to the same wrong number. Set tempnumber3 equal to number2 before the loop and keep multiplying it by 2 within the loop.
I wouldn't use the initialisation statements with the curly brackets here. They are a bit unnecessary and a C++11 extension to the standard. Just use assignment (=).
counter is wrong because the loop runs once more when tempnumber1 is exactly equal to 1. Subtract 1 from it once you finish looping
the first stage is correct the counter is outputting the same number as when I work it out e.g. 34 takes 6 steps 34, 17, 8, 4, 2, 1 an this is what the counter equals when entering this number
c < counter, not c > counter in line 40.
ok thanks I will change this
Line 43 simply keeps setting tempnumber3 to the same wrong number. Set tempnumber3 equal to number2 before the loop and keep multiplying it by 2 within the loop.
correct me if I'm wrong but I believe I did this with int tempnumber2{number2};
after fixing problem 2 its now outputting a different result and simply doubling the number once rather than multiple times