Please help me with this C code?

Discussion in 'Programmer's Corner' started by killer6008, Feb 7, 2010.

  1. killer6008

    Thread Starter New Member

    Jan 26, 2010
    20
    0
    Hi

    I am using visual studio to run this file and its giving me an error. I have tried to solve this error couple of times but it makes no sense that what could it be.

    The eror I am getting is:

    Unhandled exception at 0x0040160e in programmingcwk.exe: 0xC0000005: Access violation writing location 0x0009aeac.

    This is the code:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <math.h>
    4. #define XSIZE 256
    5. #define YSIZE 128
    6. #define RED 0
    7. #define GREEN 1
    8. #define BLUE 2
    9.  
    10. void main()
    11. {
    12.     int image[YSIZE][XSIZE][3];
    13.     int x,y;
    14.     int xlow,xhigh;
    15.     float x1,x2,x3,x4,y1,y2=0,y3=0,y4=0,width,C1,C2,C3,C4;
    16.     float red1,red2,green1,green2,blue1,blue2;
    17.     float redsize,greensize,bluesize;
    18.     float redfloat,greenfloat,bluefloat;
    19.     float m = 0, grad = 0;
    20.  
    21.     FILE *pfile = NULL;
    22.  
    23.  
    24.  
    25.         printf("\tEnter RED 1 value: ");
    26.         scanf("%f", &red1);
    27.  
    28.    
    29.  
    30.         printf("\tEnter GREEN 1 value: ");
    31.         scanf("%f", &green1);
    32.    
    33.  
    34.         printf("\tEnter BLUE 1 value: ");
    35.         scanf("%f", &blue1);
    36.  
    37.    
    38.         printf("\tEnter RED 2 value: ");
    39.         scanf("%f", &red2);
    40.        
    41.    
    42.         printf("\tEnter GREEN 2 value: ");
    43.         scanf("%f", &green2);
    44.        
    45.  
    46.         printf("\tEnter BLUE 2 value: ");
    47.         scanf("%f", &blue2);
    48.        
    49.  
    50.     redsize=(red2-red1)/256;
    51.     greensize=(green2-green1)/256;
    52.     bluesize=(blue2-blue1)/256;
    53.  
    54.     redfloat=red1;
    55.     greenfloat=green1;
    56.     bluefloat=blue1;
    57.  
    58.     for (x=0;x<XSIZE;x++)
    59.     {
    60.         for (y=0;y<YSIZE;y++)
    61.         {
    62.             image[y][x][RED]=redfloat;
    63.             image[y][x][GREEN]=greenfloat;
    64.             image[y][x][BLUE]=bluefloat;    
    65.         }
    66.             redfloat=redfloat+redsize;  
    67.             greenfloat=greenfloat+greensize;
    68.             bluefloat=bluefloat+bluesize;
    69.     }
    70.  
    71.    
    72.         printf("\tEnter Start X-Coordinate: ");
    73.         scanf("%f", &x1);
    74.    
    75.        
    76.    
    77.         printf("\tEnter Start Y-Coordinate : ");
    78.         scanf("%f", &y1);
    79.        
    80.  
    81.         printf("\tEnter End X-Coordinate: ");
    82.         scanf("%f", &x2);
    83.    
    84.  
    85.         printf("\tEnter End Y-Coordinate : ");
    86.         scanf("%f", &y2);
    87.        
    88.  
    89.     printf("\tPlease enter the width of the rectangle: ");
    90.     scanf("%f", &width);
    91.  
    92.     if (x1 == x2)
    93.         grad = 10000000000;
    94.    
    95.         else
    96.         grad= (y2-y1)/(x2-x1);
    97.    
    98.     y3 = (float)(y1 - width/(sqrt(1+grad*grad)));
    99.     x3 = (float)(x1 + (width*grad)/sqrt(1+grad*grad));
    100.     y4 = (float)(y2 - width/(sqrt(1+grad*grad)));
    101.     x4 = (float)(x2 + (width*grad)/sqrt(1+grad*grad));
    102.  
    103.  
    104.     if (grad == 0)
    105.         m = 10000000000;
    106.    
    107.     else    
    108.         m = (float)(-1/grad);
    109.  
    110.     C1=y1-grad*x1;
    111.     C2=y4-grad*x4;
    112.     C3=y1-m*x1;
    113.     C4=y4-m*x4;
    114.  
    115.    
    116.  
    117.             if (x1 > x2)
    118.             {
    119.                 xhigh = (int)x1;
    120.                 xlow = (int)x2;
    121.             }
    122.             else
    123.             {
    124.                 xhigh = (int)x2;
    125.                 xlow = (int)x1;
    126.             }
    127.             for (x=(int)xlow; x<=(int)xhigh; x++)
    128.             {
    129.                     y = grad*x+C1;
    130.                     image[y][x][RED] = 0;
    131.                     image[y][x][GREEN] = 0;
    132.                     image[y][x][BLUE] = 0;
    133.             }
    134.  
    135.  
    136.    
    137.             if (x3 > x4)
    138.             {
    139.                 xhigh = (int)x3;
    140.                 xlow = (int)x4;
    141.             }
    142.             else
    143.             {
    144.                 xhigh = (int)x4;
    145.                 xlow = (int)x3;
    146.             }
    147.             for (x=(int)xlow; x<=(int)xhigh; x++)
    148.                 {
    149.                     y = grad*x+C2;
    150.                     image[y][x][RED] = 0;
    151.                     image[y][x][GREEN] = 0;
    152.                     image[y][x][BLUE] = 0;
    153.                 }
    154.    
    155.  
    156.    
    157.  
    158.             if (x1 > x3)
    159.             {
    160.                 xhigh = (int)x1;
    161.                 xlow = (int)x3;
    162.             }
    163.             else
    164.             {
    165.                 xhigh = (int)x3;
    166.                 xlow = (int)x1;
    167.             }
    168.             for (x=(int)xlow; x<=(int)xhigh; x++)
    169.                 {
    170.                     y=m*x+C3;
    171.                     image[y][x][RED] = 0;
    172.                     image[y][x][GREEN] = 0;
    173.                     image[y][x][BLUE] = 0;
    174.                 }
    175.  
    176.  
    177.     if (x2 > x4)
    178.             {
    179.                 xhigh = (int)x2;
    180.                 xlow = (int)x4;
    181.             }
    182.             else
    183.             {
    184.                 xhigh = (int)x4;
    185.                 xlow = (int)x2;
    186.             }
    187.        
    188.     for (x=(int)xlow; x<=(int)xhigh; x++)
    189.                 {
    190.                         y = m*x+C4;
    191.                         image[y][x][RED] = 0;
    192.                         image[y][x][GREEN] = 0;
    193.                         image[y][x][BLUE] = 0;
    194.                 }
    195.                        
    196.    
    197.  
    198.    
    199.  
    200.     pfile = fopen("myfile.ppm", "w");
    201.    
    202.     fprintf(pfile, "P3\n#myfile.ppm\n256 128\n255\n");
    203.    
    204.     for (y=0;y<YSIZE;y++)
    205.     {
    206.         for (x=0;x<XSIZE;x++)
    207.         {
    208.             fprintf(pfile,"%d %d %d\n", image[y][x][RED],image[y][x][GREEN],image[y][x][BLUE]);
    209.         }
    210.     }
    211.  
    212.     fclose(pfile);
    213.  
    214.     getchar();
    215.     getchar();
    216. }
    I know its a long code, please help me with this.
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Why don't you try stepping through the code? Find the line that's giving out the problem, and see the variable values that's causing it.
     
  3. killer6008

    Thread Starter New Member

    Jan 26, 2010
    20
    0

    Just tried that. Made a new project and added this coding into a new file. Then i went through the file trying to take out as many warning and errors possible. Now I am not getting that same error but I am left with these:

    Code ( (Unknown Language)):
    1. 1>------ Build started: Project: Programming1, Configuration: Debug Win32 ------
    2. 1>Linking...
    3. 1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
    4. 1>C:\Documents and Settings\KK\Desktop\cprogram\Programming1\Debug\Programming1.exe : fatal error LNK1120: 1 unresolved externals
    5. 1>Build log was saved at "file://c:\Documents and Settings\KK\Desktop\cprogram\Programming1\Programming1\Debug\BuildLog.htm"
    6. 1>Programming1 - 2 error(s), 0 warning(s)
    7. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    This is new coding:

    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <math.h>
    3. #define XSIZE 256
    4. #define YSIZE 128
    5. #define RED 0
    6. #define GREEN 1
    7. #define BLUE 2
    8.  
    9. void main()
    10. {
    11.     int image[YSIZE][XSIZE][3];
    12.     int x,y;
    13.     int xlow,xhigh;
    14.     float x1,x2,x3,x4,y1,y2=0,y3=0,y4=0,width,C1,C2,C3,C4;
    15.     float red1,red2,green1,green2,blue1,blue2;
    16.     float redsize,greensize,bluesize;
    17.     float redfloat,greenfloat,bluefloat;
    18.     float m = 0, grad = 0;
    19.  
    20.     FILE *pfile = NULL;
    21.  
    22.  
    23.  
    24.         printf("\tEnter RED 1 value: ");
    25.         scanf_s("%f", &red1);
    26.  
    27.    
    28.  
    29.         printf("\tEnter GREEN 1 value: ");
    30.         scanf_s("%f", &green1);
    31.    
    32.  
    33.         printf("\tEnter BLUE 1 value: ");
    34.         scanf_s("%f", &blue1);
    35.  
    36.    
    37.         printf("\tEnter RED 2 value: ");
    38.         scanf_s("%f", &red2);
    39.        
    40.    
    41.         printf("\tEnter GREEN 2 value: ");
    42.         scanf_s("%f", &green2);
    43.        
    44.  
    45.         printf("\tEnter BLUE 2 value: ");
    46.         scanf_s("%f", &blue2);
    47.        
    48.  
    49.     redsize=(red2-red1)/256;
    50.     greensize=(green2-green1)/256;
    51.     bluesize=(blue2-blue1)/256;
    52.  
    53.     redfloat=red1;
    54.     greenfloat=green1;
    55.     bluefloat=blue1;
    56.  
    57.     for (x=0;x<XSIZE;x++)
    58.     {
    59.         for (y=0;y<YSIZE;y++)
    60.         {
    61.             image[y][x][RED]=(int)redfloat;
    62.             image[y][x][GREEN]=(int)greenfloat;
    63.             image[y][x][BLUE]=(int)bluefloat;    
    64.         }
    65.             redfloat=redfloat+redsize;  
    66.             greenfloat=greenfloat+greensize;
    67.             bluefloat=bluefloat+bluesize;
    68.     }
    69.  
    70.    
    71.         printf("\tEnter Start X-Coordinate: ");
    72.         scanf_s("%f", &x1);
    73.    
    74.        
    75.    
    76.         printf("\tEnter Start Y-Coordinate : ");
    77.         scanf_s("%f", &y1);
    78.        
    79.  
    80.         printf("\tEnter End X-Coordinate: ");
    81.         scanf_s("%f", &x2);
    82.    
    83.  
    84.         printf("\tEnter End Y-Coordinate : ");
    85.         scanf_s("%f", &y2);
    86.        
    87.  
    88.     printf("\tPlease enter the width of the rectangle: ");
    89.     scanf_s("%f", &width);
    90.  
    91.     if (x1 == x2)
    92.         grad = 10000000000;
    93.    
    94.         else
    95.         grad= (y2-y1)/(x2-x1);
    96.    
    97.     y3 = (float)(y1 - width/(sqrt(1+grad*grad)));
    98.     x3 = (float)(x1 + (width*grad)/sqrt(1+grad*grad));
    99.     y4 = (float)(y2 - width/(sqrt(1+grad*grad)));
    100.     x4 = (float)(x2 + (width*grad)/sqrt(1+grad*grad));
    101.  
    102.  
    103.     if (grad == 0)
    104.         m = 10000000000;
    105.    
    106.     else    
    107.         m = (float)(-1/grad);
    108.  
    109.     C1=y1-grad*x1;
    110.     C2=y4-grad*x4;
    111.     C3=y1-m*x1;
    112.     C4=y4-m*x4;
    113.  
    114.    
    115.  
    116.             if (x1 > x2)
    117.             {
    118.                 xhigh = (int)x1;
    119.                 xlow = (int)x2;
    120.             }
    121.             else
    122.             {
    123.                 xhigh = (int)x2;
    124.                 xlow = (int)x1;
    125.             }
    126.             for (x=(int)xlow; x<=(int)xhigh; x++)
    127.             {
    128.                     y = (int)(grad*x+C1) ;
    129.                     image[y][x][RED] = 0;
    130.                     image[y][x][GREEN] = 0;
    131.                     image[y][x][BLUE] = 0;
    132.             }
    133.  
    134.  
    135.    
    136.             if (x3 > x4)
    137.             {
    138.                 xhigh = (int)x3;
    139.                 xlow = (int)x4;
    140.             }
    141.             else
    142.             {
    143.                 xhigh = (int)x4;
    144.                 xlow = (int)x3;
    145.             }
    146.             for (x=(int)xlow; x<=(int)xhigh; x++)
    147.                 {
    148.                     y = (int)(grad*x+C2);
    149.                     image[y][x][RED] = 0;
    150.                     image[y][x][GREEN] = 0;
    151.                     image[y][x][BLUE] = 0;
    152.                 }
    153.    
    154.  
    155.    
    156.  
    157.             if (x1 > x3)
    158.             {
    159.                 xhigh = (int)x1;
    160.                 xlow = (int)x3;
    161.             }
    162.             else
    163.             {
    164.                 xhigh = (int)x3;
    165.                 xlow = (int)x1;
    166.             }
    167.             for (x=(int)xlow; x<=(int)xhigh; x++)
    168.                 {
    169.                     y= (int)(m*x+C3);
    170.                     image[y][x][RED] = 0;
    171.                     image[y][x][GREEN] = 0;
    172.                     image[y][x][BLUE] = 0;
    173.                 }
    174.  
    175.  
    176.     if (x2 > x4)
    177.             {
    178.                 xhigh = (int)x2;
    179.                 xlow = (int)x4;
    180.             }
    181.             else
    182.             {
    183.                 xhigh = (int)x4;
    184.                 xlow = (int)x2;
    185.             }
    186.        
    187.     for (x=(int)xlow; x<=(int)xhigh; x++)
    188.                 {
    189.                         y = (int)(m*x+C4);
    190.                         image[y][x][RED] = 0;
    191.                         image[y][x][GREEN] = 0;
    192.                         image[y][x][BLUE] = 0;
    193.                 }
    194.                        
    195.    
    196.  
    197.    
    198.  
    199.     pfile = fopen("myfile.ppm", "w");
    200.    
    201.     fprintf(pfile, "P3\n#myfile.ppm\n256 128\n255\n");
    202.    
    203.     for (y=0;y<YSIZE;y++)
    204.     {
    205.         for (x=0;x<XSIZE;x++)
    206.         {
    207.             fprintf(pfile,"%d %d %d\n", image[y][x][RED],image[y][x][GREEN],image[y][x][BLUE]);
    208.         }
    209.     }
    210.  
    211.     fclose(pfile);
    212.  
    213.     getchar();
    214.     getchar();
    215. }
     
  4. killer6008

    Thread Starter New Member

    Jan 26, 2010
    20
    0
    Okay after several tries I am back to where I started.

    It gives this error:

    Unhandled exception at 0x0040160e in programmingcwk.exe: 0xC0000005: Access violation writing location 0x0009aeac.

    The program stops at this line:
    image[y][x][RED] = 0;
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    994
    Check the values of y x and RED at the point. Make sure they are not exceeding the dimensions of the array. Remember everything is base 0.

    For example if image was dimensioned 1,1,1. y x and RED should never exceed zero.
     
  6. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    I'd add a watch to X & Y or a line to print their values before the line with the error, so you can see what the array indexes actually are and if one is out of bounds.
     
Loading...