# Ways on improving my code

Page 1 of 1

## 6 Replies - 2825 Views - Last Post: 11 August 2010 - 12:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=185519&amp;s=4853021d88ca9ae622f779a464612a67&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CasGrimes

Reputation: 10
• Posts: 97
• Joined: 09-March 10

# Ways on improving my code

Posted 11 August 2010 - 07:04 AM

Below is a program that a guest here wanted done for them so I attempted it and to my surprise I did it quite quick.
What the task was is to output numbers in a pyramid way using only loops.
So the output should look like this:
1
121
12421
1248421
1248168421
12481632168421

Here's what I did but I'm pretty sure there is a much better way to do this.

```#include "PyramidLoop.h"
#include <iostream>
using namespace std;

void PyramidLoop::runLoop()
{
short line1[] = {1}, line2[] = {1, 2, 1}, line3[] = { 1, 2, 4, 2, 1}, line4[] = {1, 2, 4, 8, 4, 2, 1},
line5[] = {1, 2, 4, 8, 16, 8, 4, 2, 1}, line6[] = {1, 2, 4, 8, 16, 32, 16, 8, 4, 2, 1};

for (short i = 0; i < 1; i++)
cout << line1[i];
cout << endl;

for (short i = 0; i < 3; i++)
cout << line2[i];
cout << endl;

for (short i = 0; i < 5; i++)
cout << line3[i];
cout << endl;

for (short i = 0; i < 7; i++)
cout << line4[i];
cout << endl;

for (short i = 0; i < 9; i++)
cout << line5[i];
cout << endl;

for (short i = 0; i < 11; i++)
cout << line6[i];
cout << endl;
}

```

Is This A Good Question/Topic? 0

## Replies To: Ways on improving my code

### #2 sacred

Reputation: 5
• Posts: 42
• Joined: 31-October 08

## Re: Ways on improving my code

Posted 11 August 2010 - 07:10 AM

i dont think that this is the right code for this problem, this must be something like: enter the higth of the triagle or enter the power of 2 etc. your code works only for this triangle but i dont think that is the main aim

### #3 CasGrimes

Reputation: 10
• Posts: 97
• Joined: 09-March 10

## Re: Ways on improving my code

Posted 11 August 2010 - 07:17 AM

sacred, on 11 August 2010 - 06:10 AM, said:

i dont think that this is the right code for this problem, this must be something like: enter the higth of the triagle or enter the power of 2 etc. your code works only for this triangle but i dont think that is the main aim

I only need to output what I wrote in the OP but only using a loop to output it (instead of cout << "1" << endl; etc).

### #4 Manipulate

Reputation: 7
• Posts: 38
• Joined: 21-July 10

## Re: Ways on improving my code

Posted 11 August 2010 - 07:17 AM

Its a pretty simple pattern so what you did is extremely brute force. You can probably solve it with some nested for loops and an array that inserts the double of the last number inserted.

### #5 sacred

Reputation: 5
• Posts: 42
• Joined: 31-October 08

## Re: Ways on improving my code

Posted 11 August 2010 - 07:23 AM

i still dont think that the problem is what you understood... it is something crazy to write down all the powers of 2 upto some point in some arrayS(!) and use for loops just for printing the array members. The loops should be used for printing the powers of 2; 2 nested for loops can be used to maintain this triangle

### #6 sacred

Reputation: 5
• Posts: 42
• Joined: 31-October 08

## Re: Ways on improving my code

Posted 11 August 2010 - 07:43 AM

for instance:
```
#include <iostream>
using namespace std;

int main()
{

int a=1 ;

for(int i=0; i<6; i++){
cout<<a;
for(int j=0 ; j<i; j++){
a *= 2;
cout<< a;

}
for(int j=i-1; j>=0 ; j--){
a=a/2;
cout<< a;

}
cout<<endl;
a=1;
}

return 1;
}

```
does exactly what it does

This post has been edited by sacred: 11 August 2010 - 07:45 AM

### #7 sungchoiok

Reputation: 44
• Posts: 140
• Joined: 18-July 10

## Re: Ways on improving my code

Posted 11 August 2010 - 12:01 PM

lol... you (the original asker) might as well have written this code:

```cout << 1 << endl
<< 121 << endl
<< 12421 << endl
<< 1248421 << endl
<< 1248168421 << endl
<< 12481632168421 << endl;

```

Of course, the problem is that the user cannot control any aspect of that pyramid without changing the code itself. However, the code posted by sacred above, can do that..

What I can improve from sacred's code is to ask the user how much lines of pyramid they want, and the input of the problem goes to the "6" part on

```    int a=1 ;

for(int i=0; i<6; i++){
cout<<a;
for(int j=0 ; j<i; j++){
a *= 2;

```