Welcome to Dream.In.Code
Become a C++ Expert!

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!




HELP! Ascii line

 
Reply to this topicStart new topic

HELP! Ascii line

Re1Mu2R3
16 Jan, 2008 - 10:21 PM
Post #1

New D.I.C Head
*

Joined: 14 Dec, 2007
Posts: 7


My Contributions
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(x1>x2)
{
  xlow=x2;
  xhigh=x1;
}
else if(x2>x1)
{    
  xlow=x1;
  xhigh=x2;
}
else
{    
  xlow=x2;    
  xhigh=x1;    
}

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). //


User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: HELP! Ascii Line
17 Jan, 2008 - 12:05 AM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

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)?
User is offlineProfile CardPM
+Quote Post

Re1Mu2R3
RE: HELP! Ascii Line
17 Jan, 2008 - 02:23 AM
Post #3

New D.I.C Head
*

Joined: 14 Dec, 2007
Posts: 7


My Contributions
Inputting 4, 5, 6 and 7?

Desired output is:

*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?
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: HELP! Ascii Line
17 Jan, 2008 - 02:36 AM
Post #4

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,113



Thanked: 9 times
Expert In: Hardware, Networking

My Contributions
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)
User is offlineProfile CardPM
+Quote Post

Re1Mu2R3
RE: HELP! Ascii Line
17 Jan, 2008 - 02:51 AM
Post #5

New D.I.C Head
*

Joined: 14 Dec, 2007
Posts: 7


My Contributions
Yeah, pretty much.
Actually, VernonDozier here located an extra bracket right above PART III...


User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: HELP! Ascii Line
17 Jan, 2008 - 10:08 AM
Post #6

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

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.
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: HELP! Ascii Line
17 Jan, 2008 - 12:30 PM
Post #7

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,113



Thanked: 9 times
Expert In: Hardware, Networking

My Contributions
Personally, I'd have an array, and tick up each value when it's encountered, and then have it display based on the array after the input is complete.
User is offlineProfile CardPM
+Quote Post

antlu
RE: HELP! Ascii Line
17 Jan, 2008 - 07:39 PM
Post #8

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 2

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

User is offlineProfile CardPM
+Quote Post

baavgai
RE: HELP! Ascii Line
18 Jan, 2008 - 04:22 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month