6 Replies - 2807 Views - Last Post: 11 August 2010 - 12:01 PM Rate Topic: -----

#1 CasGrimes   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • 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   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 CasGrimes   User is offline

  • D.I.C Head

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

Re: Ways on improving my code

Posted 11 August 2010 - 07:17 AM

View Postsacred, 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).
Was This Post Helpful? 0
  • +
  • -

#4 Manipulate   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 sacred   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 sacred   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 sungchoiok   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • 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;  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1