C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old April 27th, 2008, 06:24 PM
novprog novprog is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 novprog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 58 sec
Reputation Power: 0
Creating a rectangle / triangle with asterisks

Hi, i'm new to C++ and I just started my first course on it. I'm having some trouble with a program i'm supposed to make. The program would ask for width / length for rectangle, and then print the shape of the rectangle with asterisks (*), and display the area and perimeter. Heres what i have so far, but i dont know where to even start with creating the rectangle. I'm supposed to use a nested loop to create the rectangle.

int main()
{
//declare variables
int length = 0;
int width = 0;
int perimeter = 0;
double area = 0.0;

//inputs
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;

//calculate area and perimeter
perimeter = (length * 2) + (width * 2);
area = length * width;

//output rectangle


//output area and perimeter
cout << "Area: " << area << endl;
cout << "Perimeter: " << perimeter << endl;



return 0;
} //end of main function

Any help would be appreciated.

Reply With Quote
  #2  
Old April 27th, 2008, 07:24 PM
novprog novprog is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 novprog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 58 sec
Reputation Power: 0
okay after some work ive finally come up with this for printing the rectangle:

for (int row=0; row<length; row++) {
for (int col=0; col<width; col++) {
cout << "*";
}
cout << endl;
}

However, this prints just a full rectangle of the stars, i would like to print the rectangle, but have the center be spaces, not just asterisks.

I'm assuming i need to use if statements to do so, but can anybody help me?

Reply With Quote
  #3  
Old May 6th, 2008, 05:50 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 338 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 14 h 13 m 10 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Basically, except the first line and the last line, you need to begin and end with '*' else use ' '.
Pseudocode:
Code:
if(rows>0) for all columns output '*'
for (all other rows before rows-1) {
  if (columns>0) output '*'
  for (all othercolumns before columns-1) draw ' '
  if (columns>1) output '*'
}
if (rows>1) for all columns output '*' 


If you can assume at least 2x2, then you can remove all if(...) statements.

Triangles will be a good bit harder, depending on the requirements.

Reply With Quote
  #4  
Old May 6th, 2008, 06:58 AM
3388 3388 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 3388 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 sec
Reputation Power: 0
just come to have a look

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Creating a rectangle / triangle with asterisks


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway