using the positions of odd numbers in one array to determine which numbers to add in another

hello im trying to make a programme that takes 2 numbers and uses russian arithemetic (diplplaying each step) to multiply them. i have done the first 2 steps which are to continuously half the first number until you reach 1 and then continuously double the second number by the number it took to reach 1 when halving. what i want to know is how i would go about taking the odd numbers from the first array e.g. with the number 34 take the first number in the array (17) and the 5th (1) and using this to then take the 1'st and 5th numbers fromm the doubling array and add them together. this is the code i have for the first 2 steps.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>

using namespace 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
        cout << tempnumber1 << endl;
        tempnumber1/=2;
    }

    while // causes the loop to stop when number reaches 1

    (tempnumber1>=1);
    counter = counter - 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};
    int tempnumber3{number2};
    vector <int> doubling;


    for (int i=0; i < counter; i++) // this doubles number2 by the amount of times it took to halve number1 until it reached 1 using a counter
     {

        tempnumber3 = tempnumber3 + tempnumber3;
        cout << tempnumber3 << endl;

    }
    cout << " the result of doubling " << number2 << " by the amount of halves it took to reach 1 in the first stage is " << tempnumber3 << endl;

}

I really need to get this done any help would be appreciated thanks.
Isn't this at least third thread about the same program/problem? Previously:
http://www.cplusplus.com/forum/beginner/200847/
http://www.cplusplus.com/forum/beginner/200939/

How should it go for, say 21 and 7?
Like this?
	21	*	7	+	0	=	147
	10	*	14	+	7	=	147
	5	*	28	+	7	=	147
	2	*	56	+	35	=	147
	1	*	112	+	35	=	147

The 7 and 28 add up to 35.

On first row the "surplus" is 0.
On all other rows the surplus is initially same as the surplus on previous row, but if the first number was odd on the previous row, then we add the value of the second number from the previous row. Note too that the last line does not have surplus, even though 1%2==1.

In principle you do have all the necessary info within the vector 'halving' and the second loop.

Btw, what is the purpose of the line 37?


You, however, don't show such a table. Perhaps you would not show "35" either, but would prefer a "7+28"?
Last edited on
Isn't this at least third thread about the same program/problem? Previously
yes there are a couple other threads relating to the same programme but are different issues not really relating to each other so figures id make a new thread.
1
2
3
4
5
6
7
How should it go for, say 21 and 7?
Like this?
	21	*	7	+	0	=	147
	10	*	14	+	7	=	147
	5	*	28	+	7	=	147
	2	*	56	+	35	=	147
	1	*	112	+	35	=	147
yes it should go like this but I'm having trouble coming up with a way to implement the addition stage into the programme
Btw, what is the purpose of the line 37that's just something I forgot to delete when trying a different method thanks for pointing it out
In principle you do have all the necessary info within the vector 'halving' and the second loop yes its just a matter of accessing this information and using it to perform the addition stage




Last edited on
The info:
1
2
3
4
5
6
7
8
9
10
    cout << "The vector has " << halving.size() << " elements\n\n";
    if ( counter + 1 != halving.size() ) return 1; // assertion

    for ( size_t i=0; (i + 1) < halving.size(); ++i )
    {
        cout << "Previous " << halving[ i ] << " current " << halving[ i+1 ];
        cout << ", possible addition " << tempnumber3;
        tempnumber3 = tempnumber3 + tempnumber3;
        cout << " and doubled " << tempnumber3 << endl;
    }
Topic archived. No new replies allowed.