I am finishing up this program and need to find the GCD using euclids formula. I am attaching my code and was seeing if anyone could show me what is wrong. I am also sending the instructions just in case you need it...

//Programmed by: Bryan Kruep
//Date: 2/22/2010
//Assignment: Thread Assignment

#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 firstrandomNumberCheck = 0;
int secondrandomNumberCheck = 0;
int abc_output = 0;
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()
{
	int firstrandomNumberCheck = 1 + rand()% 100;  
	int secondrandomNumberCheck = 1 + rand()% 100;

	//SHOW OUTPUT
    cout << " The first random number is " << firstrandomNumberCheck << "." << endl;
	cout << "           The second random number is " << secondrandomNumberCheck << "." << endl;

	
}

int gcd (int firstRandomNumber, int secondRandomNumber)
{
	if (secondRandomNumber==0)
	{
		return firstRandomNumber;
	}
	else
	{
		gcd (secondRandomNumber, firstRandomNumber % secondRandomNumber);
	}
}


//CREATE AN UNSIGNED COUNTER AND HANDLE
unsigned counter;
HANDLE hMutex;

//UNSIGNED CALL
unsigned __stdcall SecondThreadFunc(void* pArguments) 
{
	//int firstRandomNumber;
	//int secondRandomNumber;
	
	//SHOW OUTPUT 
	printf( "In second thread...\n" );
	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
					printf ("Call Euclid\n");
					gcd(firstRandomNumber, secondRandomNumber);

					//OUTPUT OF EUCLAID
					cout << "Greatest common divisior (GCD) is " << gcd(firstRandomNumber,secondRandomNumber) << endl;

				}
			}

			__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;
}

Programming assignment two will demonstrate your understanding and mastery of multi-threaded application concepts in a Windows environment using Visual C++ .NET 2005. In this programming assignment you will create a multithreaded application that computes the greatest common divisor using Euclid’s algorithm.
Your program is to create multiple threads.
Thread one must pick two random numbers between 1 and 100, display the value of the random numbers selected (along with a thread identifier), and place them into shared global variables. Thread one then must wait for thread two to act on the values. Once thread two has completed operating on the variables, thread one must repeat, then thread two, and so on, until ten sets of random numbers have been picked, greatest common divisor found, displayed, and placed in the shared global variables.
The two random numbers picked by thread one become the two input values for Euclid’s algorithm.
Thread two must act in a cooperative manner with thread one. Thread two’s task is to find the greatest common divisor for the two random numbers selected. Once the greatest common divisor is found, it should be displayed (along with a thread identifier). Thread two should then wait for another set of values to be placed in the shared global variables. Thread two should act upon each set of random numbers selected by thread one.
Euclid’s Algorithm
Definition: An algorithm to compute the greatest common divisor of two positive integers.
It is Euclid(a,b){if (b=0) then return a; else return Euclid(b, a mod b);}.
The run time complexity is O((log a)(log b)) bit operations.
Source: National Institute of Standards and Technology (http://www.nist.gov/dads/HTML/euclidalgo.html)
Your program must use two threads
CSI 345 – February 2009 -- Programming Assignment Two
You may choose managed or unmanaged code for this programming assignment. Your program may use any of the available libraries (such as the ATL or the STL).
The random number generator should be seeded in some manner.
During program development and testing, using a static number to seed the random number generator will result in the same numbers being picked each time the program is run.
For production, seeding with the current time is preferred. Using the current time insures that different numbers will be generated each time the program is run.
Your program must use some method of synchronization for the cooperating threads. You may use any synchronization object (semaphore, mutex, ATL object, etc) that is available in Visual C++ .NET. Your program must not use busy-waiting.
Your program’s threads must not access the shared global variables in any manner unless they do so in a way that assures that the other thread has been blocked from accessing the variables and that any operation can occur in a mutually exclusive manner. Failure to access the shared global variables in a mutually exclusive manner will be considered a major design flaw.

Line 52: you do not return anything from the else clause.
Didn't the compiler warn you?

this is what I put for a return but I am guessing this is wrong...

int gcd (int firstRandomNumber, int secondRandomNumber)
{
	if (secondRandomNumber==0)
	{
		return firstRandomNumber;
	}
	else
	{
		gcd (secondRandomNumber, firstRandomNumber % secondRandomNumber);
		return secondRandomNumber;
	}
}

simple gcd

int g_c_d(int a,int b)
{
    if(b==0){return a;}
    else{return g_c_d(b,a%b);}
}

I apologize for responding again but I put the following in and it still returns 0...

int gcd (int firstRandomNumber, int secondRandomNumber)
{
	if (secondRandomNumber==0)
	{
		return firstRandomNumber;
	}
	else
	{
		return gcd (secondRandomNumber, firstRandomNumber % secondRandomNumber);
		
	}
}

I don't believe you:

nezachem@ ~/projects/dany $ cat gcd.c
#include <stdio.h>

int gcd (int firstRandomNumber, int secondRandomNumber)
{
    if (secondRandomNumber==0)
    {
        return firstRandomNumber;
    }
    else
    {
        return gcd (secondRandomNumber, firstRandomNumber % secondRandomNumber);
        
    }
}

int main()
{
    printf("%d\n", gcd(6, 9));
    return 0;
}
nezachem@ ~/projects/dany $ make gcd
cc     gcd.c   -o gcd
nezachem@ ~/projects/dany $ ./gcd 
3

When I put those numbers in I do get 3 and 6 to return as the random numbers but I need to pass the first and second random number so since it works when I enter the following...

printf("Greatest common divisior (GCD) is %d\n", gcd(3,6))

but does not work when I enter...

printf("Greatest common divisior (GCD) is %d\n", gcd(secondRandomNumber, firstRandomNumber))

The random numbers are not passing but not sure why

So now we agree that gdc works fine, and a problem is somewhere else.Correct me if I am wrong, but there's nothing in your code which would assign any value to firstRandomNumber (or secondRandomNumber). I am talking about the shared global variables, declared at lines 18 and 19.

PS: didn't your compiler warn you that a formal parameter shadows a global declaration?

Yes also when I change the global variables at the top from 0 to say like 10 the gcd changes as well to whatever is put in if that makes sense

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.