Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 LowestCommonMultiple
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Viper




PostPosted: Thu Nov 10, 2005 1:57 pm   Post subject: LowestCommonMultiple

**Recursion must be used**
The LCM (lowest common multiple ) of two non-zero integers can be found using the formula: lcm(num1, num2) = num1*num2/ gcd(num1, num2) where: gcd(num1, num2) is the greatest common divisor of num1 and num2. here's the trick. the program should employ a recursive approach to finding the gcd of two number. The recursive definition is given by:
if num2 divides evenly into num1 then
gcd= num2

could someone re-write tht in C++ for me plz Very Happy

here it is in turing
code:
var lcm, gcd : real := 0
var num1, num2 : int := 0
%------------------------------------------------------------------------------------------
proc starter
    put "Enter the numbers you wish to find the 'LCM' for"
    put "***********Hit enter after each number**********"
    get num1
    get num2
end starter
%------------------------------------------------------------------------------------------
proc GCD (num1, num2 : real)
    if num1 mod num2 = 0 then
        gcd := num2
    else
        GCD (num2, num1 mod num2)
    end if
end GCD
%------------------------------------------------------------------------------------------
proc LCM (gcd, num1, num2 : real)
    lcm := num1 * num2 / gcd
    put "The 'LCM' for", " ", num1, " ", "&", " ", num2, " ", "is", " ", lcm
    put "The 'GCD' for", " ", num1, " ", "&", " ", num2, " ", "is", " ", gcd
end LCM

%MAIN-----------------------------------
starter
GCD (num1, num2)
LCM (gcd, num1, num2)
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Thu Nov 10, 2005 4:17 pm   Post subject: (No subject)

No. Try it yourself first.
Geminias




PostPosted: Fri Nov 11, 2005 6:12 pm   Post subject: hey

here you go Razz



syntax = c++
code:

#include <iostream>

void intro ();
float module ();
float lcm (float GCD);
void intro ()
{
     int num1 = 0, num2 = 0, N1 = 0, N2 = 0;
     int *pNum1 =  num1;
     int *pNum2 =  num2;
     int *pN1 =  N1;
     int *pN1 =  N2;
     
     
     std::cout << "Enter the numbers you wish to find the LCM for" << std::endl  <<
     "********Hit enter after each number*********<< std::endl << std::endl;
     
     std::cin >> num1;
     std::cin >> num2;
     N1 = num1;
     N2 = num2;
}

float module ()
{
      float gcd;
 
     if (*pNum1 % *pNum2 == o)
   {
        gcd = *pNum2;
   }
     else
   {   
       *pNum1 = *pNum2;
       *pNum2 = *pNum1 % *pNum2;
       module ();
   }
   
   return gcd;
}

float lcm(float GCD)
{
      lcm = *pNum2 * (*pNum1) / GCD;
      return lcm;
}

int main ()
{
    intro();
    std::cout << "The LCM for " << *pN1 << " and " << *pN2 << " is " << lcm (module()) << std::endl;
    std::cout << "The GCM for " << *pN1 << " and " << *pN2 << " is " << lcm (module()) << std::endl;
   



wtd




PostPosted: Fri Nov 11, 2005 6:25 pm   Post subject: (No subject)

Wow...

c++:
float module ()
{
   float gcd;
 
   if (*pNum1 % *pNum2 == o)
   {
      gcd = *pNum2;
   }
   else
   {   
      *pNum1 = *pNum2;
      *pNum2 = *pNum1 % *pNum2;
      module ();
   }
   
   return gcd;
}


Where do you think pNum1 and pNum2 are coming from? I don't see a declaration for them in the function. Nor do I see a declaration for them in the global scope.

c++:
std::cout << "Enter the numbers you wish to find the LCM for" << std::endl
     "********Hit enter after each number*********<< std::endl << std::endl;


You have this, which makes no sense, since you're missing an insertion operator.

You don't have a closing brace at the end of "main".

Is there any particular reason you thought this code would work?
Geminias




PostPosted: Fri Nov 11, 2005 6:36 pm   Post subject: (No subject)

yeah i honestly thought pointers would last forever until i deleted them lol

i just tested and realized they don't...

plus yeah there is a whole score of syntax errors
wtd




PostPosted: Fri Nov 11, 2005 6:39 pm   Post subject: (No subject)

Research the concept of "scope".
Geminias




PostPosted: Fri Nov 11, 2005 8:29 pm   Post subject: (No subject)

i think i understand scope but i misunderstood how a pointer works. i thought the specific purpose of a pointer was to access private data. Which, maybe it is, but i should declare the pointers as global. i'll check as soon as i have time
wtd




PostPosted: Fri Nov 11, 2005 8:36 pm   Post subject: (No subject)

Geminias wrote:
i think i understand scope but i misunderstood how a pointer works. i thought the specific purpose of a pointer was to access private data. Which, maybe it is, but i should declare the pointers as global. i'll check as soon as i have time


No, you should not declare variables (a pointer is just a variable) globally. Learn how to pass things via arguments.
Sponsor
Sponsor
Sponsor
sponsor
Geminias




PostPosted: Sat Nov 12, 2005 11:46 am   Post subject: heyho

hey again, yeah i dropped out the idea of pointers and functions for this.
it finds the lowest common multiple and the highest common multiple (LCM) (GCM) but it excludes 1. Because 1 divides into everything.
in the case that only one number besides 1 is common to both numbers, then LCM = GCM.

Viper, your recursion technique did not work. It does not find the LCM and GCM.

try this.... (note: if one of the numbers is prime the program crashes.)

syntax = c++
code:
#include <iostream>

int main ()
{
    unsigned long int num1, num2, gcm,lcm;
    std::cout << "Enter two numbers starting with the highest one." << std::endl <<
    "*********hit enter after each number**************" << std::endl << std:: endl;
   
    std::cin >> num1;
    std::cin >> num2;
    gcm = 1;
    for (int i = num2; i >= gcm ; i--)
  {
        if (num2 % i == 0 && num1 % i == 0)
     {   gcm = i; 
     }
     else if (i = 1)
    {   
        gcm = 1
     }

  }
     for (int i = 2; i <= num2; i++)
   {
         if (num2 % i == 0 && num1 % i ==0)
         {
              lcm = i;
              i = num2;
         }
         else if (i = num2)
        {
             lcm = 1;
         }
   }

   std::cout << "The LCM is: " << lcm << std::endl;
   std::cout << "The GCM is: " << gcm << std::endl;
   std::cin.ignore(1,'\n');
   std::cin.ignore();
   
 
   return 0;
}       
   
wtd




PostPosted: Sat Nov 12, 2005 12:38 pm   Post subject: (No subject)

What is "Greatest Common Multiple"? Infinity?

code:
$ ./a.ut
Enter two numbers starting with the highest one.
*********hit enter after each number**************

4
3
The LCM is: 0
The GCM is: 1


For a cleaned up version of your code.

c++:
#include <iostream>

int main ()
{
    unsigned long int num1, num2, gcm,lcm;
    std::cout << "Enter two numbers starting with the highest one."   << std::endl
              << "*********hit enter after each number**************" << std::endl
              << std:: endl;
   
    std::cin >> num1;
    std::cin >> num2;
    gcm = 1;
   
    for (int i = num2; i > gcm ; i--)
    {
        if (num2 % i == 0 && num1 % i == 0)
        {
            gcm = i;
        }
    }
   
    for (int i = 2; i < num2; i++)
    {
        if (num2 % i == 0 && num1 % i ==0)
        {
            lcm = i;
            i = num2;
        }
    }

    std::cout << "The LCM is: " << lcm << std::endl;
    std::cout << "The GCM is: " << gcm << std::endl;
   
    return 0;
}
wtd




PostPosted: Sat Nov 12, 2005 12:55 pm   Post subject: (No subject)

I'm not going to give you working C++ code for this, but I will show you the algorithm.

code:
# let rec gcd num1 num2 =
     if num1 > num2 then
        if num1 mod num2 = 0 then
           num2
        else
           gcd (num1 mod num2) num2
     else
        gcd num2 num1;;
val gcd : int -> int -> int = <fun>
# gcd 12 8;;
- : int = 4
# gcd 2336 1314;;
- : int = 146
# let lcm num1 num2 = num1 * num2 / gcd num1 num2;;
val lcm : int -> int -> int = <fun>
# lcm 57 12;;
- : int = 228
# 57 * 12;;
- : int = 684


You may also wish to hit Google. Never know what you'll find.
Geminias




PostPosted: Sat Nov 12, 2005 1:35 pm   Post subject: (No subject)

i fixed the logic so it will return 1 as both lcm and gcm in the case that there are no other common multiples.

hey wtd, how do you get all that pretty color in your code lol
Andy




PostPosted: Sat Nov 12, 2005 1:50 pm   Post subject: (No subject)

use code tags instead of [code ] use [sytax="cpp" ]
Geminias




PostPosted: Sat Nov 12, 2005 3:16 pm   Post subject: Re: heyho

Geminias wrote:
hey again, yeah i dropped out the idea of pointers and functions for this.
it finds the lowest common multiple and the highest common multiple (LCM) (GCM) but it excludes 1. Because 1 divides into everything.
in the case that only one number besides 1 is common to both numbers, then LCM = GCM.

Viper, your recursion technique did not work. It does not find the LCM and GCM.

try this.... (note: if one of the numbers is prime the program crashes.)

syntax = c++
c++:
#include <iostream>

int main ()
{
    unsigned long int num1, num2, gcm,lcm;
    std::cout << "Enter two numbers starting with the highest one." << std::endl <<
    "*********hit enter after each number**************" << std::endl << std:: endl;
   
    std::cin >> num1;
    std::cin >> num2;
    gcm = 1;
    for (int i = num2; i >= gcm ; i--)
  {
        if (num2 % i == 0 && num1 % i == 0)
     {   gcm = i; 
     }
     else if (i = 1)
    {   
        gcm = 1
     }

  }
     for (int i = 2; i <= num2; i++)
   {
         if (num2 % i == 0 && num1 % i ==0)
         {
              lcm = i;
              i = num2;
         }
         else if (i = num2)
        {
             lcm = 1;
         }
   }

   std::cout << "The LCM is: " << lcm << std::endl;
   std::cout << "The GCM is: " << gcm << std::endl;
   std::cin.ignore(1,'\n');
   std::cin.ignore();
   
 
   return 0;
}       
   
Geminias




PostPosted: Sat Nov 12, 2005 3:17 pm   Post subject: (No subject)

thanks Laughing
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: