I am in an intro to C++ class, and I have a questiom about one of the codes our professor wants us to write. The question is this:
Write a program for a positive integer no greater than 15. The program should then display a square on the screem using the character "X". The number entered by the user will be the length of each side of the square. For example, if the user eneters 5 the program should display:
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
I have all of the input validation done and it works. I'm just hungup on how to display the sqaure. I can only get it to display a line of ten X's if I enter 5. I will post what I have, thanks for any input or help!
Also the delimanator in your for loop is for 'posint' to be lower then '15'. That means after '14' the loop is over with, it will never be more the '15' which is what your "if(posint > 15)" line looks for so your "cout << endl;" will never execute.
int ROWS = ..., COLS = ...; // initialize the number of rows and columns
// Your dealing with a 2D shape so you need one for-loop to loop though each row
for( int row = 0; row < ROWS; row++ ) {
// and in each row you need a for-loop to loop though each column
for( int col = 0; col < COLS; col++ ) {
// In each column, of each row, you print an 'X'
...
}
//also for each row you need to print something else (but not for the columns)
...
}
int rows=1, cols=1;
for (posint = 0; posint < 15; posint++)
{
for( int row = 0; row < rows;rows++)
{
cout << 'X'<< endl;
}
for( int col = 0; col < cols;rows++)
{
cout << 'X' << endl;
}
}
I have this now, and now it just gives me a never ending strand of 1 "X". I tried to do a break but it still didnt work. I tried to do everything that was mentioned.
We need to first print 'X' 5 times (1 for each column)
1 2 3
//print 5 'X' characters
for( int col = 0; col < COLS; col++ )
cout << 'X';
Then we need to print a linebreak ('\n' or endl)
1 2 3 4 5
//print 5 'X' characters
for( int col = 0; col < COLS; col++ )
cout << 'X';
//print a linebreak after the row of 'X' characters
cout << endl;
Now, we need to do this for each and every row (which would be 4 times in this example.)
So we need to put the above code in a for loop that runs 4 times (1 for each row)
1 2 3 4 5 6 7 8
//outer loop runs once for each row
for( int row = 0; row < ROWS; row++ ) {
//print 5 'X' characters
for( int col = 0; col < COLS; col++ )
cout << 'X';
//print a linebreak after the row of 'X' characters
cout << endl;
}
#include <iostream>
//this example prints 5 rows
int main() {
for( int i = 0; i < 5; i++ ) {
for( int j = 0; j <= i; j++ )
printf( "%c", 'A' + (i*i + i)/2 + j );
printf("\n");
}
return 0;
}
A | 0
B C | 1 2
D E F | 3 4 5
G H I J | 6 7 8 9
K L M N O | 10 11 12 13 14
f(i) = ? = the first number in row i
f(0) = 0
f(1) = 1 (+1)
f(2) = 3 (+2) (+1)
f(3) = 6 (+3) (+1)
f(4) = 10 (+4) (+1)
so f(i) is quadratic
f(i) = ai2 + bi + c
f(0) = 0 = a(0)2 + b(0) + c = c, c = 0
f(1) = 1 = a(1)2 + b(1) + 0 = a + b, a + b = 1
f(2) = 3 = a(2)2 + b(2) + 0 = 4a + 2b, 4a + 2b = 3
a + b = 1 | -2a - 2b = -2
4a + 2b = 3 | (+) 4a + 2b = 3
(-2a + 4a) + (-2b + 2b) = (-2 + 3),
2a + 0 = 1 , a = 1/2
a + b = 1, (1/2) + b = 1, b = 1 - (1/2), b = 1/2
f(i) = (1/2)i2 + (1/2)i + 0
= (i2 + i) / 2
so each number is just, f(i) + j, where in the first column j = 0,
and each letter is just, the above number + 'A'
Nice solution method Mathhead200! I like how you explicitly derived the form of f(i).
I found the same solution but used different reasoning to find f(i). Sharing this:
f(i) = # of letters printed on preceding lines
= (# of preceding lines)*(average length of preceding lines) = i*(i+1)/2
I was hesitant to post the solution though.
Yes, If we look at the formula, it's similar to the area of a triangle...
If I wasn't constrained to the "2 variable" rule, I would have just made a third to hold the letter value, and increase it after each iteration.
Well your way is much easier to explain fun2code. Ha ha!