This topic is 3655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have to output a diamond. But first I am trying to output this... diagonal length: 9 Character: * * *** ***** ******* ********* *********** ************* *************** ***************** My code..
#include <iostream>
using namespace std;

//Introduction to Functions
int GetLength ();
char GetCharacter ();
void PrintChars (int m, char ch);

int main()
{
int dLength = 0;
char symbol = '*';

dLength = GetLength();
symbol = GetCharacter();
PrintChars (dLength, symbol);

return 0;
}

int GetLength ()
{
int dLength = 0;

cout << "Please enter the diogonal Length: ";
cin >> dLength;

return dLength;
}

char GetCharacter ()
{
char character = '*';

cout << "Enter a character for the diamond: ";
cin >> character;

return character;
}

void PrintChars (int m, char ch)
{
for (int i = 0; i < (m+1); i++)
{
for (int a = 0; a < i; a++)
{
cout << ch;
}
cout << endl;
}
}


Please point me in the right direction. Thanks!!!

##### Share on other sites
So what exactly is the problem?

BTW, you can make the program a bit simpler by creating a new function PrintLine(ch, count) that prints a line of 'ch' characters of length 'count'. This will make the PrintChars function a bit simpler.

##### Share on other sites
The problem is it print this...
*
***
*****
*******
*********
*******
*****
***
*
but first i am trying to print this..

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

thanks!

##### Share on other sites
Your program does exactly what you want. Are you sure you're using the code you posted? (I noticed you edited your post and removed the while loop)

##### Share on other sites
Actually my program does this..
*
**
***
****
*****
******
*******
********
*********

Not this..
*
***
*****
*******
*********
***********
*************
***************
*****************

##### Share on other sites
Oh I got closer to what I want. I get this now...
*
***
*****
*******
*********
***********
*************
***************
*****************
*********

any idea how I get rid of the last row?
Thanks a lot!!

#include <iostream>using namespace std;//Introduction to Functionsint GetLength ();char GetCharacter ();void PrintChars (int m, char ch);int main(){	int dLength = 0;	char symbol = '*';		dLength = GetLength();	symbol = GetCharacter();	PrintChars (dLength, symbol);		return 0;}int GetLength (){	int dLength = 0;		cout << "Please enter the diogonal Length: ";	cin >> dLength;		return dLength;}char GetCharacter (){	char character = '*';		cout << "Enter a character for the diamond: ";	cin >> character;		return character;}void PrintChars (int m, char ch){	for (int i = 0; i < (m+1); i++)	{		for (int a = 0; a < i; a++)		{			cout << ch;		}		cout << endl;		for (int l = 0; l < i; l++)		{			cout << ch;		}	}}

##### Share on other sites
Sorry, I didn't notice that the lines grow by two characters and not by one.

Notice how there's a pattern to the number of characters in a line:

line 1 has 1 character.
line 2 has 3 characters.
line 3 has 5 characters.
line 4 has 7 characters.
etc...

You can figure out the equation that gives the number of characters given a line number, and use that as the upper limit of the inner loop.

##### Share on other sites
So each row needs two extra characters instead of one?
In that case try this:

void PrintChars (int m, char ch){  // iterate over [0, m) (not m + 1), you want to do this m times and you start at 0  for (int i = 0; i < m; ++i)  {    // calculate the length of the current line    int length = 1 + i * 2;    // create a string with this length    string line(length, ch);    // output the string and end the line    cout << line << endl;  }}

This way you also avoid the innerloop, which is just a bit faster and it gives nicer (easier to read) code.

##### Share on other sites
First, this is probably a homework question, so you are not allowed to just post a solution. It's also more educational for the OP if he figures it out for himself.

Second, the main point of this exercise is to practice loops, and by using std::string, you're bypassing that completely.

##### Share on other sites
^^^ Thanks a lot man!! but can you please tell me a little bit more about.. string line(length, ch);?
thanks!!

##### Share on other sites
Quote:
 Original post by musafir2007^^^ Thanks a lot man!! but can you please tell me a little bit more about.. string line(length, ch);?thanks!!

Sure... it creates a string containing 'length' characters 'ch'.
So one of the constructors of a string is something like this:

string::string(int n, char ch);

Which means you can give two arguments, the first is the length of the string and the second is the character used to fill the string.

##### Share on other sites
Quote:
 Original post by Gage64First, this is probably a homework question, so you are not allowed to just post a solution. It's also more educational for the OP if he figures it out for himself.Second, the main point of this exercise is to practice loops, and by using std::string, you're bypassing that completely.

Sorry, I didn't know about that rule, thanks for pointing it out. I can see your point about being it a homework question. About your second point, if it isn't a homework question my solution would be nicer.

Anyway, if it is a homework question about loops, then the topic starter can figure out for himself how the innerloop should be constructed.

##### Share on other sites
Quote:
 Original post by TheFlyingDutchmanThis way you also avoid the innerloop, which is just a bit faster and it gives nicer (easier to read) code.

Well, you avoid writing the inner loop. It's still there, in the std::string constructor (or, more likely I suppose, in a function it calls – probably std::fill), so the performance (which is something of a non-issue here) won't be significantly different.