4 Replies - 3789 Views - Last Post: 27 June 2012 - 04:17 AM Rate Topic: -----

#1 leam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-June 12

Nested loop Structures Semi-Pyramid

Posted 25 June 2012 - 01:34 AM

hello C++ users, im in need of help regarding with the nested loop.

here's the question..

Write a program that will display the pattern below depending on the value of n.

Sample output:

Enter a value of n: 4

output:

*
**
***
****
***
**
*

i need ur response
Ive tried answering it and here’s my work:

 

#include<iostream>
using namespace std;
main()

{
      int a;
      cout<<"Enter a value of n: ";
      cin>>a;
      cout<<endl;
      
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=1;y++)
      cout<<"*";
      cout<<endl;
      }
      
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=2;y++)
      cout<<"*";
      cout<<endl;
      }
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=3;y++)
      cout<<"*";
      cout<<endl;
      }
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=4;y++)
      cout<<"*";
      cout<<endl;
      }
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=3;y++)
      cout<<"*";
      cout<<endl;
      }
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=2;y++)
      cout<<"*";
      cout<<endl;
      }
      for(int x=1; x<=1; x++)
      {
      for (int y=1;y<=1;y++)
      cout<<"*";
      cout<<endl;
      }
      
      
      
      system("pause");
      return 0;
} 



Sad to say it’s still wrong.

This post has been edited by no2pencil: 25 June 2012 - 01:39 AM
Reason for edit:: Corrected code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Nested loop Structures Semi-Pyramid

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Nested loop Structures Semi-Pyramid

Posted 25 June 2012 - 02:53 AM

11 for(int x=1; x<=1; x++)
12 {
13 for (int y=1;y<=1;y++)
How do these involve the input value stored in a?

If you input 4 say, then try just printing
1
2
3
4
3
2
1

When you can do that, it should be evident how to print that many stars.
Was This Post Helpful? 0
  • +
  • -

#3 leam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-June 12

Re: Nested loop Structures Semi-Pyramid

Posted 27 June 2012 - 12:22 AM

what i want to know is how to write a program that will display the output of semi-pyramid depending on the number inputted.. like for example. if i input 4


the output must be like this.

Enter the value of n: 4

*
**
***
**** ----> 4 asterisks
***
**
*

so the maximum number of asterisks there is 4 which can be found at the center part of the pyramid or the one that divides the pyramid into 2.

and if input 3, the pyramid must be decrease in size.. like this

*
**
*** ---> 3 asterisks
**
*




i need your response.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Nested loop Structures Semi-Pyramid

Posted 27 June 2012 - 01:03 AM

Yes, and I gave you a learning exercise to try, which would lead you towards your answer.
Was This Post Helpful? 0
  • +
  • -

#5 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Nested loop Structures Semi-Pyramid

Posted 27 June 2012 - 04:17 AM

Oh, and saying "i need your response" does not make us any eager to answer your question. Try out the exercise which Salem_C gave, and then you will get the idea of making the program you want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1