6 Replies - 1284 Views - Last Post: 03 October 2012 - 12:39 PM Rate Topic: -----

#1 iScopeuDrop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
        {
            char menuItem;
            Console.WriteLine("Welcome to the calculator");
            menuItem = GetOrder();
            while (menuItem != 'X')
            {
                ProcessOrder(menuItem);
                menuItem = GetOrder();
            }
            Console.WriteLine("Thanks you, goodbye");
            Console.ReadLine();
        }

        static char GetOrder()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            while (menuItem != 'F' && menuItem != 'C' &&
                menuItem != 'I' && menuItem != 'X')
            {
                Console.WriteLine("Error - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
            Console.WriteLine("Please pick an item:");
            Console.WriteLine("F - Factorial");
            Console.WriteLine("C - Combinations");
            Console.WriteLine("I - Fibonacci");
            Console.WriteLine("X - Exit");
        }

        static void ProcessOrder(char menuItem)
        {
            switch (menuItem)
            {
                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);
            n = Int32.Parse(Console.ReadLine ());
            while (n < 0)
            {
                Console.WriteLine("Error: enter unsigned value");
                Console.WriteLine(prompt);
                n = Int32.Parse(Console.ReadLine());
            }
            return n;
        }

        static void processgcd()
        {
            Int32 x, y, greatestCommonDivisor;
            Console.WriteLine("Please enter the first value");
            x = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the second value");
            y = int.Parse(Console.ReadLine());
            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)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

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

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • 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

Was This Post Helpful? 2
  • +
  • -

#4 iScopeuDrop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
        {
            char menuItem;
            Console.WriteLine("Welcome to the calculator");
            menuItem = GetOrder();
            while (menuItem != 'X')
            {
                ProcessOrder(menuItem);
                menuItem = GetOrder();
            }
            Console.WriteLine("Thanks you, goodbye");
            Console.ReadLine();
        }

        static char GetOrder()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            while (menuItem != 'F' && menuItem != 'C' &&
                menuItem != 'I' && menuItem != 'D' && menuItem != 'X')
            {
                Console.WriteLine("Error - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
            Console.WriteLine("Please pick an item:");
            Console.WriteLine("F - Factorial");
            Console.WriteLine("C - Combinations");
            Console.WriteLine("I - Fibonacci");
            Console.WriteLine("D - Greatest Common Divisor");
            Console.WriteLine("X - Exit");
        }

        static void ProcessOrder(char menuItem)
        {
            switch (menuItem)
            {
                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);
            n = Int32.Parse(Console.ReadLine());
            while (n < 0)
            {
                Console.WriteLine("Error: enter unsigned value");
                Console.WriteLine(prompt);
                n = Int32.Parse(Console.ReadLine());
            }
            return n;
        }

        static void processgcd()
        {
            Int32 x, y, greatestCommonDivisor;
            Console.WriteLine("Please enter the first value");
            x = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the second value");
            y = int.Parse(Console.ReadLine());
            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;


        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • 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

Was This Post Helpful? 2
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 iScopeuDrop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1