Hello there.. i'm kind of new in c++ and i really suck in programming. so here is the thing. My professor left us with an assignment and i managed to do some things on my own. Here is the problem:
i should write a program that will accept a user entered number this could be any integer. The program will draw a pattern x of whose size is dependent on the user-defined number. A sample entry and corresponding output is shown below:
Input number: 3
output:
x x
x
x x
(nevermind the dots in input number 5, it represents spaces)
*i've done the part when you have inputed even numbers it will print invalid and blah blah blah. i'm also finished in the first lines of the output
*my problem is the succeeding lines..
*and the worst prolem is.. i should base my program in the for statement.
anyone's help will be greatly appriciated. thank you
You should have two loops, one for the coulumn position and one for the row position.
eg:
1 2 3 4 5 6
for(int y = 0; y < user_input; y++)
{
for(int x = 0; x < user_input; x++)
//here you should put the condition and draw an 'X' or a space
cout << '\n';// end of row so go to a new line
}
for the condition notice that in (input == 7) │0123456 (x position)
0│x x
1│ x x
2│ x x
3│ x
4│ x x
5│ x x
6│x x
(y position)
This assignment made me think for a minute; consider this:
Find the equations of the two lines and create a function for each. Then iterate through the view-plane testing for valid coordinates and plot the 'x' where appropriate. That way, you could actually plot other lines if you someday desired.
When you choose whether to print an 'x' or a space you should use as condition the equation of the two lines.
In the 'x' example the equations are:
y = x
and
y = -x + 6
The slope-intercept form of an equation of a line is y = mx + b, where m is the slope of the line and b is the intercept. (I think it's the X-intercept, but I could be wrong; it's the one where the X coordinate is 0)
1 2 3 4 5 6 7 8
/*
Create a class to represent a line that stores two doubles: _m (the slope) and _b the intercept.
*/
// add a member to return the unique Y coordinate given a specific X, such as:
double Y( double x ) {
return _m * x + _b;
}
In the traversal of the Cartesian view-plane, if the current y == line.Y( x ) then you are at a point that resides on the line.
This could be extended to support construction from a point on the line and a slope, as well. It's a neat application of the stuff that they teach you in math courses and don't use. ;)