how to use the counter from a do while loop and use it in a for loop

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

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
#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;

 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;
}
}
Last edited on
Hi,
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.

Can you give us an example?
Last edited on
was meant to say 1 sorry been updated
Was meant to say 1 sorry been updated

Can you give us a number as an example? We need to figure how it works.
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
We continually half the a certain number until it reaches 1 and then continually double the 1 until it equals to that certain number correct?
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
Hello rdunning98,

If you are talking about the for loop at line 42 all you need to do is use c < counter.

"counter" is defined in the scope of main so it can be used anywhere in main until the program ends.

Hope that helps.

Andy
Do you mean:
1
2
3
4
Div    ; Mult
5/2 = 2; 4*2 = 8
2/2 = 1; 8*2 = 16
1/2 = 0; 16*2 = 32

Why don't you do both in the same loop?
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
Last edited on
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?


Does the Russian arithmetic mean this:
4 * 5 = 2 * 10 = 1 * 20 = 20
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
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
#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;
 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
}
}
}
Last edited on
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
Last edited on
True: cannot use static array. Use vector. That is the "easy option" for "getting it working".

The "invalid types" case is hard to notice. Perhaps it's a side-effect of something else.

Your "line 35 loop" has this:
1
2
3
4
for(size_t index=0; index < ARRAY_SIZE; index++)
{
  int number = index + 1;
}

Not quite what you had in mind?

Perhaps:
1
2
3
4
5
for(size_t index=0; index < ARRAY_SIZE; index++) 
{
  int number = index + 1;
  array[index] = tempnumber2 + tempnumber2;
}

Even that has issues. The number is not used. Lets take it away:
1
2
3
4
for(size_t index=0; index < ARRAY_SIZE; index++) 
{
  array[index] = tempnumber2 + tempnumber2;
}

However, why to do ARRAY_SIZE additions, when you can cache the unchanging value:
1
2
3
4
5
int temp = tempnumber2 + tempnumber2;
for ( size_t index=0; index < ARRAY_SIZE; index++) 
{
  array[index] = temp;
}

Suddenly, things seem not quite as they should, do they?



The code below is not what you want; too obscure. It might give you some ideas though.
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
#include <iostream>
#include <vector>

struct Foo {
	int  lhs = 0;
	int  rhs = 0;
	Foo( int x ) : lhs(x) {}
};

std::ostream & operator<< ( std::ostream & out, Foo const & foo ) {
	out << '\t' << foo.lhs << "\t*\t" << foo.rhs
		<< "\t=\t" << foo.lhs*foo.rhs << '\n';
	return out;
}

int main() {
	int in = 5;
	std::vector<Foo> list;
	while ( 0 < in ) {
		list.emplace_back( in );
		in /= 2;
	}
	int out = 4;
	for ( size_t bar = 0; bar < list.size(); ++bar ) {
		list.at(bar).rhs = out;
		out *= 2;
	}
	for ( auto x : list ) {
		std::cout << x;
	}
	return 0;
}

... or confuse.
Last edited on
Topic archived. No new replies allowed.