Click here to Skip to main content
15,901,283 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to get the image from a clistctrl? Pin
shibble30-Oct-04 23:16
shibble30-Oct-04 23:16 
GeneralRe: How to get the image from a clistctrl? Pin
Ravi Bhavnani31-Oct-04 6:02
professionalRavi Bhavnani31-Oct-04 6:02 
GeneralRe: How to get the image from a clistctrl? Pin
shibble31-Oct-04 7:02
shibble31-Oct-04 7:02 
GeneralRe: How to get the image from a clistctrl? Pin
Ravi Bhavnani31-Oct-04 7:06
professionalRavi Bhavnani31-Oct-04 7:06 
GeneralRe: How to get the image from a clistctrl? Pin
shibble31-Oct-04 7:12
shibble31-Oct-04 7:12 
Questionhow to Pass arguments to main and returning values from a program... Pin
Kiran Satish30-Oct-04 11:08
Kiran Satish30-Oct-04 11:08 
AnswerRe: how to Pass arguments to main and returning values from a program... Pin
Ravi Bhavnani30-Oct-04 15:41
professionalRavi Bhavnani30-Oct-04 15:41 
GeneralDoing a program compares bubble sort and multithread sort... Pin
30-Oct-04 10:45
suss30-Oct-04 10:45 
Anyway, my teacher wants us to have an array size of 1,000,000(1 million) and have a segment size of 10000. However, if i put my segment size at 300000 or greater, i get an error saying that the program has encountered an error and needs to close. I can do 100000 and 200000 but anything greater and I get that error.
The programs works fine on 100000 and 200000 it sorts the thread correctly in bubble and multithread sort.

EDIT: For some reason i cant put the code in php.

Here is the code:

#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const int ArraySize=300000;
const int SegmentSize=10000;
const int SleepInterval=10;


DWORD WINAPI SortFun(LPVOID);
void InitializeArray(void);
void Merge(void);
int FindMinKeys(int keys[]);
void DisplayArray(int begin, int end);
void SORT(void);

static int RunFlag = ArraySize/SegmentSize;
int Array[ArraySize];

clock_t start, finish;
double duration;

LPSECURITY_ATTRIBUTES lpthreadattributes = NULL;
DWORD stacksize = 0;
DWORD dwcreationflags;
DWORD ThreadId[ArraySize/SegmentSize];
HANDLE TargetThread[ArraySize/SegmentSize];

void main(void)
{
InitializeArray();

start = clock();
// SORT();
finish = clock();
printf("\n Array Size =%d \n Segment Size =%d \nSleep Interval =%d\n",
ArraySize, SegmentSize, SleepInterval);
printf("\n ***** elapsed time for Single_threaded Bubble sort ****");

duration = (double)(finish - start) / CLOCKS_PER_SEC; // elapsed time in clocks/second
printf("\n clock ticks = %10.3lf\n",(double)(finish-start)); // elapsed time, clock ticks
printf("\n seconds = %10.3lf\n", duration );

// char ch=getchar();
// DisplayArray(0,ArraySize-1);
// ch = getchar();

// Create threads to sort every 1000 th segment of the array
int j=0;
start = clock();
for (int i=0; i<ArraySize; i=i+SegmentSize)
{

TargetThread[j] =
CreateThread(
NULL,
0,
SortFun,
&i,
0,
&ThreadId[j]
);
// printf(" j= %d th thread created\n",j);
//char ch = getchar();

// give time to the newly created thread to work

Sleep(SleepInterval);

// create the next thread

j++;
}
// wait for all threads to complete their work

while (RunFlag > 0)
Sleep(SleepInterval);

// Merge the sorted arrays

Merge();
finish = clock();

printf(" The Entire Array Sorted\n");
printf("\n ***** elapsed time for Multi_threaded Bubble sort ****");
printf("\n ***** Segment Size ***** %d\n",SegmentSize);
duration = (double)(finish - start) / CLOCKS_PER_SEC; // elapsed time in clocks/second
printf("\n clock ticks = %10.3lf\n",(double)(finish-start)); // elapsed time, clock ticks
printf("\n seconds = %10.3lf\n", duration );

// ch=getchar();
// DisplayArray (0,ArraySize-1);

// ch = getchar();
}
void InitializeArray(void)
{
for (int i=0; i<ArraySize; i++)
Array[i] = (rand()*171+rand())%1000000U;
}
void SORT(void)
{
int exchange;
int low = 0;
int high = ArraySize;


// printf(" low = %d high = %d\n",low,high);
for (int k = low; k<high-1; k++)
{
exchange = 0;
for (int l = low; l<= low +high-(k+1); l++)
if (Array[l] > Array[l+1])
{
int t = Array[l];
Array[l] = Array[l+1];
Array[l+1] = t;
exchange = 1;
}
if (exchange == 0) break;

}
}
DWORD WINAPI SortFun(LPVOID N)
{

int exchange;
int low = *(int*)N;
int high = low + SegmentSize-1;

// sort that portion of the array
// printf(" low = %d high = %d\n",low,high);
for (int k = low; k<high-1; k++)
{
exchange = 0;
for (int l = low; l<= low +high-(k+1); l++)
if (Array[l] > Array[l+1])
{
int t = Array[l];
Array[l] = Array[l+1];
Array[l+1] = t;
exchange = 1;
}
if (exchange == 0) break;

}
RunFlag--;

// printf(" ****** Array Segment Sorted ******\n");
//DisplayArray(low,low+SegmentSize-1);

// printf(" i= %d th tread ended\n",low/SegmentSize);
// char ch = getchar();
CloseHandle((void *)&TargetThread[low/1000]);
return 0;
}
void Merge(void)
{
int t[ArraySize];
int p[ArraySize/SegmentSize];
int keys[ArraySize/SegmentSize];

// set the pointers to point to the begining of the sorted segments

for (int i=0; i<ArraySize/SegmentSize; i++)
{
p[i] = i*SegmentSize;
keys[i] = Array[p[i]];
}

// Merge the Array portions

int tposition = 0;;
int Done = 0;
int MinPosition;
while (! Done)
{
MinPosition = FindMinKeys(keys);

if (MinPosition != -1)
{
t[tposition] = Array[p[MinPosition]];
tposition++;
p[MinPosition]++;

if (p[MinPosition] % SegmentSize == 0)
{
// reached the end of one segment

keys[MinPosition] = 1000000U;
}
else
keys[MinPosition] = Array[p[MinPosition]];
}
else
Done =1;

}
for (int j=0; j<ArraySize; j++)
Array[j] = t[j];

}
int FindMinKeys(int keys[])
{

int position = 0;
for (int i = 1; i<ArraySize/SegmentSize; i++)
{
if (keys[i] < keys[position])
position = i;
}
if (keys[position] == 1000000U) return -1;
return position;
}
void DisplayArray(int begin, int end)
{
for (int j=begin; j<=end; j++)
{
printf("%6d Array[i] = %6d \n",j,Array[j]);
}
}
GeneralYABQ (Yet Another Beginner Question) Pin
BeerFizz30-Oct-04 8:47
BeerFizz30-Oct-04 8:47 
Questionevent programming? Pin
Setesh8230-Oct-04 7:15
Setesh8230-Oct-04 7:15 
General2 MFC Questions from a newbie. Help please. Pin
Member 148206230-Oct-04 5:01
Member 148206230-Oct-04 5:01 
GeneralRe: 2 MFC Questions from a newbie. Help please. Pin
Member 148206230-Oct-04 5:04
Member 148206230-Oct-04 5:04 
GeneralRe: 2 MFC Questions from a newbie. Help please. Pin
Natural_Demon31-Oct-04 5:02
Natural_Demon31-Oct-04 5:02 
GeneralRe: 2 MFC Questions from a newbie. Help please. Pin
31-Oct-04 10:50
suss31-Oct-04 10:50 
GeneralRe: 2 MFC Questions from a newbie. Help please. Pin
Natural_Demon31-Oct-04 10:59
Natural_Demon31-Oct-04 10:59 
GeneralEdge Linking Algorithm Pin
gilazilla30-Oct-04 5:01
gilazilla30-Oct-04 5:01 
Generaldebugging dll's Pin
ddskid30-Oct-04 3:53
ddskid30-Oct-04 3:53 
GeneralRe: debugging dll's Pin
Member 148206231-Oct-04 11:31
Member 148206231-Oct-04 11:31 
QuestionMemory Leak...Any Help ? Pin
Amarelia30-Oct-04 1:10
Amarelia30-Oct-04 1:10 
AnswerRe: Memory Leak...Any Help ? Pin
BeerFizz30-Oct-04 5:48
BeerFizz30-Oct-04 5:48 
GeneralRe: Memory Leak...Any Help ? Pin
Amarelia31-Oct-04 17:47
Amarelia31-Oct-04 17:47 
GeneralRe: Memory Leak...Any Help ? Pin
Roger Allen1-Nov-04 3:13
Roger Allen1-Nov-04 3:13 
QuestionHow to get licence key for IBM's Rational Purify Evaluation ? Pin
Anonymous29-Oct-04 23:45
Anonymous29-Oct-04 23:45 
AnswerRe: How to get licence key for IBM's Rational Purify Evaluation ? Pin
Sujan Christo29-Oct-04 23:51
Sujan Christo29-Oct-04 23:51 
GeneralDeutsches Diskussion-Board gesucht! Pin
gerspeece29-Oct-04 23:43
gerspeece29-Oct-04 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.