This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

why can't the Loop states and multi-dimensions arrays run?

Hi,
1. In my programme,there are some loop states such as the following:

int x,x1,x2;
x1=1;x2=100;
for(x=x1;x<=x2;x++)
{
y=k*x+b;
Pixel(x, y, Mode);
}

But in fact,the loop is executed only once,then run the wrong code and the code after the loop is never executed.can you give me some advices?
2.In my programme, there is a multi-dimension array like this--

unsigned char code HZTable[2][16]={....};

But the operation to the array always goes wrong.how can I resolve the problem?
thanks

Liu ***
huihl@163.com

Parents
  • I re-wrote your program (so I could understand it) and it seems to work just fine. Maybe my changes fixed a bug or something.

    #include <math.h>
    #include <stdio.h>
    #include <reg51.h>
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    #define min(x,y) (((x)<(y)) ? (x) : (y))
    #define max(x,y) (((x)<(y)) ? (y) : (x))
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void Pixel (
      unsigned char x,
      unsigned char y,
      bit mode)
    {
    mode = mode;
    printf ("%3.3u x %3.3u\n", (unsigned) x, (unsigned) y);
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void line_x (
      unsigned char x,
      unsigned char x_end,
      float k,
      float b,
      bit mode)
    {
    for(; x<=x_end; x++)
      {
      Pixel (x, k*x+b, mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void line_y (
      unsigned char y,
      unsigned char y_end,
      float k,
      float b,
      bit mode)
    {
    for(; y<=y_end; y++)
      {
      Pixel (k*y+b, y, mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void Line (
      unsigned char x1,
      unsigned char y1,
      unsigned char x2,
      unsigned char y2,
      bit Mode)
    {
    double k,b;
    
    if (abs(y1-y2) <= abs(x1-x2))   // |k|<=1
      {
      k = (float)(y2-y1) / (float)(x2-x1);
      b = y1 - (k * x1);
    
      line_x (min(x1,x2), max(x1,x2), k, b, Mode);
      }
    else                            // |K|>1
      {
      k = (float)(x2-x1) / (float)(y2-y1);
      b = x1 - (k * y1);
    
      line_y (min(y1,y2), max(y1,y2), k, b, Mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void main (void)
    {
    SCON  = 0x50;
    TMOD |= 0x20;
    TH1   = 221;
    TR1   = 1;
    TI    = 1;
    
    Line(0,0,23,34,1);
    
    while (1);
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/

    Jon

Reply
  • I re-wrote your program (so I could understand it) and it seems to work just fine. Maybe my changes fixed a bug or something.

    #include <math.h>
    #include <stdio.h>
    #include <reg51.h>
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    #define min(x,y) (((x)<(y)) ? (x) : (y))
    #define max(x,y) (((x)<(y)) ? (y) : (x))
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void Pixel (
      unsigned char x,
      unsigned char y,
      bit mode)
    {
    mode = mode;
    printf ("%3.3u x %3.3u\n", (unsigned) x, (unsigned) y);
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void line_x (
      unsigned char x,
      unsigned char x_end,
      float k,
      float b,
      bit mode)
    {
    for(; x<=x_end; x++)
      {
      Pixel (x, k*x+b, mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void line_y (
      unsigned char y,
      unsigned char y_end,
      float k,
      float b,
      bit mode)
    {
    for(; y<=y_end; y++)
      {
      Pixel (k*y+b, y, mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void Line (
      unsigned char x1,
      unsigned char y1,
      unsigned char x2,
      unsigned char y2,
      bit Mode)
    {
    double k,b;
    
    if (abs(y1-y2) <= abs(x1-x2))   // |k|<=1
      {
      k = (float)(y2-y1) / (float)(x2-x1);
      b = y1 - (k * x1);
    
      line_x (min(x1,x2), max(x1,x2), k, b, Mode);
      }
    else                            // |K|>1
      {
      k = (float)(x2-x1) / (float)(y2-y1);
      b = x1 - (k * y1);
    
      line_y (min(y1,y2), max(y1,y2), k, b, Mode);
      }
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/
    void main (void)
    {
    SCON  = 0x50;
    TMOD |= 0x20;
    TH1   = 221;
    TR1   = 1;
    TI    = 1;
    
    Line(0,0,23,34,1);
    
    while (1);
    }
    
    /*---------------------------------------------------------
    ---------------------------------------------------------*/

    Jon

Children