Errors Across Multiple .cpp/ & .h files.

Apr 25, 2014 at 1:46pm
How can I fix my code based on these errors popping up when trying to run the program?

Thank you.

1> Sorts.cpp
1> Source.obj : error LNK2005: "int * defaultArray" (?defaultArray@@3PAHA) already defined in Sorts.obj
1> Source.obj : error LNK2005: "int randomNum" (?randomNum@@3HA) already defined in Sorts.obj
1> .exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

myHeader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef MYHEADER_H
#define MYHEADER_H

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int smallSize = 10, mediumSize = 1000, largeSize = 10000;
int defaultArray[10000], randomNum;

void bubbleSort (int[], int); 
void selectionSort (int[], int);
void insertionSort (int[], int);

#endif 


Sorts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "myHeader.h"

void bubbleSort (int defaultArray[], int size)  // Bubble Sort
{
	bool swaps = true;
	while (swaps)			
	{
		swaps = false;	// Initialize
		for (int i = 0; i < size - 1; i++) 
		{
			if (defaultArray[i] > defaultArray[i+1]) 
			{
				int tempValue = defaultArray[i];
				defaultArray[i] = defaultArray[i+1];
				defaultArray[i+1] = tempValue;
				swaps = true;
			}
		}
	}
}

void selectionSort(int defaultArray[], int size)	// Selection Sort
{
	for (int i = 0; i < size - 1; i++)
	{
		int indexMin = i;
		for (int j = i+1 ; j < size; j++)
		{
			if (defaultArray[j] < defaultArray[indexMin])
			indexMin = j;
		}
		int tempValue = defaultArray[indexMin];
		defaultArray[indexMin] = defaultArray[i];
		defaultArray[i] = tempValue;
	}
}

void insertionSort(int defaultArray[], int size)	// Insertion Sort
{
	int j;
	for (int i = 1; i < size; j++)
	{
		for (j = i; j > 0 && (defaultArray[j] < defaultArray[j-1]); j--)
		{ 
			int tempValue = defaultArray[j];
			defaultArray[j] = defaultArray[j-1];
			defaultArray[j-1] = tempValue;
		}
	}
}


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "myHeader.h"

void headerStars(), headerSpaces(), programTitle(), addRandomNumbers();

int main()
{
	headerSpaces();
	programTitle();
	headerStars();

	srand(NULL);		// Random Number Seed
	clock_t start, end;
	long timeElapsed;
	addRandomNumbers();
	
	/* Bubble Sort */
	// Small
	start = clock(); 
	bubbleSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock(); 
	bubbleSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock(); 
	bubbleSort(defaultArray, largeSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl << endl;

	/* Selection Sort */
	// Small
	start = clock();
	selectionSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock();
	selectionSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock();
	selectionSort(defaultArray, largeSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl << endl;
			
	/* Insertion Sort */
	// Small
	start = clock();
	insertionSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock();
	insertionSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock();
	insertionSort(defaultArray, largeSize);
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;
	
	cin.get();
	cin.ignore();
	return 0;

}	// End of int main()

void headerStars()
{
	for (int headerStars = 0; headerStars < 40; headerStars++)
	cout << "*";
}
void headerSpaces()
{
	for (int headerSpaces = 0; headerSpaces < 9; headerSpaces++)
	cout << " ";
}
void programTitle()
{
	cout << "Sorting - Time Analysis" << endl;
}
void addRandomNumbers()
{
	for (randomNum = 0; randomNum < largeSize; randomNum++)
	defaultArray[randomNum] = rand();
}
Apr 25, 2014 at 1:53pm
what's in your Sorts.h?
Have you defined 'defaultArray' in there as well?
Last edited on Apr 25, 2014 at 1:55pm
Apr 25, 2014 at 2:00pm
myheader.h line 11: Do not define variables in a header file. The header file gets included in multiple.cpp files and therefore your variables are defined multiple times resulting in linker errors.

Your variables should be defined as follows in your header file:
1
2
 
extern int defaultArray[], randomNum;


Then in one and only one .cpp file:
 
int defaultArray[10000], randomNum;




Last edited on Apr 25, 2014 at 2:01pm
Apr 25, 2014 at 2:03pm
@Abstraction
I fixed my Source.cpp but it is saying function idenitifiers not found

This code should use the three sorting methods and output elapsed time (in seconds) based on three different array sizes (small, medium & large) for each.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "myHeader.h"

void headerStars()
{
	for (int headerStars = 0; headerStars < 40; headerStars++)
	cout << "*";
}
void headerSpaces()
{
	for (int headerSpaces = 0; headerSpaces < 9; headerSpaces++)
	cout << " ";
}
void programTitle()
{
	cout << "Sorting - Time Analysis" << endl;
}
void addRandomNumbers(int defaultArray[], int smallSize, int mediumSize, int LargeSize)
{
	for (randomNum = 0; randomNum < largeSize; randomNum++)
	defaultArray[randomNum] = rand();
}

int main()
{
	int defaultArray[10000], randomNum;
	headerSpaces();
	programTitle();
	headerStars();

	srand(NULL);		// Random Number Seed
	clock_t start, end;
	long timeElapsed;
	addRandomNumbers(defaultArray, smallSize, mediumSize, largeSize);
	
	/* Bubble Sort */
	// Small
	start = clock(); 
	bubbleSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock(); 
	bubbleSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock(); 
	bubbleSort(defaultArray, largeSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl << endl;

	/* Selection Sort */
	// Small
	start = clock();
	selectionSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock();
	selectionSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock();
	selectionSort(defaultArray, largeSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl << endl;
			
	/* Insertion Sort */
	// Small
	start = clock();
	insertionSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock();
	insertionSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock();
	insertionSort(defaultArray, largeSize);
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;
	
	cin.get();
	cin.ignore();
	return 0;

}	// End of int main() 
Last edited on Apr 25, 2014 at 2:14pm
Apr 25, 2014 at 2:14pm
I showed you how to declare your variables in your myheader.h file.

Did you make the changes to add the extern keyword?

extern tells the compiler and linker what the variables look like, but that they are defined elsewhere.
Apr 25, 2014 at 2:17pm
Here is what I have so far: I just got the code to compile, but it is saying 0 seconds for Bubble & Selection (small medium and large), and Insertion sort is missing all-together.

myHeader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define MYHEADER_H

#include <iostream>
#include <stdlib.h>	// srand(NULL);
#include <time.h>	// clock_t

using namespace std;

// Constants
const int smallSize = 10, mediumSize = 1000, largeSize = 10000;
extern int defaultArray[10000];

// Functions
void bubbleSort (int[], int); 
void selectionSort (int[], int);
void insertionSort (int[], int);

#endif 


Sorts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "myHeader.h"

void bubbleSort (int defaultArray[], int size)  // Bubble Sort
{
	bool swaps = true;
	while (swaps)			
	{
		swaps = false;	// Initialize
		for (int i = 0; i < size - 1; i++) 
		{
			if (defaultArray[i] > defaultArray[i+1]) 
			{
				int tempValue = defaultArray[i];
				defaultArray[i] = defaultArray[i+1];
				defaultArray[i+1] = tempValue;
				swaps = true;
			}
		}
	}
}

void selectionSort(int defaultArray[], int size)	// Selection Sort
{
	for (int i = 0; i < size - 1; i++)
	{
		int indexMin = i;
		for (int j = i+1 ; j < size; j++)
		{
			if (defaultArray[j] < defaultArray[indexMin])
			indexMin = j;
		}
		int tempValue = defaultArray[indexMin];
		defaultArray[indexMin] = defaultArray[i];
		defaultArray[i] = tempValue;
	}
}

void insertionSort(int defaultArray[], int size)	// Insertion Sort
{
	int j;
	for (int i = 1; i < size; j++)
	{
		for (j = i; j > 0 && (defaultArray[j] < defaultArray[j-1]); j--)
		{ 
			int tempValue = defaultArray[j];
			defaultArray[j] = defaultArray[j-1];
			defaultArray[j-1] = tempValue;
		}
	}
}


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include "myHeader.h"

void headerStars()
{
	for (int headerStars = 0; headerStars < 40; headerStars++)
	cout << "*";
}
void headerSpaces()
{
	for (int headerSpaces = 0; headerSpaces < 9; headerSpaces++)
	cout << " ";
}
void programTitle()
{
	cout << "Sorting - Time Analysis" << endl;
}
void addRandomNumbers(int defaultArray[], int smallSize, int mediumSize, int largeSize)
{
	for (int randomNum = 0; randomNum < smallSize, randomNum < mediumSize, randomNum < largeSize; randomNum++)
	defaultArray[randomNum] = rand();
}

int main()
{
	int defaultArray[10000];
	headerSpaces();
	programTitle();
	headerStars();


	srand(NULL);		// Random Number Seed
	clock_t start, end;
	long timeElapsed;
	addRandomNumbers(defaultArray, smallSize, mediumSize, largeSize);
	
	/* Bubble Sort */
	// Small
	start = clock(); 
	bubbleSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock(); 
	bubbleSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock(); 
	bubbleSort(defaultArray, largeSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC; 
	cout << "Bubble Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl << endl;

	/* Selection Sort */
	// Small
	start = clock();
	selectionSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock();
	selectionSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock();
	selectionSort(defaultArray, largeSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Selection Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl << endl;
			
	/* Insertion Sort */
	// Small
	start = clock();
	insertionSort(defaultArray, smallSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Small, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Medium
	start = clock();
	insertionSort(defaultArray, mediumSize); 
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Medium, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;

	// Large
	start = clock();
	insertionSort(defaultArray, largeSize);
	end = clock();
	timeElapsed = (end - start)/CLOCKS_PER_SEC;
	cout << "Insertion Large, Time Elapsed (in seconds): " << timeElapsed;
	cout << endl;
	
	cin.get();
	cin.ignore();
	return 0;

}	// End of int main()
Last edited on Apr 25, 2014 at 2:35pm
Apr 25, 2014 at 2:49pm
You're declaring defaultArray with two different scopes. In myheader.h line 11 the declaration is extern, which makes it global. In source.cpp line 26, defaultArray is local to main.

You don't need the extern declaration since you're always passing defaultArray in your function calls. You should remove myheader.h line 11.

source.cpp line 20: What exactly do you think your termination condition is doing? Do you understand what the , operator does?

General comment: Your timings are going to be skewed since you populate the array only once. After the first small sort, default array will be partially sorted. After the first large sort, defaultArray will be fully sorted.
This affects subsequent timings since no reordering needs to be done.

Last edited on Apr 25, 2014 at 2:52pm
Apr 25, 2014 at 3:08pm
For Source.cpp Line 20, would I just add && instead of the commas?
Also, how do I make it so the arrays are not already partially filled?
Last edited on Apr 25, 2014 at 3:32pm
Apr 25, 2014 at 3:41pm
I do not understand in regards to source.cpp line 20

Why are you passing all three sizes to addRandomNumbers?

randomNum < smallSize, randomNum < mediumSize, serve no purpose in your loop's termination condition. Only randomNum < largeSize is considered.

It would make more sense to code addRandomNumbers as follows:
1
2
3
4
5
// Populate specified number of random numbers into the array
void addRandomNumbers(int defaultArray[], int size)
{  for (int randomNum = 0; randomNum < size; randomNum++)
        defaultArray[randomNum] = rand();
}


how do I make it so the arrays are not already partially filled?

Repopulate the array before each sort.

You can make this program a lot shorter using function pointers.
1
2
3
4
5
6
7
8
9
10
11
12
//  Handle any sort specified by function pointer
void do_sort (string sort_name, void (sort_func *)(int *,int),int * array, int size)
{    clock_t start, end;
    long timeElapsed;

    addRandomNumbers (array, size); 
    start = clock(); 
    (*sort_func) (array, size);  
    end = clock();
    timeElapsed = (end - start)/CLOCKS_PER_SEC; 
    cout << sort_name << " Elapsed (in seconds): " << timeElapsed << endl;
}


Then in main:
1
2
3
4
5
6
7
8
9
    do_sort ("small bubble", bubble_sort, array, small_size);
    do_sort ("medium bubble", bubble_sort, array, medium_size);
    do_sort ("large bubble", bubble_sort, array, large_size);
    do_sort ("small selection", selection_sort, array, small_size);
    do_sort ("medium selection", selection_sort, array, medium_size);
    do_sort ("large selection", selection_sort, array, large_size);
    do_sort ("small insertion", insertion_sort, array, small_size);
    do_sort ("medium insertion", insertion_sort, array, medium_size);
    do_sort ("large insertion", insertion_sort, array, large_size);



Apr 25, 2014 at 3:56pm
@Abstraction

Why is the cout here not recognizing?
Also, the compiler did not recognize (sort_func *)

1>error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)


1
2
3
4
5
6
7
8
9
10
11
12
void doSort (string sort_name, void (sort_func)(int *, int), int* defaultArray, int size)
{
	clock_t start, end;
	long timeElapsed;

	addRandomNumbers (defaultArray, size);
	start = clock();
	(*sort_func)(defaultArray,size);
	end = clock();
	timeElapsed = (end-start)/CLOCKS_PER_SEC;
	cout << sort_name << "Elapsed time (in seconds): " << timeElapsed << endl;
}
Last edited on Apr 25, 2014 at 3:58pm
Apr 25, 2014 at 4:14pm
Also, the compiler did not recognize (sort_func *)

Sorry. I had the * in the wrong place.
 
void do_sort (string sort_name, void (*sort_func)(int *,int),int * array, int size)


error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'

You need to add:
 
#include <string>  

Apr 25, 2014 at 4:18pm
@Abstraction Ok, so it is compiling again. However, it still says 0 seconds for small medium large, and insertion sort is missing. This is strange.

small bubble: 0
medium bubble: 0
large bubble: 0

small selection: 0
medium selection: 0
large selection: 0

myHeader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MYHEADER_H
#define MYHEADER_H

#include <iostream>
#include <string>
#include <stdlib.h>	// srand(NULL);
#include <time.h>	// clock_t

using namespace std;

// Constants
const int smallSize = 10, mediumSize = 1000, largeSize = 10000;

// Functions
void bubbleSort (int[], int); 
void selectionSort (int[], int);
void insertionSort (int[], int);

#endif 


Sorts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "myHeader.h"

void bubbleSort (int defaultArray[], int size)  // Bubble Sort
{
	bool swaps = true;
	while (swaps)			
	{
		swaps = false;	// Initialize
		for (int i = 0; i < size - 1; i++) 
		{
			if (defaultArray[i] > defaultArray[i+1]) 
			{
				int tempValue = defaultArray[i];
				defaultArray[i] = defaultArray[i+1];
				defaultArray[i+1] = tempValue;
				swaps = true;
			}
		}
	}
}

void selectionSort(int defaultArray[], int size)	// Selection Sort
{
	for (int i = 0; i < size - 1; i++)
	{
		int indexMin = i;
		for (int j = i+1 ; j < size; j++)
		{
			if (defaultArray[j] < defaultArray[indexMin])
			indexMin = j;
		}
		int tempValue = defaultArray[indexMin];
		defaultArray[indexMin] = defaultArray[i];
		defaultArray[i] = tempValue;
	}
}

void insertionSort(int defaultArray[], int size)	// Insertion Sort
{
	int j;
	for (int i = 1; i < size; j++)
	{
		for (j = i; j > 0 && (defaultArray[j] < defaultArray[j-1]); j--)
		{ 
			int tempValue = defaultArray[j];
			defaultArray[j] = defaultArray[j-1];
			defaultArray[j-1] = tempValue;
		}
	}
}


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "myHeader.h"

void headerStars()
{
	for (int headerStars = 0; headerStars < 40; headerStars++)
	cout << "*";
}
void headerSpaces()
{
	for (int headerSpaces = 0; headerSpaces < 9; headerSpaces++)
	cout << " ";
}
void programTitle()
{
	cout << "Sorting - Time Analysis" << endl;
}
void addRandomNumbers(int defaultArray[], int size)
{
	for (int randomNum = 0; randomNum < size; randomNum++)
	defaultArray[randomNum] = rand();
}
void doSort (string sortName, void (*sort_func)(int *, int), int* defaultArray, int size)
{
	clock_t start, end;
	long timeElapsed;

	addRandomNumbers (defaultArray, size);
	start = clock();
	(*sort_func)(defaultArray,size);
	end = clock();
	timeElapsed = (end-start)/CLOCKS_PER_SEC;
	cout << sortName << "Elapsed time (in seconds): " << timeElapsed << endl;
}

int main()
{
	int defaultArray[10000];
	headerSpaces();
	programTitle();
	headerStars();

	cout << endl;

	srand(NULL);		// Random Number Seed

	doSort ("Small Bubble", bubbleSort, defaultArray, smallSize);
    doSort ("Medium Bubble", bubbleSort, defaultArray, mediumSize);
    doSort ("Large Bubble", bubbleSort, defaultArray, largeSize);
    doSort ("Small Selection", selectionSort, defaultArray, smallSize);
    doSort ("Medium Selection", selectionSort, defaultArray, mediumSize);
    doSort ("Large Selection", selectionSort, defaultArray, largeSize);
    doSort ("Small Insertion", insertionSort, defaultArray, smallSize);
    doSort ("Medium Insertion", insertionSort, defaultArray, mediumSize);
    doSort ("Large Insertion", insertionSort, defaultArray, largeSize);	
	
	cin.get();
	cin.ignore();
	return 0;

}	// End of int main()
Last edited on Apr 25, 2014 at 4:23pm
Apr 25, 2014 at 4:38pm
I changed elapsedTime to a double and got the following results:

small bubble Elapsed (in seconds): 0
medium bubble Elapsed (in seconds): 0.01
large bubble Elapsed (in seconds): 0.62
small selection Elapsed (in seconds): 0
medium selection Elapsed (in seconds): 0
large selection Elapsed (in seconds): 0.22


insertionSort hung in a loop and never returned, so you have a bug there.

Apr 25, 2014 at 5:19pm
@Abstraction
Hmm I'm still getting 0 even after changing elapsed time to double.
& in regards to Insertion Sort, there's nothing we did wrong on our end?
Apr 25, 2014 at 5:28pm
I'm still getting 0 even after changing elapsed time to double.

Make sure you cast the difference to a double, otherwise you're going to be doing integer division which will result in 0 unless the difference is > CLOCKS_PER_SEC.
 
timeElapsed = (double)(end-start)/CLOCKS_PER_SEC;


in regards to Insertion Sort, there's nothing we did wrong on our end?

Since the function does not exit, there's an infinite loop in there, so yes, there is a bug in it. I suggest you step through it with a debugger.
It fails on the small insertion, so it should be easy to find.

Apr 25, 2014 at 5:30pm
Yea, I did not add (double) to that line.
Ok, thank you so much for your help today. I really appreciate it.
Apr 25, 2014 at 5:35pm
@Abstraction WOW. Haha Look at insertion sort. I had a j++ when it needed to be i++.
Topic archived. No new replies allowed.