# overloaded method while trying to run a GCD calculator

Page 1 of 1

## 6 Replies - 1422 Views - Last Post: 03 October 2012 - 12:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294116&amp;s=d511078aa1e0366e4c7751e126a38b74&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iScopeuDrop

Reputation: 0
• Posts: 3
• Joined: 03-October 12

# overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 09:48 AM

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the calculator");
{
}
Console.WriteLine("Thanks you, goodbye");
}

static char GetOrder()
{
{
}
}

{
Console.WriteLine("F - Factorial");
Console.WriteLine("C - Combinations");
Console.WriteLine("I - Fibonacci");
Console.WriteLine("X - Exit");
}

{
{
case 'F':
ProcessFactorial ();
break;
case 'I':
ProcessFibonacci ();
break;
case 'C':
ProcessCombinations();
break;
}
}

static void ProcessCombinations()
{
Int32 n, r, nChooseR;
n = GetUnsignedInt("Enter number of objects to choose from");
r = GetUnsignedInt("Enter number of objects to choose");
while (r > n)
{
Console.WriteLine("Error - must be less than or equal to {0}", n);
r = GetUnsignedInt("Enter number of objects to choose");
}
nChooseR = Combinations(n, r);
Console.WriteLine("There are {0} ways to choose {1} of {2} objects", nChooseR, r, n);
}

static Int32 Combinations(Int32 n, Int32 r)
{
return Factorial(n) / (Factorial(r) * Factorial(n - r));
}

static void ProcessFibonacci()
{
Int32 n, nthFibonacci;
n = GetUnsignedInt("Enter position of fibonacci sequence");
nthFibonacci = Fibonacci(n - 1);
Console.WriteLine("The fibonacci number at position {0} is {1}", n, nthFibonacci);
}

static Int32 Fibonacci(Int32 n)
{
Int32 count = 0, current = 0, previous = 1, previousPrevious = 0;
for (count = 0; count < n; count++)
{
previousPrevious = previous;
previous = current;
current = previous + previousPrevious;
}
return current;
}

static void ProcessFactorial()
{
Int32 n, nFactorial;
n = GetUnsignedInt("Enter n value");
nFactorial = Factorial(n);
Console.WriteLine("{0} factorial is {1}", n, nFactorial);
}

static Int32 Factorial(Int32 n)
{
Int32 nFactorial;
for (nFactorial = 1; n > 1; n--)
nFactorial *= n;
return nFactorial;
}

static Int32 GetUnsignedInt(string prompt)
{
Int32 n;
Console.WriteLine(prompt);
while (n < 0)
{
Console.WriteLine("Error: enter unsigned value");
Console.WriteLine(prompt);
}
return n;
}

static void processgcd()
{
Int32 x, y, greatestCommonDivisor;
greatestCommonDivisor = Divisor(x, y);
Console.WriteLine("The greatest common divisor of {0} and {1} is {2}", x, y, greatestCommonDivisor);
}

static Int32 Divisor(Int32 x, y)
{
Int32 x, y, divident, divisor, remainder, previousRemainder;
divident = Math.Max(Math.Abs(x), Math.Abs(y));
divisor = Math.Min(Math.Abs(x), Math.Abs(y));
remainder = divident % divisor;
previousRemainder = divisor;
while (remainder == 0)
{
divident = divisor;
divisor = remainder;
previousRemainder = remainder;
remainder = divident % divisor;
}
return previousRemainder;

}
}

```

at this point i'm getting 6 error.
1 Identifier expected
2 }expected

attached is the flow chart for the GCD calculator.

#### Attached image(s)

Is This A Good Question/Topic? 0

## Replies To: overloaded method while trying to run a GCD calculator

### #2 Skydiver

• Code herder

Reputation: 3921
• Posts: 12,426
• Joined: 05-May 12

## Re: overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 10:36 AM

It would help if you pasted the errors in verbatim including the line numbers.

Glancing at the code, I suspect that it is due to the missing type specifier for the parameter y on line 144.

### #3 h4nnib4l

• The Noid

Reputation: 1182
• Posts: 1,677
• Joined: 24-August 11

## Re: overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 10:58 AM

That same method that Skydiver referenced is also missing its closing curly brace.

EDIT: starting on line 144

This post has been edited by h4nnib4l: 03 October 2012 - 10:59 AM

### #4 iScopeuDrop

Reputation: 0
• Posts: 3
• Joined: 03-October 12

## Re: overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 11:07 AM

alright i fixed that problem but now whenever i enter two values for x and y that end with a 0, i get the error "attempted to divide by zero" and when i simply enter in two random values, the final answer always comes up as the first value i entered.
updated code
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the calculator");
{
}
Console.WriteLine("Thanks you, goodbye");
}

static char GetOrder()
{
{
}
}

{
Console.WriteLine("F - Factorial");
Console.WriteLine("C - Combinations");
Console.WriteLine("I - Fibonacci");
Console.WriteLine("D - Greatest Common Divisor");
Console.WriteLine("X - Exit");
}

{
{
case 'F':
ProcessFactorial();
break;
case 'I':
ProcessFibonacci();
break;
case 'C':
ProcessCombinations();
break;
case 'D':
processgcd();
break;
}
}

static void ProcessCombinations()
{
Int32 n, r, nChooseR;
n = GetUnsignedInt("Enter number of objects to choose from");
r = GetUnsignedInt("Enter number of objects to choose");
while (r > n)
{
Console.WriteLine("Error - must be less than or equal to {0}", n);
r = GetUnsignedInt("Enter number of objects to choose");
}
nChooseR = Combinations(n, r);
Console.WriteLine("There are {0} ways to choose {1} of {2} objects", nChooseR, r, n);
}

static Int32 Combinations(Int32 n, Int32 r)
{
return Factorial(n) / (Factorial(r) * Factorial(n - r));
}

static void ProcessFibonacci()
{
Int32 n, nthFibonacci;
n = GetUnsignedInt("Enter position of fibonacci sequence");
nthFibonacci = Fibonacci(n - 1);
Console.WriteLine("The fibonacci number at position {0} is {1}", n, nthFibonacci);
}

static Int32 Fibonacci(Int32 n)
{
Int32 count = 0, current = 0, previous = 1, previousPrevious = 0;
for (count = 0; count < n; count++)
{
previousPrevious = previous;
previous = current;
current = previous + previousPrevious;
}
return current;
}

static void ProcessFactorial()
{
Int32 n, nFactorial;
n = GetUnsignedInt("Enter n value");
nFactorial = Factorial(n);
Console.WriteLine("{0} factorial is {1}", n, nFactorial);
}

static Int32 Factorial(Int32 n)
{
Int32 nFactorial;
for (nFactorial = 1; n > 1; n--)
nFactorial *= n;
return nFactorial;
}

static Int32 GetUnsignedInt(string prompt)
{
Int32 n;
Console.WriteLine(prompt);
while (n < 0)
{
Console.WriteLine("Error: enter unsigned value");
Console.WriteLine(prompt);
}
return n;
}

static void processgcd()
{
Int32 x, y, greatestCommonDivisor;
greatestCommonDivisor = Divisor(x, y);
Console.WriteLine("The greatest common divisor of {0} and {1} is {2}", x, y, greatestCommonDivisor);
}

static Int32 Divisor(Int32 x, Int32 y)
{
Int32 divident, divisor, remainder, previousRemainder;
divident = Math.Max(Math.Abs(x), Math.Abs(y));
divisor = Math.Min(Math.Abs(x), Math.Abs(y));
remainder = divident % divisor;
previousRemainder = divisor;
while (remainder == 0)
{
divident = divisor;
divisor = remainder;
previousRemainder = remainder;
remainder = divident % divisor;
}
return previousRemainder;

}
}
}

```

### #5 Momerath

• D.I.C Lover

Reputation: 1012
• Posts: 2,451
• Joined: 04-October 09

## Re: overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 11:36 AM

Line 155 should be != 0

And your method is overly complicated, GCD is
```int GCD(int a, int B)/> {
if (b == 0) return a;
return GCD(b, a % b );
}
```

This post has been edited by Momerath: 03 October 2012 - 11:37 AM

### #6 h4nnib4l

• The Noid

Reputation: 1182
• Posts: 1,677
• Joined: 24-August 11

## Re: overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 11:37 AM

Let's step through your Divisor function.

You assign the larger of the numbers to divident, and the smaller to divisor. Then you assign the remained of the divide function to remainder. Then you put divisor into previousRemainder. Now, your while statement will only be evaluated if the divisor is a factor of divident. If the remainder isn't 0, you then return previousRemainder (which is really just divisor). So if the smaller number isn't a factor of the larger number, you return the smaller number, which is wrong. If the modulus does evaluate to 0, it gets even weirder. You assign divisor's value to divident, then you assign remainder (which we know is equal to 0 because of the while condition) to divisor. You also set previousRemainder equal to remainder (0), and then try to divide divisor's original value (stored in divident now) by it's new value (which is 0 due to the assignment you made). That's where your divide by 0 error comes from.

First, you need to read the MSDN documentation on the modulus operator, then you need to seriously reexamine your logic in the Divisor function.

EDIT: Bit of a brain fart, took out something that didn't make sense. Once I looked at Momerath's solution, I realized that your while loop is just trying to recreate a simple recursive method. I wasn't a CS major, so I never programmed a lot of the mathematical functions that CS majors usually do...

This post has been edited by h4nnib4l: 03 October 2012 - 12:10 PM

### #7 iScopeuDrop

Reputation: 0
• Posts: 3
• Joined: 03-October 12

## Re: overloaded method while trying to run a GCD calculator

Posted 03 October 2012 - 12:39 PM

I've only been programming for 2 weeks now and I really must say thanks to all of you.