I am new to C++ and want to learn a lot, I have a project that I'm working on but can't seem to understand it at all.
Here is the assignment:
1. The program first creates an array of 20 random positive numbers between 1 and 100. Make sure there
are no duplicates in the array.
2. Then apply selection sort to sort the array in ascending order, then display the entire array.
3. Now, ask user to enter any number between 1 and 100.
4. Use binary search to check if the user’s number is contained in the array. If so, display ‘yes’ with the
index of the matching element. If not, display ‘no’.
5. Ask if the user wants to continue. If yes, return to Step 3. If no, terminate program.
Any help would be highly appreciated!
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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void Sort(int array[], int numbers)
{
for(int i = 0; i < numbers; i++)
{
for(int j = i + 1; j < numbers; j++)
{
if(array[j] < array[i])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
void cast() {
for (int i = 0; i < 20; i++) {
int num = 2 + (2 * rand() % 100);
cout << num << endl;
}
}
int main()
{
const int ARRAY_SIZE = 20;
int numbers[ARRAY_SIZE];
Sort(numbers, 20);
srand(time(NULL));
cast();
return 0;
}
|
For some reason I can't seem to get Selection Sort working