Exponentials

Mar 20, 2016 at 10:04pm

I'm aware of how to do incremental patterns and there's lots of tutorials on that already, but this multiple-like function I cannot figure out. Would appreciate help, thank you.
And for clarity, every line is a multiple of its self times 2.
So it goes
1
2
4
8
16.
-----------------------------------------------------------------------------------------------------
Here's what I have currently:
#include <iostream>
#include <cmath>
using namespace std;
int main(){

int r=0;
int k=pow(2,r);

for (int r=0; r < 7; r++)
{
for (int c=0; c < k; c++)
{
cout << "+";
}
k=pow(2,r);
cout << endl;
}
return 0;
}
-------------------------------------------------------------------------------------------------
Issue is: I keep getting an extra + when I run it.
+
+
++
+++++

So I'm trying to get rid of the extra, and mirror it downwards.
Last edited on Mar 27, 2016 at 11:50pm
Mar 21, 2016 at 5:02am
@DragonflyBeach

I just moved the k=pow(2,r) after the parenthesis in the r loop, plus changed k from an int, to a double, and the program works fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>

using namespace std;
int main()
{

	int r = 0;
	double k;

	for (int r = 0; r < 7; r++)
	{
		k = pow(2, r); 
		for (int c = 0; c < k; c++)
		{
			cout << "+";
		}
		cout << endl; 
		
	}
	return 0;
}
Mar 21, 2016 at 7:02am
@whitenite1

Yeah I knew the placement was messing something up , but I should've declared via doubles the K value. I had tried only with the R value. Thanks a million.

By chance, do you know how to loop it backwards like a sideways pyramid using the same function? Perhaps adding:

for (int r=7; r>0; r--)

Thanks again
Mar 21, 2016 at 12:45pm
@DragonflyBeach

Sure.
All you need to do, is use the same two loops you already have, but substitute line 11 with
for (int r = 5; r >=0; r--)
Mar 21, 2016 at 3:34pm
@whitenite1

Ah, but using it within the same loop. I'm only allowed to use the '+' once in my assignment. So I suppose I have to place that line within the first loop somewhere.
Literally: "Your code should have only one '+' "

I need to find a way to use:
for (int r = 5; r >=0; r--)

Without making another cout<<"+" in a new loop because I can only use + once.
Last edited on Mar 21, 2016 at 4:44pm
Mar 21, 2016 at 3:52pm
@DragonflyBeach

Okay, here's how I would do the loops, using just one '+'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{

	int r = 0,back;
	double k;

	for (int r = 0; r < 13; r++)
	{
		if (r < 7)
			back = r; //Let back increase in value
		else
			back--; // Start decreasing back variable
		k = pow(2, back); 

		for (int c = 0; c < k; c++)
		{
			cout << "+";
		}
		cout << endl; 
	}
return 0;
}
Mar 21, 2016 at 5:08pm
@DragonflyBeach
So the back function returns the print to for (int r = 0; r < 13; r++) ?

EDIT: Oh I see, its not a function, its just holding for r value.
Very helpful. If/else are likely better than for if I'm restricted to using a statement once.

Thank you very much!
Last edited on Mar 21, 2016 at 5:12pm
Topic archived. No new replies allowed.