Saturday, May 22, 2010

I need help with this program for C++?

//double rectangle of stars


#include %26lt;iostream%26gt;


using namespace std;


int main () {


int h,w;


cout %26lt;%26lt; "Enter a height and a width: "%26lt;%26lt;endl;


cin %26gt;%26gt; h,w;


for (int row =1; row %26lt;=h; row ++) {


for (int col =0; col %26lt;=w; col++) {


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


}


cout %26lt;%26lt;endl;


}


return 0;


}

I need help with this program for C++?
Fix the limit of your inner for loop buddy - it is running one too many times! Just like the outer loop, it should start with 1 and not zero. Alternatively, if you stick to 0, use col %26lt; w instead of using %26lt;= in the inner for loop.





If you fix that, then you will get the right number of stars, but still you won't get the spaces. Some previous answers do address the space issue. Here is the shortest and sweetest one (assuming you fix the inner loop counting problem by making it start from 1, also note the use of endl):





if( col == w ) cout %26lt;%26lt; "*" %26lt;%26lt; endl; //without space


else cout %26lt;%26lt; "* "; // with space
Reply:Thanx Report It

Reply:add an If-Then statement around the cout %26lt;%26lt;"*" lines so if the row is odd it is "* " and if even " *"
Reply:cout %26lt;%26lt; "* ";





instead of cout %26lt;%26lt; "*";
Reply:WOAH!cool!
Reply:int h;


int w;


cin%26gt;%26gt;h; w;...


for a start


No comments:

Post a Comment