On Thursday, 28 January 2021 at 18:37:37 UTC, Ruby The Roobster wrote:
Here is the output/input of the program:
Type in  data for an egg:
Width: 3
Hight: 2

object.Error@(0): Integer Divide by Zero
...

Here is the source code:

import std.stdio;
import std.string;
void main(){
    egg[1000] data;
    data[0] = (new egg(0,0,"a"));
    for(int i = 1;i<1000;i++)
    {
        ...
        data[i] = new egg(tempx,tempy,tempz);
        for(int x = 0;x < i;x++)
        {
            int tempa;
            int tempb;
            double temp;
            tempa = data[x].x;
            if(tempa < 0)
                tempa-=tempa;
            tempb = data[x].y;
            if(tempb < 0)
                tempb-=tempb;
            temp = tempa / tempb;
            if(temp > highs)
            {
                highs = temp;
                high = x;
            }
        }
...

Why is this happening? Does anybody know?

1) Outside the loops, you set `data[0] = (new Egg(0, 0, "a"))`

2) The outer `for(int i` loop, starts at `i = 1`, and so it never overwrites the work of step (1).

3) The inner `for(int x` loop starts at `x = 0`, so its first pass divides by `data[1].y`. Since you set that field equal to 0 in step (1), and both operands have type `uint`, the result is a divide by zero error.

4) Also, `if(tempb < 0) tempb-=tempb;` will convert any negative values into zeroes. Maybe you meant to write `if(tempb < 0) tempb = -tempb;` to get the absolute value, instead?

There are many ways you could fix the program. (I won't try to guess what the best way is, though, because it's not entirely clear to me what this program is actually intended to do. As is, it does some calculations, but doesn't try to output their results in any way.)

Reply via email to