-
C++ beginner's problem
I need to write a program that inputs an integer and a character, and outputs a diamond composed of the character and extending the width specified by the integer. I can only use 'while' loops at this stage of the course, and I know it has to be nested 'while' loops. I can not use 'for/next' loops. I have done this before in VB6, so I am somewhat exasperated that I can not even get past firstbase with this same problem in C++. And I like C++, strangely enough. I know I would be better off thinking this out for myself, but at this point, time is of the essence and I need help fast.
Any suggestions would be greatly appreciated.
-
diamond like this?
a, 5
a
aa
aaa
aaaa
aaaaa
aaaa
aaa
aa
a
not quite sure on ur specs..
it cut out the leading spaces...but, you get the idea..
-
Yes, but with the leading spaces, so that using your letter and number, it would be 5 rows, 5 wide:
a
aaa
aaaaa
aaa
a
The leading spaces is what is throwing me: the leading spaces decrease and the a's increase.
(yeh, this system is throwing out the leading spaces)
-
Senior Member
number of space will be equal to integer entered - number of current a's divided by 2
(i-a+1)/2
The +1 takes care of the case of the integer entered being even. Although it will still look a little funny.
Banti
-
Senior Member
by the way to get the
affect use the [ code ] [ /code ] options without the spaces
Banti
Last edited by Banti; 10-09-2001 at 09:56 AM.
-
-
ok seriously,
Is it a req for you to use nested while's ? I think it's a bit easier using just one while loop. (not to mention more readable). I assumed you're allowed at least one if statement. if not...
Code:
#include <iostream.h>
#include <math.h>
int main(void)
{
float MaxWidth;
float RowCount, ColCount, MidMarker, yEquiv, xEquiv;
char DisplayChar;
cout << "\nDisplay Character> ";
cin >> DisplayChar;
cout << "\nEnter Max Width> ";
cin >> MaxWidth;
MidMarker = ( MaxWidth + 1 )/2;
RowCount = MaxWidth;
while( RowCount ){
ColCount = MaxWidth;
while( ColCount ){
yEquiv = RowCount-MidMarker;
xEquiv = ColCount-MidMarker;
// (sqrt of FLOAT*FLOAT) is used to take abs() of a float
if( sqrt(yEquiv * yEquiv) + sqrt(xEquiv * xEquiv) < MidMarker )
cout.put(DisplayChar);
else
cout << " ";
ColCount--;
}
cout << endl;
RowCount--;
}
return 0;
}
There's prob a better way to take the abs of a float but i didn't bother skimming through math.h.
Dunno when your hw is due. good luck.
-
This is what I finally did:
int main()
{
int width = 0;
char sym;
char space = ' ';
int count = 0;
int line = 0;
int w = 0;
int numChar = 0;
// get input
cout << "Enter a symbol, and press Enter: ";
cin >> sym; //Put the value in variable.
cout << "Specify a width in a whole positive integer, and press Enter: ";
cin >> width;//Put the value in variable.
if (width%2 != 1)
{ width++;
}
w = width/2;
count = 0;
line = 0;
numChar = 1;
while ( line <= w )//Start the line counter.
{
while (count <= w )
{
cout <<space;// display output
count ++;//Increment
}
count = 0;//Reset the counter for this loop.
while (count < numChar )
{ cout <<sym; // display output
count ++;//increase counter.
}
numChar+=2;//Increment by two for proper format.
count =0; //Reset counter.
cout <<endl;//
w--;//Decrement space counter.
}
w = width/2;
count = 0;
line = 1;
numChar -= 4;
while ( line <= w )
{
while (count <= line)//Start the spaces counter.
{
cout <<space;// display output.
count ++; //increase counter.
}
count = 0; //Reset counter for this loop.
while (count < numChar )//Start counter.
{
cout <<sym;
count ++; //Increment.
}
numChar-=2; //Reduce number of characters by two.
count = 0; //Reset counter for space loop.
line++; //Increment line counter.
cout <<endl; }
// code to pause the program. Will pause the .exe
cout << "\n\n\t\t Hit Enter" << endl;
cin.get(); // the program waits
return 0;
}
And a big thankyou.
-
Banned
Oh, boy...
tightlines,
your code doesn't compile in Borland C++5.5.
HyperSlug,
Your code does compile and work, but you have a problem. It hangs (infinite loop, I think) iffin user just hits 'enter'. One should validate all user input.
-
Yes, your right. Good idea, I'm going to amend it immediately.
Thanks.
I'm using Visual C++ 6.0
Originally posted by qball
Oh, boy...
tightlines,
your code doesn't compile in Borland C++5.5.
HyperSlug,
Your code does compile and work, but you have a problem. It hangs (infinite loop, I think) iffin user just hits 'enter'. One should validate all user input.
Last edited by tightlines; 10-09-2001 at 09:10 PM.
-
true, i didn't really have robustness on my mind.
my old CS teacher told me to make the code good enough "so a monkey could bang on the keyboard."
besides, i figured tightlines would handle the error checking.
but you're right, all production level code needs to fail gracefully.
BTW, tightlines' code will compile if you put in a #include<iostream.h>
-
I put in just about ALL the header files just to be safe at this juncture.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks