Jump to content


C++ Graphic Diamond

Programming

  • Please log in to reply
6 replies to this topic

#1 Positive Tension

Positive Tension

    Member

  • Posts:865
  • Joined:08-September 05
  • RS Status:Retired

Posted 25 September 2008 - 06:11 PM

I'm my computer programming class we have to create this program

Quote

For this program you are to prompt the user for an integer and for a character.  The output will be a diamond composed of the character and extending the width specified by the integer.  For example, if the integer is 5 and the character is an asterisk (*), the diamond will look like this:
    *
  ***
*****
  ***
   *

Because a diamond must have an odd number for its width, if the user enters an even number, it should be increased to the next odd number.  You will have multiple loops inside of loops and lots of counters; comment thoroughly to help keep track of things.

//Program #11 Graphic Diamonds
 
//include interfaces
#include 
 
// constants
 
//main
void main()
{	
	int num; //reserving memory
	char dichar; //reserving memory
	cout << " This program will create a graphic diamond using your chosen character and number. " << endl;
	cout << " Please enter an integer -> " << endl;
	cin>> num; //inputs the user's chosen number
	cout << " Please enter a character -> " << endl;
	cin>> dichar; //inputs user's chosen character 
}

I emailed the code to myself but a large part of it got cut off.  I'll edit this post tomorrow but I cannot for the life of me figure out how to get this program to work correctly. If anyone has any ideas I'd love to hear them, thanks!

Edited by Positive Tension, 25 September 2008 - 06:12 PM.


#2 Lunchbox

Lunchbox

    bow bow bow.

  • Posts:450
  • Joined:17-October 07
  • RS Name:BEEN HAD EXP
  • RS Status:Member

Posted 25 September 2008 - 06:55 PM

I don't see why you would need many loops.
Couldn't you just use ceil to get the odd number?
If they enter 4 then just use ceil and it will get 5, which is an odd number.
You could always set a lot of reserv int's and just keep subtracting two until it hits 1, then display them.

#3 No1

No1

    Disaster Control Ward

  • Posts:15,759
  • Joined:24-December 02
  • RS Name:No1 1000
  • RS Status:Member
  • Clan:Zybeznet

Posted 25 September 2008 - 07:02 PM

As far as I can see you would only really need TWO loops... one for each row and one to put together the line of characters.

Something to consider: Is the number of lines always equal to the value entered? If so, that would make things much simpler.

Do you have to do spacing from the left to actually create a diamond, or are all the characters left-aligned? that would make things a little tricky.


You know that the first row and final row will both be zero, and each row will be either two more than or two less than the previous one... Do a loop for each line less than the number inputted [doing the error check first], start at one and keep adding two each time until you pass halfway through the number entered, or when the number of characters equals what they entered. Either would work. From there, subtract two with each loop until the number gets back to 1 [or line number equals entered number].

Not really that difficult of a program.
"He attacked everything in life with a mix of extraordinary genius and
naive incompetence, and it was often difficult to tell which was which."

- Douglas Adams (Hitchhiker's Guide to the Galaxy)

#4 Positive Tension

Positive Tension

    Member

  • Posts:865
  • Joined:08-September 05
  • RS Status:Retired

Posted 25 September 2008 - 08:09 PM

View PostNo1 1000, on Sep 25 2008, 08:02 PM, said:

As far as I can see you would only really need TWO loops... one for each row and one to put together the line of characters.

Something to consider: Is the number of lines always equal to the value entered? If so, that would make things much simpler.

Do you have to do spacing from the left to actually create a diamond, or are all the characters left-aligned? that would make things a little tricky.


You know that the first row and final row will both be zero, and each row will be either two more than or two less than the previous one... Do a loop for each line less than the number inputted [doing the error check first], start at one and keep adding two each time until you pass halfway through the number entered, or when the number of characters equals what they entered. Either would work. From there, subtract two with each loop until the number gets back to 1 [or line number equals entered number].

Not really that difficult of a program.

first, thanks to both of you, i think i get it now.

it's not really a difficult program for you, but the first time i'd ever seen C++ was less than a week ago haha, i think i'm getting the hang of it now, we'll see.

#5 Curtains

Curtains

    WAKE UP

  • Posts:841
  • Joined:02-August 03
  • RS Name:Conker7
  • RS Status:Retired

Posted 28 September 2008 - 12:43 AM

Since I don't know C++, I wrote this in java, but you should be able to understand what is going on...

So the two parameters you would pass in would be the maximum width of the diamond and the character that you want it to draw.

public class Diamond
{
	public static void main(int _width, String _character)
	{
		// Variables
		int width = _width; // The maximum width of the diamond
		String character = _character; // The character you want to draw
		int drawCount = 1; // Counter that keeps track of how many "characters" to print out
		boolean middleDrawn = false; // Keeps track of whether or not the middle line has been drawn
		
		// Repeat as many times as the maximum width of the diamond
		for (int i = 0; i < width; i++)
		{			
			// Draw the character as many times as the counter 
			for (int j = 0; j < drawCount; j++)
			{
				System.out.print(character);   
			} 
			
			// If the middle line has been draw, set the flag variable to true
			if (drawCount == width)
			{
				middleDrawn = true;
			}   
			
			// If the middle line hasn't been drawn, increase the counter by 2
			if (middleDrawn == false)
			{
				drawCount+=2;
			// Otherwise, decrease the counter by 2
			} else {
				drawCount-=2;
			}
			
			// Draw a new line
			System.out.println("");
		}
	}
}

Signed,
Alex


#6 Veracon

Veracon

    it's back! (in its original version!)

  • Posts:2,959
  • Joined:27-May 04
  • RS Status:Free

Posted 28 September 2008 - 05:36 AM

num = input('Number: ')
num = num if num % 2 else num + 1
print '%s\n%s' % ('\n'.join('*' * i for i in range(1, num + 1, 2)), '\n'.join('*' * i for i in range(num - 2, 0, -2)))

There's a Python equivalent. Couldn't resist.

Edited by Veracon, 28 September 2008 - 05:36 AM.

Posted Image

#7 Curtains

Curtains

    WAKE UP

  • Posts:841
  • Joined:02-August 03
  • RS Name:Conker7
  • RS Status:Retired

Posted 28 September 2008 - 10:01 AM

View PostVeracon, on Sep 28 2008, 06:36 AM, said:

num = input('Number: ')
num = num if num % 2 else num + 1
print '%s\n%s' % ('\n'.join('*' * i for i in range(1, num + 1, 2)), '\n'.join('*' * i for i in range(num - 2, 0, -2)))

There's a Python equivalent. Couldn't resist.
Don't you just love that modular arithmetic! :P

Signed,
Alex





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Font:
Arial | Calibri | Lucida Console | Verdana
 
Font Size:
9px | 10px | 11px | 12px | 10pt | 12pt
 
Color: