4 Replies - 330 Views - Last Post: 15 January 2013 - 05:09 PM Rate Topic: -----

#1 wikki2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-January 13

Trouble in required output

Posted 15 January 2013 - 09:38 AM

Hi there,

I am a beginner to C++. I have been trying to write the codes for approximately a month. Now I am studying Loops. I have come across many programs printing a certain pattern with the help of loops. Here is a program written by me;

#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
	int number;
	int temp;
	cout << "Enter a number: ";
	cin >> number;
	temp = number;
	for (int i = 1; i <= number; i++)
	{
		for (int j = 1; j<=temp; j++)
			cout << j;
		temp--;
		cout << endl;
	}
	getch();
}



Now, I have tried to get the same output but in a pyramid form, I have tried a lot but I can't make any logic. I know some of it like

#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
	int number, temp;
	cout << "Enter a number: ";
	cin >> number;
	temp = number;
	for (int i = 1; i<=number; i++)
	{
		for (int j = 1; j<=i; j++)
			cout << " ";
		for (int k = 1; k<=2*i-1; k++)
			cout << k;
		cout << endl;
	}
	getch();
}



I want the output of the first program to appear in a form of a pyramid. I tried to code it (as i showed you in the second code) but it is not giving me the required output. I hope you can give me some hints to help solve this issue. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble in required output

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 356
  • View blog
  • Posts: 785
  • Joined: 27-June 09

Re: Trouble in required output

Posted 15 January 2013 - 11:01 AM

In the first code, before you output the row, you need to add a certain number of spaces. for the first row it is 0 spaces, for the second row it is 1 space, for the third it is 2 and so on. So, before the "j" loop add a "k" loop to print these spaces. Also, the pyramid would look better if you put a space after each j.

This post has been edited by mojo666: 15 January 2013 - 11:10 AM

Was This Post Helpful? 1
  • +
  • -

#3 wikki2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-January 13

Re: Trouble in required output

Posted 15 January 2013 - 11:39 AM

Okay! I have now completed the program with the hints you have given.
I have this program, it also requires a valid output;
	#include <iostream>
	#include <conio.h>
	using namespace std;
	 
	void main()
	{
	    int number;
	    int temp;
    cout << "Enter a number: ";
	    cin >> number;
	    temp = number;
	    for (int i = 1; i <= number; i++)
	    {
			for (int k = temp; k>=1; k--)
				cout << " ";
	        for (int j = 1; j<=i; j++)
	            cout <<  j << " ";
	        temp--;
	        cout << endl;
	    }
	    getch();
	}


This program prints the following output:

.........1
.......1 2
....1. 2.. .3
. .1.. 2. . 3. . 4.
1. 2. . 3. 4. .5

{excluding the points, )

Now i want this input like this;

.........1
.......2 1
....3. 2.. .1
. .4.. 3. . 2. . 1.
5. 4. . 3. 2. .1

I hope you can help me solve this issue on your earliest. Regards.
Was This Post Helpful? 0
  • +
  • -

#4 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 356
  • View blog
  • Posts: 785
  • Joined: 27-June 09

Re: Trouble in required output

Posted 15 January 2013 - 04:21 PM

The J loop is contolling the row output

for(int j = 1; j<=i; j++)


As written it starts at 1 and increases to i. Change it to start at i and decrease to 1.
Was This Post Helpful? 2
  • +
  • -

#5 wikki2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-January 13

Re: Trouble in required output

Posted 15 January 2013 - 05:09 PM

View Postmojo666, on 15 January 2013 - 04:21 PM, said:

The J loop is contolling the row output

for(int j = 1; j<=i; j++)


As written it starts at 1 and increases to i. Change it to start at i and decrease to 1.


Thank you very much. I couldn't imagine it was so simple. Thank you, once again. Long live programming. LOLZ
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1