Sunday, August 2, 2009

//c++ help making a pyramid of $'s;?

its supposed to be like


....$


..$$$


$$$$$





(without the .'s)





heres what i have





for(loopCounter = 1; loopCounter %26lt;= 5; loopCounter += 2);


{


dollarsToPrint = loopCounter;


spacesToPrint = (5 - dollarsToPrint) / 2;


int i;


for (int i; i %26lt;= spacesToPrint; i++)


{


cout %26lt;%26lt; " ";


}


for (i = 1; i %26lt;= dollarsToPrint; i++)


{


cout %26lt;%26lt; "$";


}


cout %26lt;%26lt; "\n";


}





yes, everything is declared, it just loops infinately








heres another attempt, i just started over and all it does is 1 $ sign





for (loopCounter = 1; loopCounter %26lt;= 5; loopCounter += 2);


{


dollarsToPrint = loopCounter;


spacesToPrint = (5 - dollarsToPrint) / 2;


int i;


for (i = 1; i %26lt;= spacesToPrint; i++) //print spaces


cout %26lt;%26lt; " ";


//print dollar signs


cout %26lt;%26lt; "$";


cout %26lt;%26lt; endl;


}





i appreciate any help as i am a n00blet

//c++ help making a pyramid of $'s;?
You have a semi-colon at the end of your 'for' statement:





for (loopCounter = 1; loopCounter %26lt;= 5; loopCounter += 2);





Even with that fixed, you're only printing one $ per pass through your outer loop, when you need 1, 3, 5, etc. You're definitely on the right track, but just have some problems with your counters and computations. If you walk through it on paper, you should quickly see what's happening.





There are many different ways to code this, but I thought I'd try to fix your approach, rather than offer you my own. Check this out:





const int pyramidBaseWidth = 5;


const int pyramidHeight = pyramidBaseWidth/2 + 1;





int main(int argc, char *argv[]) {





for (int loopCounter = 0; loopCounter %26lt; pyramidHeight; loopCounter++) {


int spacesToPrint = pyramidHeight - loopCounter - 1;


int dollarsToPrint = loopCounter * 2 + 1;


for (int i = 0; i %26lt; spacesToPrint; i++) cout %26lt;%26lt; " ";


for (int i = 0; i %26lt; dollarsToPrint; i++) cout %26lt;%26lt; "$";


cout %26lt;%26lt; endl;


}


return 0;


}





It'll work for any value you plug in for pyramidBaseWidth. Even if your problem statement explicitly specified 5 for the width of your pyramid base, get in the habit of using constants, as I've done, rather than using hardcoded numbers, as you did with 5 in your code. It's a good habit to get into; it's almost never acceptable to use hardcoded values.
Reply:you almost had it with your first program...


i took that code and adjusted it so it should run.


Im at work and couldnt test it, but it looks good to me





for (loopCounter = 1; loopCounter %26lt;= 5; LoopCounter +=2)


{


dollarsToPrint = loopCounter;


spacesToPrint = (5 - dollarsToPrint)/2;





for (int i = 1; i %26lt;= spacesToPrint; i ++)


{


cout%26lt;%26lt;" ";


}


for (i = 1; i %26lt;= dollarsToPrint; i ++)


{


cout %26lt;%26lt; "$";


}


cout%26lt;%26lt; endl;


}





looks like your problem stemmed from the extra


semicolon on the first loop and not assigning a


value to the "i" in the second loop
Reply:For your second attempt:





- Remove the semicolon at the end of your top for statement.


- Add curly brackets after your second for statement, enclosing everything you want to be executed in that for loop.





Examine your first for statement. Why is loop counter being incremented by 2? The outer loop will only execute twice before it is no longer %26lt;= 5





Good luck!

blazing star

No comments:

Post a Comment