hello I'm currently trying to set up Russian arithmetic for an assignment I have. to do this I first need to halve the number given until it reaches 1 and then double the second number by the amount of times the first number needed to be halved to reach 1. what I need to know is how to use the counter form the do while loop and use it to determine how many to times to execute the for loop. here is what I have so far
#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;
vector <int> halving;
int tempnumber1{number1};
do
{
halving.push_back(tempnumber1);
counter++;
cout << tempnumber1 << counter << endl;
tempnumber1/=2;
}
while
(tempnumber1>=1);
return 0;
int tempnumber2{number2};
vector <int> doubling;
for (int value : tempnumber2){
cout << value << endl;
}
for (int c=0; c< counter.size(); c++){
cout << tempnumber2 << endl;
tempnumber2*=2;
}
}
I first need to halve the number given until it reaches 1 and then double the second number by the amount of times the first number needed to be halved to reach 1.
so say you enter the number 40 the programme would repeatedly the half the number until it get to 1 and then note the amount of times the loop needed to execute to reach 1 (in this case 6) so then needs to double the second number entered 6 times
no so the user will enter 2 numbers the first number will be continually halved until it reaches 1 and then the second number will be continually doubled by the amount of times it took to continually halve the first number until it reaches 1 e.g. the user enters 5 and 4 5 will be halved until it reaches 1 which takes 3 steps so then 4 will be doubles 3 times
the reason for this is because I'm also required to make the steps for the Russian arithmetic be shown to the user when calculating so when they enter the numbers the stages for repeatedly halving and then repeatedly doubling the numbers are shown to the user if it was that simple I could simply say number1 * number2 buts its for an assignment so its overcomplicated to add difficulty
Nothing complicated in it. Your code, on the other hand has way too many unnecessary and erroneus bits. It does not even compile. Why the vector? Why the return on line 31?
there are more steps to the Russian arithmetic that I haven't started coding yet.
Why the vector?
the reason I am using a vector is because it is one of the requirements for the assessment. I have made some changes to the code and am getting a new error message on line 37 this is the new code
#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;
vector <int> halving;
int tempnumber1{number1};
do
{
halving.push_back(tempnumber1);
counter++;
cout << tempnumber1 << endl;
tempnumber1/=2;
}
while
(tempnumber1>=1);
int tempnumber2{number2};
const size_t ARRAY_SIZE {counter}; int array[ARRAY_SIZE];
for(size_t index=0; index < ARRAY_SIZE; index++)
int number = index + 1;
array[index] = tempnumber2 + tempnumber2;
for(int value : array) {
cout << value <<endl
}
}
}
What does the compiler say? Error messages are supposed to tell what seems to be wrong.
(Statement must end with a semicolon.)
Line 33 is not standard C++. The counter is not a compile-time constant, and therefore the compiler cannot possibly know how large array to create. Why do you create a plain array anyway? You said that you must use vector.
What statements are in the body of the for-loop that starts on line 35? What do those statements do?
What does the compiler say? Error messages are supposed to tell what seems to be wrong.
invalid types ' int [ARRAY_SIZE] [char*(const char*, int)]' for array subscript
Line 33 is not standard C++. The counter is not a compile-time constant, and therefore the compiler cannot possibly know how large array to create
ok that makes sense still new to C++ so figuring out what I can and cannot do
Why do you create a plain array anyway
because I have already used a vector for the first stage and am prioritising getting it working before anything else
What statements are in the body of the for-loop that starts on line 35? What do those statements do?
what it was supposed to do was keep doubling the second number by the amount it took to reach 1 when halving but since I cannot use counter to declare the size of the array guess this isn't possible with a static array