Compile issue with Visual Studio and Searching and Sorting HW

Hello

My complier is Visual Studio 2019, and this was the homework task. Probably did something wrong in it as well. Each "Task" is annotated by me with a "Process + Number" to make looking through it easy on you guys!

C++ Searching and Sorting Homework
Create an int array of 100000 random numbers (Process 1)
Make a copy of your array (Process 2)
Using this timer
https://stackoverflow.com/questions/22387586/measuring-execution-time-of-a-function-in-c

Run the bubbleSort() algorithm on your array. (Process 3)
Be patient! It is very slow.
Print the execution time
Then run the selectionSort() algorithm on your copy. (Process 4)
Print the execution time

Now run a linear search on 250, 15000, 29000 (Process 5)
Print the execution time

Now run a binary search on 250, 15000, 29000 (Process 6)
Print the execution time

Also please don't give me the "REEEE Namespace STD", that's how my prof taught us how to do it so I'm just keeping it like that, and its easier to read otherwise i'd have to do STD:: on a BUNCH of code

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <ctime>
#include <chrono>
#include <stdio.h>
using namespace std;

void swapping(int& a, int& b) {      //swap the content of a and b
	int temp;
	temp = a;
	a = b;
	b = temp;
}
void display(int* array, int size) {
	for (int i = 0; i < size; i++)
		cout << array[i] << " ";
	cout << endl;
}
void bubbleSort(int* array, int size) { //Bubble Sort 
	for (int i = 0; i < size; i++) {
		int swaps = 0;         //flag to detect any swap is there or not
		for (int j = 0; j < size - i - 1; j++) {
			if (array[j] > array[j + 1]) {       //when the current item is bigger than next
				swapping(array[j], array[j + 1]);
				swaps = 1;    //set swap flag
			}
		}
		if (!swaps)
			break;       // No swap in this pass, so array is sorted
	}
}
void selectionSort(int* array, int size) { //Selection Sort 
	int i, j, imin;
	for (i = 0; i < size - 1; i++) {
		imin = i;   //get index of minimum data
		for (j = i + 1; j < size; j++)
			if (array[j] < array[imin])
				imin = j;
		//placing in correct position
		swap(array[i], array[imin]);
	}
} 
void sleep(float seconds) { //Sleep to pause program 
	clock_t startClock = clock();
	float secondsAhead = seconds * CLOCKS_PER_SEC;
	// do nothing until the elapsed time has passed.
	while (clock() < startClock + secondsAhead);
	return;
}

int search(int arr[], int n, int x) //Linear Search
{
	int i;
	for (i = 0; i < n; i++)
		if (arr[i] == x)
			return i;
	return -1;
}

int binarySearch(int arr[], int l, int r, int x) // Binary Search
{
	if (r >= l) {
		int mid = l + (r - l) / 2;

		// If the element is present at the middle
		// itself
		if (arr[mid] == x)
			return mid;

		// If element is smaller than mid, then
		// it can only be present in left subarray
		if (arr[mid] > x)
			return binarySearch(arr, l, mid - 1, x);

		// Else the element can only be present
		// in right subarray
		return binarySearch(arr, mid + 1, r, x);
	}

	// We reach here when element is not
	// present in array
	return -1;
}
int main()
{

	using chrono::high_resolution_clock;
	using chrono::duration_cast;
	using chrono::duration;
	using chrono::milliseconds;

	srand(static_cast<unsigned int>(time(nullptr)));
	const size_t n { 100000 };
	int arr1[n]{};
	int arr2[n];

	// RANDOM ARRAY PROCESS 1 
	// ========================================================================
	cout << "Running Random Array in 10 seconds:\n";
	sleep(10);
	for (size_t i = 0; i < n; ++i) {
		arr1[i] = rand() % 100 + 1;
		cout << arr1[i] << '\t';
	}

	// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// RANDOM ARRAY PROCCESS 2 
	//=========================================================================
	copy(begin(arr1), end(arr1), begin(arr2)); // Copies Array to Array 2 for execution count 
	cout << "\n \n In 10 seconds array will repeat counting execution time";
	sleep(10);
	auto t1 = high_resolution_clock::now();
	for (auto e : arr2)
		cout << e << '\t';
	cout << '\t';
	auto t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	auto ms_int = duration_cast<milliseconds>(t2 - t1);

	/* Getting number of milliseconds as a double. */
	duration<double, std::milli> ms_double = t2 - t1;

	cout << "\nTime to complete array:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	// Bubble Sort PROCESS 3 
	//==================================================================
	cout << "\n \n Running bubblesort() algorithm in 10 seconds \n";
	sleep(10);

	auto t1 = high_resolution_clock::now();
	bubbleSort(arr1, n);
	auto t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	auto ms_int = duration_cast<milliseconds>(t2 - t1);

	/* Getting number of milliseconds as a double. */
	duration<double, std::milli> ms_double = t2 - t1;

	cout << "Array after bubble sort:";
	display(arr1, n);

	cout << "\nTime to complete bubble sort:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// Selection Sort PROCESS 4 
	//===================================================================
	cout << "\n \n Running selectionsort() algorithm in 10 seconds \n";
	sleep(10);

	auto t1 = high_resolution_clock::now();
	selectionSort(arr1, n);
	auto t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	auto ms_int = duration_cast<milliseconds>(t2 - t1);

	/* Getting number of milliseconds as a double. */
	duration<double, std::milli> ms_double = t2 - t1;

	cout << "Array after selection sort:";
	display(arr1, n);

	cout << "\nTime to complete selection sort:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// Linear Sort PROCESS 5
	// ===============================================================
	cout << "\n Running a linear search on 250, 15000, 29000 in 10 seconds \n";
	sleep(10);

	auto t1 = high_resolution_clock::now();

	int result = search(arr1, n, 250); // Search for 250
	(result == -1)
		? cout << "250 is not present in array"
		: cout << "250 is present at index " << result;

	int result = search(arr1, n, 15000); // Search for 15000
	(result == -1)
		? cout << "15000 is not present in array"
		: cout << "15000 is present at index " << result;

	int result = search(arr1, n, 29000); // Search for 29000
	(result == -1)
		? cout << "29000 is not present in array"
		: cout << "29000 is present at index " << result;

	auto t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	auto ms_int = duration_cast<milliseconds>(t2 - t1);

	/* Getting number of milliseconds as a double. */
	duration<double, std::milli> ms_double = t2 - t1;

	cout << "\nTime to complete linear search:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// Binary Sort PROCESS 6
	// ===============================================================
	cout << "\n Running a binary search on 250, 15000, 29000 in 10 seconds \n";
	sleep(10);

	auto t1 = high_resolution_clock::now();

	int result = binarySearch(arr1, 0, n - 1, 250);
	(result == -1) ? cout << "250 is not present in array"
		: cout << "250 is present at index " << result;

	int result = binarySearch(arr1, 0, n - 1, 15000);
	(result == -1) ? cout << "15000 is not present in array"
		: cout << "15000 is present at index " << result;

	int result = binarySearch(arr1, 0, n - 1, 29000);
	(result == -1) ? cout << "29000 is not present in array"
		: cout << "29000 is present at index " << result;

	auto t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	auto ms_int = duration_cast<milliseconds>(t2 - t1);

	/* Getting number of milliseconds as a double. */
	duration<double, std::milli> ms_double = t2 - t1;

	cout << "\nTime to complete binary search:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	cout << "End of Program" << endl;
	cin.get();
	return 0;
}


Thanks guys you rock!
L134 - t1 has already been defined. Same for t2 on L136. Same for other following lines

L220, 224 result has already been defined. Same for other following lines.

Same for ms_int, ms_double

Within the same scope, a variable can only be defined once.

If you remove all the duplicate definitions, the code then compiles.
Oh that was incredibly dumb.

So I know how to fix all of the "t" defines but how am i supposed to redefine the ms_int and ms_doubles to work? Because i don't think i can do ms_int_1 successfully
Hello nickmcp11,

Next time do not just I have errors post the errors or at least the oned that you do not understand.

Your include files would be better as:
1
2
3
4
5
#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>    // <--- For "rand()" and "srand()".
//#include <stdio.h>  // <--- Covered under "<iostream>". 

In the VS series "<cstdlib>" is included through "<iostream>", but do not count on this for every compiler. It is better to include the header files that you know are needed.

You are halfway there. Include the header file "<thread>" then you can use:
 
std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread". 

There are only 2 things to know: "seconds" can be changed to "milliseconds". The 3 is the whole number of seconds and if it is "milliseconds" 3 becomes 3000. I have found that anything around 250 ms or less is not very noticeable.

As seeplus says that is only part of the 38 errors I received when I compiled.

If you can use the header file "<random>" this would be better than using rand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int GetRandomNum()
{
    const int range_from = 0;
    const int range_to = 201;

    std::random_device rand_dev;

    std::mt19937 generator(rand_dev());

    std::uniform_int_distribution<int>  distr(range_from, range_to);

    //std::cout << distr(generator) << '\n';

    return distr(generator);
}


Andy
to redefine the ms_int and ms_doubles to work? Because i don't think i can do ms_int_1 successfully


You don't need to redefine. Just remove the type from the duplicates.

1
2
3
4
5
//L134 first time defined
auto t1 = high_resolution_clock::now();
...
//L157 now just reuse the variable
t1 = high_resolution_clock::now();


et al etc.
ohhhh okay! I get it
So i changed the dupes but i don't get how to deal with these

1
2
3
4

ms_int = duration_cast<milliseconds>(t2 - t1);
duration<double, std::milli> ms_double = t2 - t1;


after the first use
Hello nickmcp11,

1
2
ms_int = duration_cast<milliseconds>(t2 - t1);
duration<double, std::milli> ms_double = t2 - t1;

After the first definition of line 2 the rest of the program does not need:
1
2
ms_int = duration_cast<milliseconds>(t2 - t1);
duration<double, std::milli> ms_double = t2 - t1;

Just ms_double = t2 - t1. Since you are setting "ms_double" equal to t2 - t2 the value will be overwritten, so there is no problem in reusing the variable. The same is true for the other variables.

I made a change in your code that you may find useful:
1
2
3
4
5
int main()
{
constexpr float SLEEP_TIME{ 3 };  // <--- Original 10

sleep(SLEEP_TIME);  // <--- For the function call 

This way there is only 1 place to change the value and you can adjust it as you like.

You can even change the "cout" statements to make use of the variable "SLEEP_TIME". Doing this the "cout" statements will match any change that you make in "SLEEP_TIME".

After that I added a few (\n)s so the output would not be on 1 line at the end.

Andy
so am i supposed to do this with the int

1
2
3
4
ms_int = duration_cast<milliseconds>(t2 - t1);

ms_int = (t2 - t1);
Hello nickmcp11,

Read carefully:
duration<double, std::milli> ms_double = t2 - t1;. "ms_double" only needs to be defined once. After this the code is redefining the same variable. You do not need to redefine the variable just use it.

Have a look over this. Those lines that are duplicate definitions are commented out with (/* */). Once you get rid of the duplicates it compiles and runs:
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
int main()
{
	constexpr float SLEEP_TIME{ 3 };  // <--- Original 10

	using chrono::high_resolution_clock;
	using chrono::duration_cast;
	using chrono::duration;
	using chrono::milliseconds;

	srand(static_cast< unsigned int >(time(nullptr)));
	const size_t n{ 100000 };
	int arr1[n]{};
	int arr2[n];

	// RANDOM ARRAY PROCESS 1 
	// ========================================================================
	cout << "Running Random Array in 10 seconds:\n";
	
	sleep(SLEEP_TIME);

	for (size_t i = 0; i < n; ++i)
	{
		arr1[i] = rand() % 100 + 1;

		cout << arr1[i] << '\t';
	}

	// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// RANDOM ARRAY PROCCESS 2 
	//=========================================================================
	copy(begin(arr1), end(arr1), begin(arr2)); // Copies Array to Array 2 for execution count 

	cout << "\n\n In 10 seconds array will repeat counting execution time";

	sleep(SLEEP_TIME);

	auto t1 = high_resolution_clock::now();
	for (auto e : arr2)
		cout << e << '\t';

	cout << '\t';

	auto t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	auto ms_int = duration_cast< milliseconds >(t2 - t1);

	/* Getting number of milliseconds as a double. */
	duration<double, std::milli> ms_double = t2 - t1;  // Original definition.

	cout << "\nTime to complete array:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// Bubble Sort PROCESS 3 
	//==================================================================
	cout << "\n \n Running bubblesort() algorithm in 10 seconds \n";
	sleep(SLEEP_TIME);

	/*auto*/ t1 = high_resolution_clock::now();

	bubbleSort(arr1, n);
	/*auto*/ t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	/*auto*/ ms_int = duration_cast< milliseconds >(t2 - t1);

	/* Getting number of milliseconds as a double. */
	/*duration<double, std::milli>*/ ms_double = t2 - t1;  // <--- Duplicate definition.

	cout << "Array after bubble sort:";
	display(arr1, n);

	cout << "\nTime to complete bubble sort:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// Selection Sort PROCESS 4 
	//===================================================================
	cout << "\n \n Running selectionsort() algorithm in 10 seconds \n";

	sleep(SLEEP_TIME);

	/*auto*/ t1 = high_resolution_clock::now();
	selectionSort(arr1, n);
	/*auto*/ t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	/*auto*/ ms_int = duration_cast< milliseconds >(t2 - t1);

	/* Getting number of milliseconds as a double. */
	/*duration<double, std::milli>*/ ms_double = t2 - t1;

	cout << "Array after selection sort:";
	display(arr1, n);

	cout << "\nTime to complete selection sort:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	// Linear Sort PROCESS 5
	// ===============================================================
	cout << "\n Running a linear search on 250, 15000, 29000 in 10 seconds \n";

	sleep(SLEEP_TIME);

	/*auto*/ t1 = high_resolution_clock::now();

	int result = search(arr1, n, 250); // Search for 250

	(result == -1) ? cout << "250 is not present in array\n" : cout << "250 is present at index " << result << '\n';

	/*int*/ result = search(arr1, n, 15000); // Search for 15000

	(result == -1) ? cout << "15000 is not present in array\n" : cout << "15000 is present at index " << result << '\n';

	/*int*/ result = search(arr1, n, 29000); // Search for 29000

	(result == -1) ? cout << "29000 is not present in array\n" : cout << "29000 is present at index " << result <<'\n';

	/*auto*/ t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	/*auto*/ ms_int = duration_cast< milliseconds >(t2 - t1);

	/* Getting number of milliseconds as a double. */
	/*duration<double, std::milli>*/ ms_double = t2 - t1;

	cout << "\nTime to complete linear search:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";

	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// Binary Sort PROCESS 6
	// ===============================================================
	cout << "\n Running a binary search on 250, 15000, 29000 in 10 seconds \n";

	sleep(SLEEP_TIME);

	/*auto*/ t1 = high_resolution_clock::now();

	/*int*/ result = binarySearch(arr1, 0, n - 1, 250);
	(result == -1) ? cout << "250 is not present in array\n" : cout << "250 is present at index " << result << '\n';

	/*int*/ result = binarySearch(arr1, 0, n - 1, 15000);

	(result == -1) ? cout << "15000 is not present in array\n" : cout << "15000 is present at index " << result << '\n';

	/*int*/ result = binarySearch(arr1, 0, n - 1, 29000);

	(result == -1) ? cout << "29000 is not present in array\n" : cout << "29000 is present at index " << result << '\n';

	/*auto*/ t2 = high_resolution_clock::now();

	/* Getting number of milliseconds as an integer. */
	/*auto*/ ms_int = duration_cast< milliseconds >(t2 - t1);

	/* Getting number of milliseconds as a double. */
	/*duration<double, std::milli>*/ ms_double = t2 - t1;

	cout << "\nTime to complete binary search:";
	cout << ms_int.count() << "ms\n";
	cout << ms_double.count() << "ms";

	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	cout << "\n     End of Program.\n     Press enter to continue." << endl;
	cin.get();

	return 0;
}


Andy

Edit:
Last edited on
Topic archived. No new replies allowed.