X patterns

Jan 1, 2009 at 1:10pm
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

Input number: 5
output:
x...x
.x.x.
..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


Jan 1, 2009 at 2:06pm
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)


the 'X' depends on x and y position
Last edited on Jan 1, 2009 at 2:10pm
Jan 1, 2009 at 5:00pm
cool.. thx :D

umm.. can anyone try it on a different approach in a for statement?
Jan 1, 2009 at 9:12pm
That is in a for statement. Try actually reading it.
You didn't even notice the correlation between x and y when you chart an X?

If you had looked at that, maybe you would understand that the person who posted above you ALREADY GAVE YOU THE ANSWER.
Jan 2, 2009 at 8:27am
duh, i know what a for statement looks like, what i meant is.. is there another way for a for statement to be written?
Jan 2, 2009 at 10:04am
What you mean with "is there another way for a for statement to be written" ?

You could use the for statement as you want (according to its format), for more documentation read
http://www.cplusplus.com/doc/tutorial/control.html#for
Jan 8, 2009 at 4:41pm
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.
Jan 8, 2009 at 6:58pm
can you give some sample .... @ ABOVE
Jan 8, 2009 at 10:10pm
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
Jan 8, 2009 at 10:37pm
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. ;)
Last edited on Jan 9, 2009 at 7:45pm
Jan 8, 2009 at 10:48pm
I think it's the X-intercept


It's the y-intercept, but otherwise you are right.
Topic archived. No new replies allowed.