0

I'm trying to make a program that makes a cartesian plane; you input the basic information of the line and it draws the line.

Too bad that it doesn't work well. Basically this is the loop that puts an X char in every point of the line:

for(int x=0;x<MAP_HEIGHT;x++)
{
    piano.griglia[x][a*x+b]='X';
}

But it does not work well! here is the result:
output

Notice how there are 3 lines where there should be only one. Here's the code of the plane, it may help:

class Cartesiano
{
public:
char griglia[MAP_LENGHT+1][MAP_HEIGHT+1];
Cartesiano( void )
{
    for(int y=0;y<=MAP_HEIGHT;y++)
    {
        for(int x=0;x<=MAP_LENGHT;x++)
        {
            if (y==0)griglia[x][y]='_';
            else
            {
                if(x==0)griglia[x][y]='|';
                else griglia[x][y]=' ';
            }
        }
    } 
}
void Draw( void )
{
    for(int y=MAP_HEIGHT;y>=0;y--)
    {
        for(int x=0;x<=MAP_LENGHT;x++)cout<<griglia[x][y];
        cout<<"\n";
    }
}
}piano;

Any ideas?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
olinarr
  • 261
  • 3
  • 13
  • What's the values of `MAP_HEIGHT` and `MAP_LENGTH`? And when initializing, why use a single loop when you use a proper nested loop when "drawing"? – Some programmer dude Dec 14 '13 at 14:36
  • I defined those with #define at the beginning, they are both 50. Besides, I didn't get the loop part. I used a nested loop, what's wrong? – olinarr Dec 14 '13 at 14:38

1 Answers1

4

When your y values exceed 49 (MAP_HEIGHT - 1) you are overflowing your array. If in your draw loop you calculate y=a*x+b and print Z instead of X you'll see the first line is X's and the overflowed errors are all Z's

for(int x=0;x<MAP_LENGTH;x++)
{
    int y = a*x+b;
    if(y<MAP_HEIGHT) {
        piano.griglia[x][y]='X';
    } else {
        piano.griglia[x][y]='Z';
    }
}

This is due to the way memory for the arrays is stored in C++, it just happens writing outside one array here causes you to write into the next one. In general doing this will crash your program.

Also note that here the x loop should be over MAP_LENGTH.

I would also strongly advise getting a proper graphics library

djechlin
  • 59,258
  • 35
  • 162
  • 290
user2711915
  • 2,704
  • 1
  • 18
  • 17
  • Note that someone has 'helpfully' corrected your misspelling of `MAP_LENGHT` to `MAP_LENGTH` and removed my reference to this in my answer, so if you just copy paste in what I've supplied it now won't work. Correct your spelling of length to use this. – user2711915 Dec 14 '13 at 15:46
  • Thank you. A proper graphics library like? could you suggest one? Also, sorry for the MAP_LENGHT thing! english is not my first language! :) Also the array size is actually MAP_HEIGHT+1, not -1... – olinarr Dec 14 '13 at 15:48
  • A simple library that can be used with C++ is [allegro](https://www.allegro.cc/) There are lots of setup guides and tutorials out there. I am not the best person to ask for this, and if all you want to do is plot graphs then there are specific libraries for that, and a different language like R would be vastly easier to use. – user2711915 Dec 14 '13 at 15:51
  • Thank you a lot! I'll try to see allegro. But I think i'll stick with c++: I'm doing this to improve my programming skills. – olinarr Dec 14 '13 at 15:54
  • Ok, have a good time. Do give things like [R](http://www.r-project.org/) a try, it is still programming, and actually has lots of features for graphical output built in, while avoiding lots of technical challenges that you don't really need when starting out in C++. Also remember that you'll need to avoid going outside your arrays by using negative y values as well. If you think I've adequately answered your question there should be a tick mark next to my answer you can press to accept it, which would be much appreciated. – user2711915 Dec 14 '13 at 15:59