Integer division by zero

-4

The error returned is "Unhandled exception at 0x01355144 in Homework_3_Problem_15.exe: 0xC0000094: Integer division by zero."

I can see that the variable 'greatestCommonDivisor' is being assigned a value of 0, but I can't figure out why.

Here's my code:

#include "stdafx.h"
#include <iostream>
using namespace std;

int greatestCommonDivisor;

void inputNumbers(int& numerator, int& denominator);
int convertToGreatestCommonDivisor(int numerator, int denominator);
void convertToLowestTerms(int& numerator, int& denominator);

int _tmain(int argc, _TCHAR* argv[])
{
    int numerator = 1;
    int denominator = 1;

    inputNumbers(numerator, denominator);
    convertToGreatestCommonDivisor(numerator, denominator);
    convertToLowestTerms(numerator, denominator);

    return 0;
}

void inputNumbers(int& numerator, int& denominator)
{
    cout << "\n";
    cout << "Please enter the numerator: ";
    cin >> numerator;
    cout << "Please enter the denominator: ";
    cin >> denominator;
    cout << "\n";
    return;
}

int convertToGreatestCommonDivisor(int numerator, int denominator)
{
    int greatestCommonDivisor;
    int i;
    // checks if i is greater than the numerator *and* the denominator
    for (i = 1; i <= numerator && i <= denominator; i++)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            greatestCommonDivisor = i;
        }
    }
    return (greatestCommonDivisor);
}

void convertToLowestTerms(int& numerator, int& denominator)
{
    cout << "The fraction " << numerator << " / " << denominator << " reduces to " <<
    (numerator / greatestCommonDivisor) << " / " << (denominator / greatestCommonDivisor);
}

Any help you can give would be greatly appreciated.

c++
integer-division
asked on Stack Overflow Jun 19, 2014 by user3654700 • edited Jun 19, 2014 by WhozCraig

4 Answers

3

You seem to misunderstand how returning from a function works.

In convertToGreatestCommonDivisor, you have a local variable called greatestCommonDivisor. You assign some values to this and then return it. When you call convertToGreatestCommonDivisor(numerator, denominator);, you are ignoring this return value.

Then, convertToLowestTerms uses the global variable greatestCommonDivisor that is declared on line 5. This variable has value 0.

It looks like you probably want to store the value returned from convertToGreatestCommonDivisor and then pass it to convertToLowestTerms:

int greatestCommonDivisor = convertToGreatestCommonDivisor(numerator, denominator);
convertToLowestTerms(greatestCommonDivisor, numerator, denominator);

This requires adding an additional parameter to convertToLowestTerms. The global variable is not necessary at all.

answered on Stack Overflow Jun 19, 2014 by Joseph Mansfield
1
int greatestCommonDivisor;

Is global and if uninitialized, is automatically equal to 0. I think you meant to do this:

greatestComonDivisor = convertToGreatestCommonDivisor(numerator, denominator);
answered on Stack Overflow Jun 19, 2014 by yizzlez
1

Since greatestCommonDivisor is global, simply you can change your convertToGreatestCommonDivisor function to

void convertToGreatestCommonDivisor(int numerator, int denominator)  //Change return type to void
{
    // Remove the local variable "greatestCommonDivisor"
    int i;
    // checks if i is greater than the numerator *and* the denominator
    for (i = 1; i <= numerator && i <= denominator; i++)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            greatestCommonDivisor = i;
        }
    }
    //Remove the return statement
}
answered on Stack Overflow Jun 19, 2014 by haccks • edited Jun 19, 2014 by haccks
0

You have 2 greatestCommonDivisor variables one is global one is local. Global one is always 0,undefined or holds some random value depending on your environment build type etc.

answered on Stack Overflow Jun 19, 2014 by Barış Uşaklı

User contributions licensed under CC BY-SA 3.0