Thread: Greatest Common Factor

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Greatest Common Factor

    This is my probem: my code should work. in fact, for small numbers it does. (like 2 and 2). but it isnt working, and it gives me one of those "microsoft error" things. please help me.

    Code:
    #include <iostream>
    #include <math.h>
    #include <string.h>
    using namespace std;
    int main(){
        int a, b, c, x, y, z;
        int big [100];
        int small [100];
    z=1;
                     cout<<"what are the two numbers?\n";
                     cin>>a>>b;
                     if(a>b){
                     x=a;
                     }
                     if(a<b){
                     x=b;
                     }
                     for(; x<100;x--){
                     if((a%x==0)&&(b%x==0)&&(x>z)){
                     z=x;
                     }
                     }
                     cout<<z;
                     
                     
                     cin.get();
                     cin.get();
                     }
    Last edited by cppdungeon; 11-27-2005 at 03:40 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Consider the domain of integers you need to test in order to obtain the GCF between two numbers... determine the smaller of the two user entered numbers, and test all positive integers until you have tested the entire range of posibilities, from '2' to the lesser of the two user entries.

    In your loop condition, you arbitrarily test the two numbers for a GCF using a range of integers from the largest user entered integer to negitive infinity (if 'x' is less than or equal to 100) and from x to 100 (if 'x' is greater than 100)... but what if the two integers were like.. 10 and 15 for example? in which case, all you need to do is test a range of integers from 2 to 10 inclusive.

    Code:
    cin >> a;   // a == 10 in this example
    
    cin >> b;   // b == 15 in this example
    which brings us to this:
    Code:
    if(a<b){
                     x=b;    // x == 15      I think the smaller of the two number should set the loop condition..  not the largest

    and finally, we are brought to the main algorithm.. a loop that should find a GCF if one exists..

    Here is your code:
    Code:
    for(; x<100;x--){  
                     if((a%x==0)&&(b%x==0)&&(x>z)){
                     z=x;
                     }

    I think this would be better logic for your loop condition:
    Code:
    z=0;
    for(; x>1;x--){  //where 'x' == the lesser of two user entered integers
                     if((a%x==0)&&(b%x==0)&&(x>z)){
                     z=x;

    tips: Look for ways to break out of the loop as soon as the GCF is detected. Also, make sure your program can handle a situation where the two user entered integers are the same.. and cases where no GCF exists.
    Last edited by The Brain; 11-27-2005 at 08:56 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Man, there's some ugly indenting going on in this thread...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    Thanks!!! i got it wo work finaly. here is the completed code:

    Code:
    #include <iostream>
    #include <math.h>
    #include <string.h>
    using namespace std;
    int main(){
        int a, b, c, x, y, z;
        int big [100];
        int small [100];
    z=1;
                     cout<<"what are the two numbers?\n";
                     cin>>a>>b;
                     if(a>b){
                     x=b;
                             }
                     if(b>a){
                     x=a;     
                             }
                     if(a==b){
                     z=a;
                     }
                     if(a!=b){
                     for(  ;x>0   ;x--  ){
                     if(a%x==0&&b%x==0&&x>z){
                     z=x;
                     }
                     }        
                     } 
                     
                     cout<<z;
                     
                     
                     cin.get();
                     cin.get();
                     }

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    As the formatting remark was reported as "rude, random and unnessecary" I'll post the "nice, targetted and neccessary" version: This code looks like someone randomly sprinkled breaks and new lines over the code. For good, easily understandable and maintainable code, it IS a neccessity to properly format it. The sooner you learn to format in a readable manner, the sooner will you understand your and other peoples programs.

    Just to give an example of code that was formatted and cleaned:

    PHP Code:
    #include <iostream>

    using namespace std;

    int main()
    {
        
    int abxz;

        
    1;

        
    cout << "what are the two numbers?" << endl;
        
    cin >> >> b;

        if( 
    )
        {
            
    b;
        }

        if( 
    )
        {
            
    a;     
        }
        
        if( 
    == )
        {
            
    a;
        }
        
        if( 
    != )
        {
            for(  ; 
    x-- )
            {
                if( ( 
    == ) && ( == ) && ( ) )
                {
                    
    x;
                }
            }        
        } 
         
        
    cout << z;
         
        
    cin.get();
        
    cin.get();

        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I see the OP hasn't improved much either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Lowest Common Factor
    By PJYelton in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2002, 09:30 AM
  3. reducing a fraction/greatest common factor
    By dbaryl in forum C Programming
    Replies: 2
    Last Post: 07-21-2002, 03:05 PM
  4. Greatest Common Factor
    By NavyBlue in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 02:47 PM
  5. Greatest Common Factor problem
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 03:29 PM