divide error in C
I have a graphics program which has some computations in it. its not giving any errors but after the program is run, its giving 6/4 divide error. Can anyone help me with this?
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
struct face
{
int **vertices;
};
void main()
{
int gd = DETECT,gm;
int x, y, z, i, j, k, m, n;
struct face *f_3d, *f;
FILE *fp;
char c;
initgraph(&gd, &gm, "d:/tc/bgi");
fp=fopen("cube.txt", "r");
clrscr();
if(fp == NULL)
{
printf("\nFile doesn't exist");
exit(0);
}
fscanf(fp, "%d", &m);
fseek(fp, 1, SEEK_CUR); //no of faces
fscanf(fp, "%d", &n); //no of vertices for each face
printf("\n%d %d", m, n);
f = (struct face *) malloc(sizeof(m*sizeof(struct face)));
f_3d = (struct face *) malloc(sizeof(m*sizeof(struct face)));
for(i = 0; i < m; i++)
{
f[i].vertices = (int **) malloc(n*sizeof(int));
f_3d[i].vertices = (int **) malloc(n*sizeof(int));
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
{
f[i].vertices[j] = (int *) malloc(2*sizeof(int));
f_3d[i].vertices[j] = (int *) malloc(3*sizeof(int));
}开发者_StackOverflow社区
for(i = 0; i < m; i++) //for every face
for(j = 0;j < n; j++) // for every vertex in the face
{
fscanf(fp, "%d", &f_3d[i].vertices[j][0]); //read x coord
fseek(fp, 1, SEEK_CUR); //to skip comma
fscanf(fp, "%d", &f_3d[i].vertices[j][1]); //read y coord
fseek(fp, 1, SEEK_CUR);
fscanf(fp, "%d" ,&f_3d[i].vertices[j][2]); //read y coord
fseek(fp, 1, SEEK_CUR);
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
{
x=f_3d[i].vertices[j][0];
y=f_3d[i].vertices[j][1];
z=f_3d[i].vertices[j][2];
f[i].vertices[j][0]=(int)x/z;
f[i].vertices[j][1]=(int)y/z;
}
for(i = 0; i < 6; i++)
{
for(j = 0; j < 3; j++)
line(f[i].vertices[j][0], f[i].vertices[j][1], f[i].vertices[j+1][0], f[i].vertices[j+1][1]);
line(f[i].vertices[j][0], f[i].vertices[j][1], f[i].vertices[0][0], f[i].vertices[0][1]);
}
getch();
}
EDIT: Added additional information from comments:
All the values of the array assigned to x
and y
are integer values. So the final value assigned should also be an integer only. So, I wanted to know the reason for the divide error.
cube.txt
contains vertices of the form 400,200,2
, 300,400,2
etc. I am running this program in Turbo C. I am doing 400/2
and 200/2
(where x=400
, y=200
and z=2
) and assigning it to f[i].vertices[j][0]=x/z
and f[i].vertices[j][1]=y/z?
. There is no error as such. But when the program is run, the output is not seen. I expect to see the output of f[i].Vertices
array.
My guess is that the problem is here:
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
x=f_3d[i].vertices[j][0];
y=f_3d[i].vertices[j][1];
z=f_3d[i].vertices[j][2];
f[i].vertices[j][0]=(int)x/z;
f[i].vertices[j][1]=(int)y/z;
}
You probably have z = 0 at some point. Try adding some debug code:
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
x=f_3d[i].vertices[j][0];
y=f_3d[i].vertices[j][1];
z=f_3d[i].vertices[j][2];
if (z == 0)
{
printf("ERROR: i = %d, j = %d, x = %d, y = %d, z = %d\n", i, j, x, y, z);
exit(1);
}
f[i].vertices[j][0]=(int)x/z;
f[i].vertices[j][1]=(int)y/z;
}
The error "divide error
" occurs when some division is done by 0 'zero'
. Try to identify the place where such division has occurred and modify those part.
精彩评论