Programming Assignment Help

Feb 14, 2012 at 2:57am
I have this beginning programing assignment and I'm really stuck...any help would be useful.

In the output that follows, a C++ program prompted a user for two inputs, the height and width of a box and the user responded with “32” for the height and “30” for the width. Write and compile the C++ program that did this:

Height of box: 32
Width of box: 30
* * * * * * * * * * * * * * *
** ** ** ** ** ** ** ** ** **
*** *** *** *** *** *** *** **
**** **** **** **** **** ****
***** ***** ***** ***** *****
****** ****** ****** ****** **
******* ******* ******* ******
******** ******** ******** ***
********* ********* *********
********** ********** ********
*********** *********** ******
************ ************ ****
************* ************* **
************** **************
*************** **************
**************** *************
***************** ************
****************** ***********
******************* **********
******************** *********
********************* ********
********************** *******
*********************** ******
************************ *****
************************* ****
************************** ***
*************************** **
**************************** *
*****************************
******************************
******************************
******************************


You must use two nested for-loops to solve this problem. You may not use any other loops to solve this problem.

You may not use arrays, chars or string variables.

You must only use cout statements that have only single character, for example, cout << "*";. Something like cout << "**"; or cout << " *"; is not permitted.

Spaces are counted as a character.

Your program must work for any height and width from 1 to 80 inclusive.

Hint Look closely at the pattern that the spaces form, and ... review the modulus operator “%”.
Feb 14, 2012 at 3:03am
Could you please post the code that you're using? =)
Feb 14, 2012 at 3:26am
You mean what I've come up with so far? Well so far I just have this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(void)

{	int w, h;
  
	cout << "Enter screen width:" ;
	cin >> w;
	cout << "Enter screen height:";
	cin >> h;

	for (int i = 1; i <= h; i++)
		{	for (int j = 1; j <= i; )
			{	cout << "*" ;
			}
			cout << endl;		
			
		}

	cout << endl;

return 0;
}


Which basically just makes a block of *'s with the width and height input. I don't know how to get it in the pattern it requests with only two for loops...

I notice that each row, the number of *'s before a space is equal to the row number. So for example, row 4 has 4 *'s, then a space, then another 4 *'s, then a space, and so on...I'm just not sure how to incorporate this.
Feb 14, 2012 at 3:34am
Ahhh, nevermind, I figured it out just now. I was just going about thinking about it wrong. The output of a space or an asterisk depends on whether the column number is evenly divisible by the row number.

...thanks anyway! :p
Feb 14, 2012 at 3:42am
Lol, I had just figured it out xD

If you still need help, I'd be glad to assisst ;)
Last edited on Feb 14, 2012 at 3:42am
Topic archived. No new replies allowed.