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