Please help me with this C code?

Thread Starter

killer6008

Joined Jan 26, 2010
20
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:

Rich (BB code):
#include <stdio.h>
#include <math.h>
#define XSIZE 256
#define YSIZE 128
#define RED 0
#define GREEN 1
#define BLUE 2

void main()
{
    int image[YSIZE][XSIZE][3];
    int x,y;
    int xlow,xhigh;
    float x1,x2,x3,x4,y1,y2=0,y3=0,y4=0,width,C1,C2,C3,C4;
    float red1,red2,green1,green2,blue1,blue2;
    float redsize,greensize,bluesize; 
    float redfloat,greenfloat,bluefloat;
    float m = 0, grad = 0; 

    FILE *pfile = NULL;



        printf("\tEnter RED 1 value: ");
        scanf("%f", &red1);

    

        printf("\tEnter GREEN 1 value: ");
        scanf("%f", &green1);
    

        printf("\tEnter BLUE 1 value: ");
        scanf("%f", &blue1);

    
        printf("\tEnter RED 2 value: ");
        scanf("%f", &red2);
        
    
        printf("\tEnter GREEN 2 value: ");
        scanf("%f", &green2);
        

        printf("\tEnter BLUE 2 value: ");
        scanf("%f", &blue2);
        

    redsize=(red2-red1)/256;
    greensize=(green2-green1)/256;
    bluesize=(blue2-blue1)/256;

    redfloat=red1;
    greenfloat=green1;
    bluefloat=blue1;

    for (x=0;x<XSIZE;x++)
    {
        for (y=0;y<YSIZE;y++)
        {
            image[y][x][RED]=redfloat;
            image[y][x][GREEN]=greenfloat;
            image[y][x][BLUE]=bluefloat;    
        }
            redfloat=redfloat+redsize;  
            greenfloat=greenfloat+greensize;
            bluefloat=bluefloat+bluesize;
    }

    
        printf("\tEnter Start X-Coordinate: ");
        scanf("%f", &x1);
    
        
    
        printf("\tEnter Start Y-Coordinate : ");
        scanf("%f", &y1);
        

        printf("\tEnter End X-Coordinate: ");
        scanf("%f", &x2);
    

        printf("\tEnter End Y-Coordinate : ");
        scanf("%f", &y2);
        

    printf("\tPlease enter the width of the rectangle: ");
    scanf("%f", &width);

    if (x1 == x2)
        grad = 10000000000;
    
        else
        grad= (y2-y1)/(x2-x1);
    
    y3 = (float)(y1 - width/(sqrt(1+grad*grad)));
    x3 = (float)(x1 + (width*grad)/sqrt(1+grad*grad));
    y4 = (float)(y2 - width/(sqrt(1+grad*grad)));
    x4 = (float)(x2 + (width*grad)/sqrt(1+grad*grad));


    if (grad == 0)
        m = 10000000000;
    
    else    
        m = (float)(-1/grad);

    C1=y1-grad*x1;
    C2=y4-grad*x4;
    C3=y1-m*x1;
    C4=y4-m*x4;

    

            if (x1 > x2)
            {
                xhigh = (int)x1;
                xlow = (int)x2;
            }
            else
            {
                xhigh = (int)x2;
                xlow = (int)x1;
            }
            for (x=(int)xlow; x<=(int)xhigh; x++)
            {
                    y = grad*x+C1;
                    image[y][x][RED] = 0;
                    image[y][x][GREEN] = 0;
                    image[y][x][BLUE] = 0;
            }


    
            if (x3 > x4)
            {
                xhigh = (int)x3;
                xlow = (int)x4;
            }
            else
            {
                xhigh = (int)x4;
                xlow = (int)x3;
            }
            for (x=(int)xlow; x<=(int)xhigh; x++)
                {
                    y = grad*x+C2;
                    image[y][x][RED] = 0;
                    image[y][x][GREEN] = 0;
                    image[y][x][BLUE] = 0;
                }
    

    

            if (x1 > x3)
            {
                xhigh = (int)x1;
                xlow = (int)x3;
            }
            else
            {
                xhigh = (int)x3;
                xlow = (int)x1;
            }
            for (x=(int)xlow; x<=(int)xhigh; x++)
                {
                    y=m*x+C3;
                    image[y][x][RED] = 0;
                    image[y][x][GREEN] = 0;
                    image[y][x][BLUE] = 0;
                }


    if (x2 > x4)
            {
                xhigh = (int)x2;
                xlow = (int)x4;
            }
            else
            {
                xhigh = (int)x4;
                xlow = (int)x2;
            }
        
    for (x=(int)xlow; x<=(int)xhigh; x++)
                {
                        y = m*x+C4;
                        image[y][x][RED] = 0;
                        image[y][x][GREEN] = 0;
                        image[y][x][BLUE] = 0;
                }
                        
    

    

    pfile = fopen("myfile.ppm", "w");
    
    fprintf(pfile, "P3\n#myfile.ppm\n256 128\n255\n");
    
    for (y=0;y<YSIZE;y++)
    {
        for (x=0;x<XSIZE;x++)
        {
            fprintf(pfile,"%d %d %d\n", image[y][x][RED],image[y][x][GREEN],image[y][x][BLUE]);
        }
    }

    fclose(pfile);

    getchar();
    getchar();
}
I know its a long code, please help me with this.
 

n9352527

Joined Oct 14, 2005
1,198
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.
 

Thread Starter

killer6008

Joined Jan 26, 2010
20
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.

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:

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

Rich (BB code):
#include <stdio.h>
#include <math.h>
#define XSIZE 256
#define YSIZE 128
#define RED 0
#define GREEN 1
#define BLUE 2

void main()
{
    int image[YSIZE][XSIZE][3];
    int x,y;
    int xlow,xhigh;
    float x1,x2,x3,x4,y1,y2=0,y3=0,y4=0,width,C1,C2,C3,C4;
    float red1,red2,green1,green2,blue1,blue2;
    float redsize,greensize,bluesize; 
    float redfloat,greenfloat,bluefloat;
    float m = 0, grad = 0; 

    FILE *pfile = NULL;



        printf("\tEnter RED 1 value: ");
        scanf_s("%f", &red1);

    

        printf("\tEnter GREEN 1 value: ");
        scanf_s("%f", &green1);
    

        printf("\tEnter BLUE 1 value: ");
        scanf_s("%f", &blue1);

    
        printf("\tEnter RED 2 value: ");
        scanf_s("%f", &red2);
        
    
        printf("\tEnter GREEN 2 value: ");
        scanf_s("%f", &green2);
        

        printf("\tEnter BLUE 2 value: ");
        scanf_s("%f", &blue2);
        

    redsize=(red2-red1)/256;
    greensize=(green2-green1)/256;
    bluesize=(blue2-blue1)/256;

    redfloat=red1;
    greenfloat=green1;
    bluefloat=blue1;

    for (x=0;x<XSIZE;x++)
    {
        for (y=0;y<YSIZE;y++)
        {
            image[y][x][RED]=(int)redfloat;
            image[y][x][GREEN]=(int)greenfloat;
            image[y][x][BLUE]=(int)bluefloat;    
        }
            redfloat=redfloat+redsize;  
            greenfloat=greenfloat+greensize;
            bluefloat=bluefloat+bluesize;
    }

    
        printf("\tEnter Start X-Coordinate: ");
        scanf_s("%f", &x1);
    
        
    
        printf("\tEnter Start Y-Coordinate : ");
        scanf_s("%f", &y1);
        

        printf("\tEnter End X-Coordinate: ");
        scanf_s("%f", &x2);
    

        printf("\tEnter End Y-Coordinate : ");
        scanf_s("%f", &y2);
        

    printf("\tPlease enter the width of the rectangle: ");
    scanf_s("%f", &width);

    if (x1 == x2)
        grad = 10000000000;
    
        else
        grad= (y2-y1)/(x2-x1);
    
    y3 = (float)(y1 - width/(sqrt(1+grad*grad)));
    x3 = (float)(x1 + (width*grad)/sqrt(1+grad*grad));
    y4 = (float)(y2 - width/(sqrt(1+grad*grad)));
    x4 = (float)(x2 + (width*grad)/sqrt(1+grad*grad));


    if (grad == 0)
        m = 10000000000;
    
    else    
        m = (float)(-1/grad);

    C1=y1-grad*x1;
    C2=y4-grad*x4;
    C3=y1-m*x1;
    C4=y4-m*x4;

    

            if (x1 > x2)
            {
                xhigh = (int)x1;
                xlow = (int)x2;
            }
            else
            {
                xhigh = (int)x2;
                xlow = (int)x1;
            }
            for (x=(int)xlow; x<=(int)xhigh; x++)
            {
                    y = (int)(grad*x+C1) ;
                    image[y][x][RED] = 0;
                    image[y][x][GREEN] = 0;
                    image[y][x][BLUE] = 0;
            }


    
            if (x3 > x4)
            {
                xhigh = (int)x3;
                xlow = (int)x4;
            }
            else
            {
                xhigh = (int)x4;
                xlow = (int)x3;
            }
            for (x=(int)xlow; x<=(int)xhigh; x++)
                {
                    y = (int)(grad*x+C2);
                    image[y][x][RED] = 0;
                    image[y][x][GREEN] = 0;
                    image[y][x][BLUE] = 0;
                }
    

    

            if (x1 > x3)
            {
                xhigh = (int)x1;
                xlow = (int)x3;
            }
            else
            {
                xhigh = (int)x3;
                xlow = (int)x1;
            }
            for (x=(int)xlow; x<=(int)xhigh; x++)
                {
                    y= (int)(m*x+C3);
                    image[y][x][RED] = 0;
                    image[y][x][GREEN] = 0;
                    image[y][x][BLUE] = 0;
                }


    if (x2 > x4)
            {
                xhigh = (int)x2;
                xlow = (int)x4;
            }
            else
            {
                xhigh = (int)x4;
                xlow = (int)x2;
            }
        
    for (x=(int)xlow; x<=(int)xhigh; x++)
                {
                        y = (int)(m*x+C4);
                        image[y][x][RED] = 0;
                        image[y][x][GREEN] = 0;
                        image[y][x][BLUE] = 0;
                }
                        
    

    

    pfile = fopen("myfile.ppm", "w");
    
    fprintf(pfile, "P3\n#myfile.ppm\n256 128\n255\n");
    
    for (y=0;y<YSIZE;y++)
    {
        for (x=0;x<XSIZE;x++)
        {
            fprintf(pfile,"%d %d %d\n", image[y][x][RED],image[y][x][GREEN],image[y][x][BLUE]);
        }
    }

    fclose(pfile);

    getchar();
    getchar();
}
 

Thread Starter

killer6008

Joined Jan 26, 2010
20
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;
 

spinnaker

Joined Oct 29, 2009
7,830
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;
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.
 

rjenkins

Joined Nov 6, 2005
1,013
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.
 
Top