GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 4.50 average.
  #1  
Old 21-Apr-2006, 05:49
Zayne Zayne is offline
New Member
 
Join Date: Apr 2006
Posts: 7
Zayne is on a distinguished road
Smile

Print diamond shaped star pattern


I need help to design the following C++ programs!
Please help me with the following:

1)Write a program to design the otput in a diamond shape of star symbols.
When you run this program, the output must be like so:

How many stars you want in the middle: 3
*
* *
* * *
* *
*

2)Write a program to change the numeric value into words from ZERO to TEN THOUSAND.
When you run this program, the output must be like so:

Enter the value: 999
NINE HUNDRED AND NINTY NINE

>THANX<
Last edited by LuciWiz : 21-Apr-2006 at 10:11. Reason: Edited title; was "Need Help With 2 Programs!!!"
  #2  
Old 21-Apr-2006, 05:50
Zayne Zayne is offline
New Member
 
Join Date: Apr 2006
Posts: 7
Zayne is on a distinguished road
Exclamation

Re: Need Help With 2 Programs!!!


For some reason the shape for question 1 looks like a Triangle in the first post... It shoul be a diamond please!
  #3  
Old 21-Apr-2006, 05:54
Zayne Zayne is offline
New Member
 
Join Date: Apr 2006
Posts: 7
Zayne is on a distinguished road
Exclamation

Re: Need Help With 2 Programs!!!


Oh and sorry... i posted this in C++! But it should be made on MS VISUAL C++
  #4  
Old 21-Apr-2006, 06:04
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,037
LuciWiz is just really niceLuciWiz is just really niceLuciWiz is just really niceLuciWiz is just really nice

Re: Need Help With 2 Programs!!!


Senior members might not reply to this thread if you:
  • used "Please help", "C problem" or something equally vague as the title for your thread.
  • included example code in your message and not use the syntax highlighting [c] tags.
  • did not describe the problem accurately or include the error message (if any).
  • tried to get your homework assignment solved by someone else (and did not show your attempts at solving the problem)
The guidelines for posting can be found here.

We encourage you to post again once you've done that.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
Last edited by LuciWiz : 21-Apr-2006 at 07:26.
  #5  
Old 21-Apr-2006, 06:44
Zayne Zayne is offline
New Member
 
Join Date: Apr 2006
Posts: 7
Zayne is on a distinguished road

Re: Need Help With 2 Programs!!!


I tried the following code for (1) but it only works with odd numbers and i can't get the bottom star to show:
CPP / C++ / C Code:
#include <iostream>
using namespace std;
void print (int rowsize);
int main()
{
	int rowsize;
	do{
		cout<<"Please type in a positive odd integer: ";
		cin>>rowsize;
	}while((rowsize%2==0)||(rowsize<=0));
	print(rowsize);
	return (0);
}
void print(int rowsize)
{
	int spaces=rowsize/2, currentsize=1, counter, count;
	for(counter=0;counter!=(rowsize+1)/2;counter++){
		for(count=spaces;count!=0;count--)
			cout<<" ";
		spaces--;
		for(count=currentsize;count!=0;count--)
			cout<<"*";
		currentsize+=2;
		cout<<endl;
}
currentsize=rowsize-1;
spaces+=2;
for(counter=rowsize/2;counter!=0;counter--){
	for(count=0;count!=spaces;count++)
		cout<<" ";
	spaces++;
	for(count=currentsize;count!=0;count--)
		cout<<"*";
	currentsize-=2;
	cout<<endl;
	}
}
Last edited by cable_guy_67 : 21-Apr-2006 at 06:50. Reason: READ the GUIDELINES and Please use Code Tags
  #6  
Old 21-Apr-2006, 09:00
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: Need Help With 2 Programs!!!


The problem isn't that for odd numbers that it wasn't showing the bottome star... It was just printing 1 too many stars at the end of each line in the bottom half of your diamond:

Code:
* *** ***** ******* ****** **** **

The fix is a one liner... in between the top half and the bottom half, you do a little maintainence... because you don't want the middle row printing out twice. So, the bottom half would start out printing 2 less stars than the row above it. Here's what you have:

CPP / C++ / C Code:
currentsize=rowsize-1;
spaces+=2;

You only compensated for 1 less star (since currentsize is going to be used to determine the number of times through the * loop in the bottom half. What you needed to do was subtract 2 from rowsize to account for the 2 less stars in that row (as opposed to 1):

CPP / C++ / C Code:
      currentsize=rowsize-2;
      spaces+=2;

Once you do that, it's easy sailing... and the program seems to work fine:

Code:
* *** ***** ******* ***** *** *

Also, on a side note... alot of your loops used != in their conditional parameter. This is NOT a good practice to get into... especially when it isn't exactly what you want. Here is an example... your loop:

CPP / C++ / C Code:
for(counter=0;counter!=(rowsize+1)/2;counter++) {

The counter!=(rowsize+1)/2 is really not what you want. You want this...

CPP / C++ / C Code:
for(counter=0; counter<=rowsize/2; counter++){

You just want to loop until the counter is less than or equal (hence the <= sign) to rowsize/2. Or in this example ... your code:

CPP / C++ / C Code:
for(count=currentsize;count!=0;count--)

You should use this:

CPP / C++ / C Code:
// Much safer and easier to read
for(count=currentsize; count>0; count--)

Besides, that's all you are wanting anyways. You are wanting to loop while count is greater than 0... so write it that way

Hope that helped,
Trey
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
  #7  
Old 21-Apr-2006, 10:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need Help With 2 Programs!!!


Quote:
Originally Posted by TreyAU21
The problem isn't that for odd numbers that it wasn't showing the bottome star... It was just printing 1 too many stars at the end of each line in the bottom half of your diamond:

Once you do that, it's easy sailing... and the program seems to work fine:

Code:
* *** ***** ******* ***** *** *

But the requirement was more like this (according to the original post):

Code:
How many stars you want in the middle: 3 * * * * * * * * *

My suggestion is to create a few cases (with a text editor) and see what is required before writing the code. Is it something like the following:

Code:
n = 3: * * * * * * * * * n = 4: * * * * * * * * * * * * * * * * n = 5: * * * * * * * * * * * * * * * * * * * * * * * * *

Note that the results give symmetrical results for even and odd numbers, so it is reasonable to attempt a program that covers all positive integers. (Whereas the kind of diamond in your solution doesn't really work for even integers, which is why the main() program only accepted odd integers, I guess.) Of course, since we weren't told what the assignment really is, I can't say what we are really supposed to do.

Now if my examples show what the program is supposed to do, look at the results: I see the problem as:

1. Each row has a certain number of spaces, followed by a certain number of "* " sequences.

2. If we are told how many stars are on the center row, how can we know how many rows will be required?

3. How can we know how many spaces and how many "* " sequences are required on each row?

Now, you might look at it from another point of view that would lead to a program that is different from mine but would give the same results. Maybe your way is superior (from the context of your experience and your ability to visualize). The point is: look at the problem; look at the desired output; try to visualize a way to make a program create those results. Write down what it is that you have to do to complete the program. What are the steps? What do you have to know at each step?

Then write the code. (Do I need while() loop(s) or for() loop(s)? How many loops do I need? These are implementation details that can follow after you have decided on a logical sequence for printing each row.)

Regards,

Dave

Richard Feynman's methodology for problem solving:

1. Look at the problem.
2. Think real hard.
3. Write down the solution.

---Murray Gell-Mann (From a New York Times interview)
  #8  
Old 23-Apr-2006, 13:00
Zayne Zayne is offline
New Member
 
Join Date: Apr 2006
Posts: 7
Zayne is on a distinguished road

Re: Print diamond shaped star pattern


Thank u for ur help...
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guestbook error BobbyDouglas Web Design Forum 1 16-Oct-2003 22:17

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:17.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.