-4

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));
    } 

    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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Pooja N Babu
  • 347
  • 3
  • 5
  • 15
  • 6
    well, help us help you - where is the error??? – Daniel Mošmondor May 15 '11 at 12:38
  • 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. – Pooja N Babu May 15 '11 at 12:44
  • 1
    I guess it's for the sake of the example, but remember to free the memory that you have malloc'ed. – Francesco May 15 '11 at 12:44
  • 2
    @hamsa: What exact error do you get? What is the error output? What unexpected things happen? What did you expect to happen instead? – sth May 15 '11 at 12:49
  • 1
    Why not tell us what the contents of cube.txt looks like so that folks can have a fighting chance of running the code – Kev May 15 '11 at 12:49
  • 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. – Pooja N Babu May 15 '11 at 12:59
  • 1
    do you always write code in such a horrible way? never heard about 'indentation'?? – BlackBear May 15 '11 at 13:12
  • @BlackBear - to give hamsa the benefit of doubt, there was probably trouble pasting code here. OTOH hamsa can aspire for IOCCC :P – hawk May 15 '11 at 13:16
  • @hawk: lol :) he might like this, too: http://www.thc.org/root/phun/unmaintain.html – BlackBear May 15 '11 at 13:17
  • @BlacKBear: I am new to this. I had a trouble pasting the code. THE ORIGINAL CODE WAS PROPERLY INDENTED. SORRY – Pooja N Babu May 15 '11 at 13:41
  • @hamsa: don't worry, I was kidding. I had problems too when first came to this site. I didn't want to offend you ;) – BlackBear May 15 '11 at 14:18

2 Answers2

1

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.

MELWIN
  • 1,093
  • 4
  • 12
  • 19
1

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;
    }
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Thanks for suggestions. my cube.txt looks like this. It doesn't have any z value set to 0.Its content is as follows- 6,4 400,200,2 600,200,2 750,450,3 600,600,4 400,200,2 600,200,2 600,400,2 600,600,3 750,450,3 600,200,2 600,400,2 500,500,2 600,600,3 600,400,2 500,500,2 450,750,3 600,600,4 400,200,2 600,600,3 450,750,3 600,600,4 750,450,3 500,500,2 450,750,3 Hence, I thought there can't divide by zero error. – Pooja N Babu May 15 '11 at 13:38
  • The problem may be in your file reading code though - please try the printf debug suggestion and if it catches a 0 then you can work back from there. – Paul R May 15 '11 at 14:33