There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot browser bsod computer cpu crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware missing monitor network networking outlook outlook 2003 outlook 2007 outlook express password popups problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
c++ code


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
king1's Avatar
Junior Member with 9 posts.
 
Join Date: Feb 2006
Experience: Intermediate
08-Oct-2006, 08:15 AM #1
Arrow c++ code
i am new at c++ and i a trying to write a program tha outputs the following but to look center alinged:

*
***
*****
*******
am also trying on a prog that can reverse numbers i.e 456362 to be 263654
BanditFlyer's Avatar
Distinguished Member with 10,906 posts.
 
Join Date: Oct 2005
Location: BOT бaHДиT
Experience: It Depends
08-Oct-2006, 08:36 AM #2
Sounds like a pretty simple program. How much code have you written so far?
king1's Avatar
Junior Member with 9 posts.
 
Join Date: Feb 2006
Experience: Intermediate
08-Oct-2006, 08:40 AM #3
this is what i have for the stars, but it cant aling it center
for the inversion of numbers am stuck


int z, a;

printf("please enter the number of rows you want: ");
z = GetInteger();
for(int i = 1; i <= z; i++)
{
a +=2;
for(int a = 0; a < i; a++)
{
printf("*");
}
printf("\n");
}
BanditFlyer's Avatar
Distinguished Member with 10,906 posts.
 
Join Date: Oct 2005
Location: BOT бaHДиT
Experience: It Depends
08-Oct-2006, 08:59 AM #4
Quote:
Originally Posted by king1
this is what i have for the stars, but it cant aling it center
for the inversion of numbers am stuck


int z, a;

printf("please enter the number of rows you want: ");
z = GetInteger();
for(int i = 1; i <= z; i++)
{
a +=2;
for(int a = 0; a < i; a++)
{
printf("*");
}
printf("\n");
}
I would throw in another integer for the number of spaces to prepend. Then whenever you print, you would subtract two from the spaces integer, s , so that the next line of print would be two spaces over to the left.

I would start the new integer, s, as being equal to z. Then, do another loop which prints spaces before you run the loop that prints asterisks. Keep printing spaces until you've printed as many as the current value of s.


As far as inverting numbers, normally that would be done with a stack, but you can do it with a loop also. Take a single digit off of the rear of the number that was input(I think you can do this with the mod operator) and append it to the rear of a new string. Keep doing that until you have no resuts from the mod operation.
__________________
Don't taze me, Bro!


(there's got to be a dance version of that somewhere - remember the old song "Don't hurt me"?)
king1's Avatar
Junior Member with 9 posts.
 
Join Date: Feb 2006
Experience: Intermediate
08-Oct-2006, 10:12 AM #5
i really dont know where to put it
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
08-Oct-2006, 01:02 PM #6
Breaking down the problem and knowing where to "put it" is the point of the lesson.

Break down the problem like this:

1) Draw out the desired output for 4 or so rows
2) Make observations
3) Derive code that matches the observations

Code:
/*

when rows = 4

Desired Output:

   *        spaces before the stars = 3, stars = 1, total length = 4
  ***       spaces before the stars = 2, stars = 3, total length = 5
 *****      spaces before the stars = 1, stars = 5, total length = 6
*******     spaces before the stars = 0, stars = 7, total length = 7

Observe:
a) spaces decrement by 1 four times (same number as rows) but start at 3 and end at 0
b) stars start at 1 then increment four times but by odd numbers
c) the total length of the printout for each row starts equal to 
   rows then increments by one (probably not useful)


Derive code:

int rows = 4;
int spaces = rows - 1;   // spaces start at rows - 1;
int stars = 1;           // stars start at 1
do
    // do print out here         

    stars+=2;           // inc stars by two (will always be odd since we start at 1)
    spaces--;           // dec spaces;
    rows--;             // dec rows;
while(rows>0);          // exit the loop when rows = 0


*/
There are slicker (optimized/generic) ways to solve this, but the point is breaking down the problem and deriving code.

Best,
Dan
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:14 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.