This topic is 3772 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!!

• 37
• 12
• 10
• 10
• 9
• Forum Statistics

• Total Topics
631360
• Total Posts
2999554
×