I have a program due tonight and the GCD keeps returning a 0 but when I change the following to like 2 or 3 instead of 0 it will return a 2 or 3 for gcd so something is not passing right. Can someone please show me what I need to do or show me what line is the issue. This is kind of imp. as my graduation depends on this and could appreciate any help.

#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 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;


}

//change
int gcd (int firstrandomNumberCheck, int secondrandomNumberCheck)
{
	//change
	if (secondRandomNumber==0)
	{
		//change
		return firstRandomNumber;
	}
	else
	{
		return 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
	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
					gcd(firstRandomNumber, secondRandomNumber);
					//changed
					

					//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 ", 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;
}
Member Avatar for iamthwee

Ah the pitfalls of debugging a multi-threaded program.

yea i cant really debug this

Ok I got the 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...

#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;
}
Member Avatar for Mouche

You're getting zero for gcd() because you never assign anything to firstRandomNumber or secondRandomNumber except when you first initialize them to 0.

In the second post I am getting a number returned just not the correct one

Member Avatar for Mouche

For one, line 56 is missing "gcd".

I changed the name because gcd is the name of the function

Member Avatar for Mouche

I was saying you have this:

int x = (secondRandomNumber, firstRandomNumber % secondRandomNumber);

instead of this:

int x = gcd(secondRandomNumber, firstRandomNumber % secondRandomNumber);

o im sorry about that

looks like that did it...that was simple thanks....if u know why its counting 11 counters and its simple let me know but thas not a big deal I have pretty much the program done

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.