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:
I know its a long code, please help me with this.
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();
}