Implement parallel algorithm with VC thread simulation

zhaozj2021-02-17  80

The topic is as follows: Let N = 2 M times, A is an array of 2N dimensions, and the number of to be maximum is placed in A (N), A (N 1), ... a (2n-1), The maximum value is placed in a (1), so that the algorithm is described as follows: N = 2 M (N; 2N-1) is present in array a (N; 2N-1); output: Maximum number is placed in A (1) .

Beginfor K = M-1 TO 0 do for j = 2k 1-1 to 2k par do a (j) = max (2j), A (2J 1)) End Forend Forend

Implementation:

Defining the global variables as follows, Array is used to store the random number generated.

INT G_NCOUNT = 0, J = 0; int * array;

Randomly generate N numbers and stored in array array, the program code is as follows:

Void cdemodlg :: Onbtninitial () {// Initialization Array SRAND (NULL); INT I, TEMP; CSTRING STR; Updatedata (TRUE); g_ncount = POW (2, m_intcount); array = new int = 2 * g_ncount]; // According to the user's input, generating 2 M-Party random number is also stored in Array [n] ... A [2N-1] in FOR (i = g_ncount; i <2 * g_ncount; i ) { Temp = rand () / 100; array [i] = Temp;} // Display generated N random number for (i = g_ncount; i <2 * g_ncount; i ) {str.format ("array [% d] =% D ", I, Array [i]); m_strarray =" / r / n "; m_strarray = Str; m_strarray =" / r / n ";} m_strarray =" / r / n "; Updatedata (false); }

Void cdemodlg :: OnbtNCompute () {// Starting Thread for calculation INT K; for (k = m_intcount-1; k> = 0; k -) {for (j = pow (2, k); J = array [2 * j 1]) {array [j] = array [2 * j];} else {array [j] = Array [2 * J 1];} Return 0;}} // Display calculation result Void cdemodlg :: Onbtnshow () {// Todo: Add Your Control Notification Handler Code Here Int K; CString Str; for (K = 1 ; k <2 * g_ncount; k ) {str.format ("Array [% D] =% D", K, Array [k]); m_strarray = "/ r; m_strarray = Str; M_STRARRAY =" / R / n ";} Updatedata (false);} Reset function implementation code, mainly emptying array: void cdemodlg :: OnbTnclear () {// Todo: add your control notification handler code her_strarray.empty (); m_ctrlcount.setfocus (); m_ctrlcount.clear (); m_ctrolcount.setsel (1); Updatedata (false); g_ncount = 0; J = 0;

}

转载请注明原文地址:https://www.9cbs.com/read-31103.html

New Post(0)