Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,501 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,373 people online right now. Registration is fast and FREE... Join Now!




creating a pattern with 'while' and 'if' loops

 
Reply to this topicStart new topic

creating a pattern with 'while' and 'if' loops

nirovanton
24 Feb, 2007 - 11:20 AM
Post #1

New D.I.C Head
*

Joined: 12 Feb, 2007
Posts: 12


My Contributions
i need to make a code that takes an input integer value >=1 and makes a pattern with it, i cannot create the loops i need, have been on this forever and cannot think of correct code.

CODE

for example:
x=3;
*
**
***
***
**
*

x=5;
*
**
***
****
*****
*****
****
***
**
*


This has been driving me crazy, i know it can't be that hard but im just to brain dead to think about it anymore all i have is this

CODE

#include <iostream>
#include <cstdlib>

using namespace std;

int main( )
  {
int x, n=0;

while(n<x && n!=x)
  {
    n++;
    cout<<"*";
  }


all my following code sends the program into chaos please help me!
User is offlineProfile CardPM
+Quote Post

IainMackay85
RE: Creating A Pattern With 'while' And 'if' Loops
24 Feb, 2007 - 11:41 AM
Post #2

New D.I.C Head
*

Joined: 7 May, 2005
Posts: 44


My Contributions
CODE

int n,y,z;
printf("x=");
cin("%d",n);
for z = 0; z<=n z++;{
for y = 0; y<=z y++;{
  cout<<"*";
}
cout<<"\n";
}
for z = n; z>0; z--;{
for y = 0; y<=z y++;{
  cout<<"*";
}
cout<<"\n";
}

i dont know if thats exactly correct but you just need to change some numbers, its been ages since i programmed in C


User is offlineProfile CardPM
+Quote Post

nirovanton
RE: Creating A Pattern With 'while' And 'if' Loops
24 Feb, 2007 - 12:51 PM
Post #3

New D.I.C Head
*

Joined: 12 Feb, 2007
Posts: 12


My Contributions
[quote name='IainMackay85' post='205357' date='24 Feb, 2007 - 12:41 PM']
CODE

int n,y,z;
printf("x=");
cin("%d",n);
for z = 0; z<=n z++;{
for y = 0; y<=z y++;{
  cout<<"*";
}
cout<<"\n";
}
for z = n; z>0; z--;{
for y = 0; y<=z y++;{
  cout<<"*";
}
cout<<"\n";
}

i dont know if thats exactly correct but you just need to change some numbers, its been ages since i programmed in C

I was wondering, is this code even possible using only 'while' and 'if' functions? I got nowhere with them, i would just like to know if i was very close.. thanks for your help man it works great.

This post has been edited by nirovanton: 24 Feb, 2007 - 12:58 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Creating A Pattern With 'while' And 'if' Loops
24 Feb, 2007 - 03:13 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
QUOTE(IainMackay85 @ 24 Feb, 2007 - 12:41 PM) *

CODE

int n,y,z;
printf("x=");
cin("%d",n);
for z = 0; z<=n z++;{
for y = 0; y<=z y++;{
  cout<<"*";
}
cout<<"\n";
}
for z = n; z>0; z--;{
for y = 0; y<=z y++;{
  cout<<"*";
}
cout<<"\n";
}

i dont know if thats exactly correct but you just need to change some numbers, its been ages since i programmed in C


smile.gif I think we will have to call that psudocode.

Lets break the problem up a bit. Given a value n how do you print n *'s?

CODE
for(int i=0; i<n; i++) { cout << '*'; } cout <<'\n";

would do that nicely.
Now all we need to do is count from 1 to x, and then from x to 1 in a loop using the above code to print out the bar-graph.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month