Question:
c++ Loop structure question?
Takkun
2008-09-24 20:38:46 UTC
So I need to write a code using a loop structure and I need to make this shape:
**********
* *
* *
* *
* *
* *
**********
height= 11
width= 10
I couldnt really draw the picture correctly. But I am stuck on how to make the rectangle hollow.

#include
using namespace std;

int main()
{
char symbol, space;
int counter1;
int counter2;
int width,height;
//space= " ";

cout << "Please enter a character and I will generate a shape using the character" << endl;
cin >> symbol;
cout << "Please give width" << endl;
cin >> width;
cout << "Please give height" << endl;
cin >> height;

counter1= 0;
while (counter1 <= height)
{
counter2=0;
while (counter2 <= width)
{
cout << symbol;
counter2++;
}
cout << endl;
counter1++;
}

system ("PAUSE");
}
Four answers:
Jason A
2008-09-24 22:33:22 UTC
Part of homework is to think about ALL that you have learned so far.



You alread know HOW to print 1 line of Symbols. Which means you can also do 2 lines. The only tricky part you are currently facing is how to tell the computer to print one star THEN print some spaces THEN print the last star (aka Symbol).



I can see the answer was already provided. But keep in mind that you will need to reduce the "Width" by 2 for the First and Last "Symbol" of the hollow areas; and "Height" by 2 for First and Last rows. I'm sure your teacher will take points off if you don't. Consider the following display:



[visual example]

height = 4; Width = 10

********** (10 stars)

*........* (2 stars + 8 spaces/dots = 10 chrs on the line)

*........* (2 stars + 8 spaces/dots = 10 chrs on the line)

********** (10 stars)



The best thing to do when you are stuck is write out pseudo code so you can have a better understanding of what you want the computer to do -- note: it doesn't have to be good programming it just has to have a flow you (and others) can understand.



[pseudo code - not code but theory of How you want it to work]

h=10; w=10

neww=w-2 (New width = original width - 2 [first & last chrs])

newh=h-2 (New height = original height - 2 [first & Last rows are solid])



/*main body*/

Print 1 line of Syms (takes care of the First row of stars)



/*hollow section*/

While count1 <= newH

....Print only 1 Sym (the first char of line)

.........While count2 <= newW

............Print Dots (the "hollow" area)

.........End of count2

....Print only 1 Sym (only prints Last char of line)

End of count1

/*end of Hollow Section*/



Print 1 line of Symbols (Takes care of the Last row of stars)

/*end of main body*/



See? In theory, it is do-able without IF statements. Hopefully, you will come up with something on your own instead of simply being given the answer. Yes, it's ironic in this place but remember the old saying:



"If you give a man a fish, he'll eat for today; But, if you teach a man to fish, he'll eat for a life time."



Good luck with your assignment.

--JA
modulo_function
2008-09-25 03:58:22 UTC
You just need a little more logic. Howz bout:



for(int h=1; h<=height;h++){



for( int w=1; w<=width;w++){



if( (h==1)||(h==height) ) cout<
else if ( (w==1)||(w==width) ) cout<
else cout<<" ";

}

cout<
}



See how that works? If you're on the first row or the last row then it prints a symbol all across the width. If you're on any other row then it only prints a symbol at the start and end of the width.



+add

I needed to add the cout<
cja
2008-09-25 11:57:47 UTC
See below a slight variation of what you're trying to do, it prompts for the side length, and displays a nice looking square. As requested, no if statements were used in the writing of this code. I'm guessing you haven't learned about iomanip yet; that's ok, you can look it up. Think of setw and setfill as helper functions that encapsulate loops that you would otherwise have to write. Here you go:



#include

#include



using namespace std;



const char star('*');

const char spacer(' ');



void horizSide(const int len, const char c, const char spacer);



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

int size;

cout << "Enter box side length: ";

cin >> size;

horizSide(size,star,spacer);

for (int i = 0; i < size-1; i++) {

cout << star << setw(size*2-2) << setfill(spacer) << star << endl;

}

horizSide(size,star,spacer);

return 0;

}



void horizSide(const int len, const char c, const char spacer) {

for (int i = 0; i < len-1; i++) {

cout << c << spacer;

}

cout << c << endl;

}
notenn
2008-09-25 04:18:25 UTC
without using "if"? ummm....





cout << "Please give height" << endl;

cin >> height;



int w, h;



// top side

w = 0;

while (w++ < width) {

   cout << symbol;

}

cout << endl;



// left and right side

h = 1

while (h++ < height - 1) {

   cout << symbol; // left side

   w = 1;

   while (w++ < width - 1) { // mid space

     cout << space;

   }

   cout << symbol; // right side

   cout << endl;

}



// bottom side

w = 0;

while (w++ < width) {

   cout << symbol;

}

cout << endl;



system ("PAUSE");

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...