Euclid gcd

This is a discussion on Euclid gcd within the C++ Programming forums, part of the General Programming Boards category; Ok I got a program to return a GCD but the math is not correct I believe because it is ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    15

    Euclid gcd

    Ok I got a program to return a GCD but the math is not correct I believe because it is returning a number now but some of the GCD it returns is not the correct one...

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <process.h>
    #include <ctime>
    #include <iostream>
    #include <cstdlib>
    #include <stdlib.h>
    using namespace std;
    
    //DECLARE GLOBAL VARIABLES
    
    int firstRandomNumber = 0;
    int secondRandomNumber = 0;
    
    //FUNCTION TO TEST IF secondrandomNumberCheck EQUALS ZERO AND TO RETURN firstrandomNumberCheck AND ANC IF IT DOES
    //int abc(int firstrandomNumberCheck, int secondrandomNumberCheck)
    //{
    	//IF STATEMENT TO DETERMIN IF THE SECOND NUMBER IS 0
    	//if (secondrandomNumberCheck == 0)
        //return firstrandomNumberCheck;
    
      //return abc(secondrandomNumberCheck, firstrandomNumberCheck % secondrandomNumberCheck);
    //}
    
    //BEGIN RANDOM NUMBER TEST FUNCTION
    void randomNumberCheck()
    {
    	firstRandomNumber = 1 + rand()% 100;  
    	secondRandomNumber = 1 + rand()% 100;
    	
    
    
    	//SHOW OUTPUT
        cout << " The first random number is " << firstRandomNumber << "." << endl;
    	cout << "           The second random number is " << secondRandomNumber << "." << endl;
    
    
    
    }
    
    //change
    int gcd (int firstRandomNumber, int secondRandomNumber)
    {
    	
    	//change
    	//if (x==0)
    	if (secondRandomNumber==0)
    	{
    		//change
    		//return y;
    		return firstRandomNumber;
    	}
    	else
    	{
    		int x = (secondRandomNumber, firstRandomNumber % secondRandomNumber);
    		return x;
    		
    	}
    	//return returngcd;
    }
    
    
    //CREATE AN UNSIGNED COUNTER AND HANDLE
    unsigned counter;
    HANDLE hMutex;
    
    //UNSIGNED CALL
    unsigned __stdcall SecondThreadFunc(void* pArguments) 
    {
    	//int firstRandomNumber;
    	//int secondRandomNumber;
    	
    	//SHOW OUTPUT
    	cout << "In second thread....." << endl;
    	//printf( "In second thread..." );
    	DWORD checkWaitResultTest;
    
    	//SHOW THE COUNTER BEING LESS THAN 10
    	while (counter < 10)
    
    	{
    		//REQUEST OWNERSHIP WILL NEED TO BE PERFORMED
    		checkWaitResultTest = WaitForSingleObject(hMutex, 5000L); 
    
    		//SWITCH STATEMENT TO CHECK RESULTSW
    		switch (checkWaitResultTest)
    
    		{
    			// The thread got mutex ownership.
    			case WAIT_OBJECT_0:
    			__try
    
    			{
    				//IF STATEMENT IF THERE IS A REMAINDER
    				if (counter % 2)
    
    				{
    					counter++;
    					//SHOW OUTPUT
    					cout << "Thread #2 " ;
    					
    
    					//ENTER RANDOM NUMBER FUNCTION
    					//randomNumberCheck();
    
    					//SHOW OUTPUT OF THE THREAD TWO COUNTER
    					printf ("Thread Two Counter is %d\n", counter);
    
    					//CALL EUCLAID
    					//Changed
    					
    					printf ("Call Euclid\n");
    					//changed
    					cout << firstRandomNumber << secondRandomNumber;
    					
    				
    					int x = gcd(secondRandomNumber, firstRandomNumber);
    					//gcd(firstRandomNumber, secondRandomNumber);
    					//changed
    					
    					//cout << firstRandomNumber << secondRandomNumber;
    
    					//OUTPUT OF EUCLAID
    					//When I put in gcd(30,40)) or any other number it returns the values and it works but
    					//when I use gcd(firstRandomNumber,secondRandomNumber it keeps returning 0 so I believe
    					//the values are not passing in correctly
    					//changed
    					printf("Greatest common divisior (GCD) is %d\n", gcd(secondRandomNumber, firstRandomNumber));
    					//printf("Greatest common divisior (GCD) is %d\n", gcd(secondRandomNumber, firstRandomNumber));
    					//printf("Greatest common divisior (GCD) is ", gcd, "\n\n");
    
    				}
    			}
    
    			__finally
    
    			{
    				//RELEASE THE MUTEX
    				if (! ReleaseMutex(hMutex))
    
    				{
    					
    				}
    
    				break;
    			}
    			// TIME OUT IN THE MUTEX
    			case WAIT_TIMEOUT:
    
    			{
    				// MUTEX ABANDONED
    				return false;
    			}
    
    			case WAIT_ABANDONED:
    			{
    				return false;
    			}
    		}
    	}
    	_endthreadex( 0 );
    	return 0;
    }
    
    //MAIN FUNCTION
    int main()
    
    {
    	srand((unsigned int)time(NULL));
    	//srand (5);
    
    	//DECLARE VARIABLES
    	HANDLE hThread;
    	unsigned threadID;
    	counter = 0;
    	hMutex = CreateMutex(NULL, FALSE, (LPCWSTR) "MutexToProtectDatabase"); 
    
    	//IF STATEMENT TO CHECK IF HMUTEX IS NULL AND ERROR IF IT IS
    	if (hMutex == NULL)
    	{
    		// Check for error.
    	}
    
    	//SHOW OUTPUT
    	printf ("Start program by creating second thread\n\n");
    
    	// SECOND THREAD WILL NEED TO BE CREATED
    	hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
    	DWORD dwWaitResult;
    
    	//WHILE STATEMENT TO MAKE SURE COUNTER IS UNDER 10
    	while (counter < 10)
    	{
    		// OWNERSHIP OF MUTEX
    		dwWaitResult = WaitForSingleObject(hMutex, 5000L); 
    
    		//SWITCH STATEMENT FOR THE DWWAIT
    		switch (dwWaitResult)
    		{
    			//THREAD NOW HAS OWNERSHIP
    			case WAIT_OBJECT_0:
    			__try
    			{
    				//IF STATEMENT IF THERE IS NOT A REMAINDER
    				if (!(counter % 2))
    				{
    					//SHOW OUTPUT
    					
    					cout << "Thread #1 " ;
    					counter++;
    
    					//ENTER ABC FUNCTION
    					//abc(firstrandomNumberCheck,secondrandomNumberCheck);
    
    					
    					randomNumberCheck();
    					
    					
    
    					//SHOW OUTPUT
    					printf ( "Primary Thread Counter is %d\n", counter );
    					
    				}
    			}
    
    			__finally
    
    			{
    				//MUTEX WILL NEED OWNERSHIP 
    				if (! ReleaseMutex(hMutex))
    				{
    					// ERROR WILL OCCUR
    				}
    
    				break;
    			}
    
    			// MUTEX TIMES OUT
    			case WAIT_TIMEOUT:
    			{
    				//OWNERSHIP OF ABANDONED MUTEX
    				return false;
    			}
    
    			case WAIT_ABANDONED:
    
    			{
    				return false;
    			}
    		}
    	}
    
    	//ENTER FUNCTION
    	WaitForSingleObject( hThread, INFINITE );
    
    	//SHOW OUTPUT
    	printf( "Counter should be 10.\nCounter is currently %d\n", counter);
    
    	// DESTROY THIS THREAD
    	CloseHandle( hThread );
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    15
    disregard I figured it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Euclid Algorithm (extended)
    By nacho4d in forum C Programming
    Replies: 13
    Last Post: 12-07-2006, 01:56 AM
  2. Euclid's game.
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-31-2005, 04:13 PM
  3. Binary Euclid's Algorithm
    By exluddite in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2004, 08:08 PM
  4. Euclid Algorithm
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 10:27 PM
  5. Euclid's algorithm
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-03-2001, 10:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21