Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,975 people online right now. Registration is fast and FREE... Join Now!
Hello. My code compiles but I can't get the desired output. Basically, my code should display a graph of a line when I enter the endpoints of x1, x2, y1, and y2.
for example:
X X X X X
or
a negative slope
or X X X X X X
or
XXXXXXXXX
Also, I want to clear the screen first before the graph is displayed similar to clrscr(); in C++ but it does not seem to work in C...
CODE
//PART I// #include<stdio.h>
int main(void)
{
int x1; int x2; int y1; int y2;
printf("Enter the value of x1:"); scanf("%d",&x1); printf("Enter the value of x2:"); scanf("%d",&y2); printf("Enter the value of y1:"); scanf("%d",&y1); printf("Enter the value of y2:"); scanf("%d",&y2);
//PART II// //These conditions in the 'if' are basically the width and height of a cleared screen.:// if((0<x1) && (x1<81) && (0<x2) && (x2<81) && (0<y1) && (y1<21) && (0<y2) && (y2<21)) { int xlow; int xhigh; int ylow; int yhigh;
//These if statements are supposed to get the higher and lower values of the x and y inputs which will be the data to be used in the next part of the program(the one that actually does the plotting)://
if(y1>y2) { ylow=y2; yhigh=y1; } else if(y2>y1) { ylow=y1; yhigh=y2; } else { ylow=y2; ylow=y1; } } //PaRT III// //This part is supposed to display the graph:// int wi; int ex; int m; m=((xhigh-xlow)/(yhigh-ylow)); for(wi=0;wi=ylow;wi++) { for(;ylow=yhigh;ylow++) { for(ex=0;xlow>=xhigh;xlow+m,ex++) { if(ex>=xlow+m) { printf("X"); } } printf("\n"); } printf("\n"); } }
//I only placed an extra int here because my C program immediately terminates before I can even see the output. I'm using the bloodshed devC++ if you're familliar with it...)//
int u; scanf("%d",&u); }
//Pretty much it. Can you Help correct my code, if you could, I think it be better if parts I and II are to be placed in two different functions other than the int main(void). //
Well, a few things. One, it did not compile for me until I commented out the bracket directly above Part III. If you don't do that, xhigh, xlow, yhigh, ylow will go out of scope. I think you have an extra bracket in there. It seemed to compile and run when I took that bracket out. Two, I'm not sure about these lines:
CODE
for(wi=0;wi=ylow;wi++)
and
CODE
for(;ylow=yhigh;ylow++)
You are using '=' as the second argument in the for loops, which in C++, and I believe C as well, is the assignment operator, not the comparison operator. The comparison operator is "==". Do you want that instead of "="? I actually think you may want "<" or "<=" as the comparison test for these loops. Otherwise they won't loop through even once if the two numbers don't start with the same values.
I'm a little unclear on what it's exactly supposed to do, but making those changes made it go through the loop a few times at least whereas before it did not. Can you give an example of the desired output given certain input of the four variables (like 4, 5, 6, 7 or something)?
*screen is cleared* I only put the numbers so you can imagine it in a cleared screen since a cleared screen can contain 80 x values and 20 y values but, in the actual output they wont appear. 12345...80 //-----> x increases this way... // 3 4 5 6 X 7 X . . . 20
//y increases this way...// There. Thanks for the advice. I'll try it out. By the way, have you?
So you want it to "tick" up the correct line on the screen every time that number is encountered? (I haven't so much as looked at your source code, let alone run it)
I tried it out but didn't get any X's. I see where the 6 and the 7 come in in the sample output, but not where the 4 and the 5 come in. Is this program supposes to draw a line segment from point (x1, y1) to point (x2, y2)? Or from (xlow, ylow) to (xhigh, yhigh)? I noticed you had a variable "m", which it looks like you want to represent the slope. That makes me think that you want to plot a line segment from (xlow, ylow) to (xhigh, yhigh). Is this correct? Also, what would the output be for, say:
xlow = 2 ylow = 3
xhigh = 6 yhigh = 7
Would this be a line segment (represented by X's) from points (2,3) to (6, 7)? Just trying to get a feel for this problem.
so, is your prof's surname Guinto? anyway, I had the same problem as you do when the slope isn't equal to 1 or -1... i posted my code somewhere but sorry for it not being so organized thats why i did'nt get any reply...
just to answer VernonDozier, this program must form a line connecting the two points given... thats just that...
the user can input any combination of x and y as long as it belongs to the valid numbers of x and y. ex input: x1= 10 y1= 1 x2= 1 y2= 19
You have two issues here. One is plotting a line. The other is drawing it on the screen. The problem is, in C, that you can't just plot a point on the screen.
Step 1, create a function that actually produces a vaild point set. Before you even think about graphs, if that doesn't work you're going nowwhere. Once you can produce the points you need, you can worry about getting them onto the screen.
Here's a test for your code:
CODE
void plot(int x, int y) { printf("(%d,%d)\n",x,y); }
void draw(int xlow, int ylow, int xhigh, int yhigh) { int wi; int ex; int m; m=((xhigh-xlow)/(yhigh-ylow)); for(wi=0;wi=ylow;wi++) { for(;ylow=yhigh;ylow++) { for(ex=0;xlow>=xhigh;xlow+m,ex++) { if(ex>=xlow+m) { plot(ex, ylow); } } } } }
int main(void) { draw(3, 2, 20, 10); }
This goes into an infinite loop and locks up. It's a little beyond help, I'm afraid. Make it so it gives you your points. Then worry about the pictures.