loops asterisks

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 19823 Views - Last Post: 26 January 2011 - 05:39 AM Rate Topic: -----

#1 Guest_mich*


Reputation:

loops asterisks

Posted 24 January 2011 - 04:17 PM

...please help!!
im supposed to have an output like this
    *
   **
  ***
 ****
*****

notes:(_)means spaces
but my result is different
heres my code:
 #include<stdio.h>
 main(void)
{
 for(int x=1;x<=5;x++)
 {
  for(int y=5;y>x;y--)
   printf("\t");
  {
    for(int z=1;z<=y;z++)
    printf("*");
   printf("\n");
  }
}
  getch();
}



..thank you..

This post has been edited by JackOfAllTrades: 24 January 2011 - 04:26 PM
Reason for edit:: Put pattern in code tags


Is This A Good Question/Topic? 0

Replies To: loops asterisks

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: loops asterisks

Posted 24 January 2011 - 04:24 PM

What's the pattern that you can see? :)
Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: loops asterisks

Posted 24 January 2011 - 04:29 PM

should be int main(), not just main() ... Also, what output are you getting?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: loops asterisks

Posted 24 January 2011 - 04:29 PM

Why are you using a tab \t character?
Was This Post Helpful? 0
  • +
  • -

#5 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 24 January 2011 - 08:28 PM

View PostJackOfAllTrades, on 24 January 2011 - 04:29 PM, said:

Why are you using a tab \t character?


I was wondering the same thing. I have almost the same homework assignment as mich there and have been working on it, but haven't gotten to the point of making it pretty.

Mich ~ Just so you know, you and I are on the same page...apparently our first programming class...so there's not much I can do to help you except ask some questions.

1. Why are you using getch? I've been reading up on different forums and see most everyone saying cin.get or cin.ignore are better ways of keeping your window open. In my class, we never even thought of getch.

2. I see that you are trying to right-align your stars. Is there a format character for right-align? I'll bet there is, though we haven't gotten much into printf in my class except a brief discussion about how much easier standard cin and cout functions are. (The prof wants us to do everything as simply as possible even if it means more work.)

My homework is a little bit different from Mich's in that a) the user gets to decide how many rows of stars are in our tree; B) the rows of stars are to be centered so it looks like an actual tree instead of half a tree; c) the number of stars in each line is equal to the (line number x 2) - 1; and d) each row is to be numbered.

I'm going to post my code here and then ask a question if it's ok to hijack Mich's thread instead of starting a new thread on the same topic. Please bear in mind that I borrowed a few lines of code that I found online as a starting point and have been modifying it as I go along. Also, there are a couple things I don't understand yet but am trying to learn as I work through this. And... I've only actually worked on it for about 2 hours; the rest of my time has been spent researching possible solutions. Also..there's tons of comments there because that's how I think through a problem.

#include <iostream>
using namespace std;
int main (void)
{
	const int MIN_LINES = 1; // the minimum number of lines allowed
		const int MAX_LINES = 30; // the maximum number of lines allowed
		int desiredLines;


int i; //an index to use in the loop's conditional expression.  
int line; 
cout << "Please enter the number of lines you would like in your tree." << endl;
cin >> desiredLines;
//Going to need an if else statement here to compare the input to min and max lines.
//Can we compare to two different constants or variables at the same time?
for (i=1; i<=desiredLines; i++)//This counts the number of lines executed until the
//index reaches the desired number of lines.
{
for (line=1; line<=i; line++)//This outputs the stars but it's not the right formula. 
//The number of stars should
//be equal to the line number plus the previous line number.

cout << "*"; //How do I get the line numbers at the beginning of each line of stars?
//putting an i before the asterisk alternates the line number and a star all the way 
//across the line.
cout<< endl;
}

system("pause");
return 0;
}



Ok, question 1: The system("pause"); was in the code I borrowed. I haven't seen reference to that yet in my textbook or class discussions. Can someone please tell me what it does? If it's not necessary or if it just holds the window open, I'd like to replace it with cin.ignore(2) which is what I have been using for the past three weeks. (I promise, that's the only question I will request an outright answer for.)

Question 2: The next most important thing for me to do is get the right number of stars in each line. (I'm already getting the right number of lines.) Is it possible to use a formula in this line: for (line=1; line<=i; line++)? I guess I'm just not comprehending exactly how that line functions or I would know the answer. what I want to do is print (line*2)-1 number of stars on each line. Suggestions? General idea of what I should search for?

Thanks, and sorry if I was out of line asking so much.
Was This Post Helpful? 0
  • +
  • -

#6 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 24 January 2011 - 08:37 PM

Wow....the code editor here doesn't like what I pasted. It doesn't seem to recognize my comments as comments. Also, I'm not getting any errors, but just remembered the "getting started" thread or whatever it was (you know, the one that says "I can do your homwork for you!") said we should tell what compiler we are using. I'm using Visual C++ 2008 Express Edition. It works well and was free. :dots:
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: loops asterisks

Posted 25 January 2011 - 05:23 AM

Question 1: You should not use system("pause"): Here's why.

Question 2 is the meat of your assignment, so don't expect a lot of help. The idea is for YOU to think about the algorithm needed to create that output. Read the first entry in this blog for some hints.
Was This Post Helpful? 2
  • +
  • -

#8 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 25 January 2011 - 06:00 AM

Thanks for the links, Jack. I've replaced the system pause with cin.get and got an error, so went back to cin.ignore. Also, I'm understanding a little better how the nested loops work. I've managed to get spaces in front of my stars. The pattern is all wrong and now the number of lines is also wrong, but at least I have an idea of where I messed up. In this case, it's all in the equations I used to automatically calculate the number of each. Guess it's back to the drawing board. :)

Once I get this right...or even mostly right...I'll post the code. It's going to be a fun little program that does absolutely nothing but seems to be an excellent learning experience.

:yes:
Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: loops asterisks

Posted 25 January 2011 - 07:26 AM

I am so glad to see someone that actually WANTS to learn, not just have the code handed to them.

What error did you receive with cin.get()? We should fix that, as it should not be causing an error.
Was This Post Helpful? 1
  • +
  • -

#10 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 25 January 2011 - 05:47 PM

I'd better be interested in learning, Jack. I'm paying $2000 per class for my engineering degree...and there are a LOT of classes. :eek:

I can't remember exactly what the error was, so I went back and put cin.get() back in and tried to build. The build went smoothly, with no errors. When I did it before, the build failed. It was the cin.get that was flagged and it said something about...well, I'm not sure...calling a function...or not calling a function. Anyway, I think I know what I did wrong. I think I typed
cin.get
instead of
cin.get()
.

:offtopic: Is there a different flag to use when highlighting just a word or two? I clicked the link to get the BBCode references, but that page won't load.

Back on topic: I haven't slept much over the past couple of days because of this assignment. Fortunately, I had a slow day at work though and got this crazy idea that I should quit trying to work with code or even pseudocode and go all the way back to a flowchart. It's amazing how simple life becomes when you have a PICTURE of what your loops are really supposed to look like. The assignment requires that we use FOR loops so they won't be written exactly like the flowchart, but I still have a very good grasp of everything that goes INSIDE each loop.

So...Mich...if you're still out there watching this thread....try doing a flowchart to get a handle on all your options, requirements, loops, etc. You'll find that it's much easier to write the code once you know what the problem actually is. :yes:

-Mel

This post has been edited by honeybeemel1969: 25 January 2011 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -

#11 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 25 January 2011 - 07:44 PM

Where did my comment go? :cry: I lost it. It was eaten by the board. So, I'm just going to say this one more time before I actually try to run my program.

I think I did it! I got it to compile, at least. I used multiple if's, while's and for's as well as a do_while. I got the do_while to accept that the chars Y and y are both true and that they are ***not*** undeclared variables.

I have pages upon pages of worthless pseudocode. I have lists of unneeded, unused variables. I have multiple formulas that just weren't right. That flowchart really did the trick. Now, wish me luck. :clover: I'm going to try to actually run and test this program. If it works, I'll be back to tell you what I learned. If it doesn't, you'll see a 10 year old monitor flying through your backyard. :P
Was This Post Helpful? 0
  • +
  • -

#12 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 25 January 2011 - 09:33 PM

:w00t: How's this?

#include <iostream>
#include <iomanip>

using namespace std;
int main (void)
{

const int MIN_LINES = 1;
const int MAX_LINES = 30;
int lineNumber, desiredLines, starsCount, spaceCount;
char newTree;
bool doOver;

do
{ //opening brace for do...while loop.

cout << "Please enter the number of lines you would like in your tree." << endl;
cin >> desiredLines;

if (desiredLines < MIN_LINES)
{
	cout << "Please enter an integer greater than zero." << endl;
	cin >> desiredLines;
}
if (desiredLines > MAX_LINES)
{
	cout << "The maximum number of lines is 30." ;
	cout << "Your tree will have thirty lines." << endl;
	desiredLines = 30;
}
for (lineNumber = 1; lineNumber<=desiredLines; lineNumber++)
{ //beginning bracket for the lineNumber FOR loop.
cout << setw(2) << lineNumber ;
for (spaceCount = desiredLines - lineNumber + 1; spaceCount>0; spaceCount--)
	cout << " " ; //should print spaces on the same line as the line number
for (starsCount = lineNumber*2-1; starsCount>0; starsCount--)
	cout << "*" ; //should print stars right behind the previous spaces
cout << endl;
} //ending bracket for the lineNumber FOR loop.

if (lineNumber>desiredLines)
{ //beginning brace for if statement.	
	cout << "Would you like to make another tree?  Please type Y or N." << endl;
	cin >> newTree;
} //ending brace for if statement.

if (newTree != 'Y')
	doOver = false;
else
	doOver = true;
	

} //ending brace for do...while loop.
while (doOver == true);
cout << endl;
cout << "Thank you.  Have a nice day." << endl;

cin.get();
return 0;
}//ending brace for main



It compiled. It tested. It worked! There is just one problem, though. It's wrong. I am, somehow, supposed to make the whole thing loop if the user enters either Y or y when asked if they want to make another tree. I have tried using

if (newTree == 'Y'||'y')
doOver = true;
else
doOver = false;


as well as plenty of variations on that theme, but the OR doesn't seem to be recognized in this case. I believe with this one, I got an infinite loop where I was constantly being asked for the number of lines no matter what I pressed in answer to the newTree question. There was another instance, and I don't remember what I did (I really should learn to take notes, huh?) when no matter what I pressed, including Y, I was asked to have a nice day and to press any key.
So, could someone please point me in the right direction so I can figure out how to make both Y and y begin a new tree while any other character exits the loop?

By the way, the "Thank you. Have a nice day." is only there so I could tell where the program was exiting.
Was This Post Helpful? 0
  • +
  • -

#13 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: loops asterisks

Posted 25 January 2011 - 09:49 PM

Your source looks close to that of mine.

Is this any good?:
#include <iostream>

int main()
{
	int j = 0;
	for(int i = 5; i > 0; i--)
	{
		for(j = 0; j < i; j++)
		{
			std::cout << " ";
		}
		for(int z = 5; z >= i; z--)
			std::cout << "*";
		std::endl(std::cout);
	}
	std::cin.get();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#14 Crutoy  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 70
  • Joined: 10-January 11

Re: loops asterisks

Posted 25 January 2011 - 09:58 PM

this is what i came up with.
#include <iostream>


using namespace std;

int main(){

	int nRow, nColumn, nAstrics = 44;
	
	for ( nRow = nAstrics; nRow >= 0; nRow-- ){

		for ( nColumn = 0; nColumn <= nAstrics; nColumn++ ){

			//determines how when blanks are added
			if ( nAstrics - nRow < nAstrics - nColumn )
				cout << ' ';
			else
				cout << '*';

		} //end columns

		cout << endl;

	} //end rows


	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#15 honeybeemel1969  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 24-January 11

Re: loops asterisks

Posted 25 January 2011 - 10:05 PM

View PostSlumdog, on 25 January 2011 - 09:49 PM, said:

Your source looks close to that of mine.

Is this any good?:
#include <iostream>

int main()
{
	int j = 0;
	for(int i = 5; i > 0; i--)
	{
		for(j = 0; j < i; j++)
		{
			std::cout << " ";
		}
		for(int z = 5; z >= i; z--)
			std::cout << "*";
		std::endl(std::cout);
	}
	std::cin.get();
	return 0;
}


well, I notice that you don't declare the variable i. does the FOR statement also declare the variable? If so, that's way cool and might save me a step or two.

Aside from that, it looks like your code will print 5 spaces and one star on the first line, four spaces and two stars on the second line, etc. I didn't enter and compile your code, but it seems that it would produce a right-aligned right triangle pattern. Is that what you got?

Recall that my assignment was a little more complex than that so my code is also going to be more complex. One question.... why do you use the std:: format instead of just using namespace std?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2