Need help with C++ project

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
Last edited on
@Semirxbih

You haven't even done #1 of the assignment. You created an array on line 33, then you send it to be sorted, BUT nowhere do you do assignment part 1. It's an uninitialized array
@whitenite1

How do I do that? I'm still in the learning process, was I supposed to declare the ARRAY_SIZE up top? I'm trying to have them all aligned in ascending order with no duplicates, having a hard time.
Last edited on
Yes...you are also supposed to make it have random number
 
int i = (rand()% 99) + 1 //an example 
@Flaze07

Doesn't srand work better or just use rand?
Srand is enterinf the seed
Rand is the proccess
Got it! Now it finally is in ascending order, but it's not even numbered anymore.

1
2
3
4
5
6
void cast() {
  int i = (rand()% 99) + 1
   for (int i = 0; i < 20; i++) {
      cout << i << endl;
   }
}


This is the code that I have it throw even numbers between 1 and 100 but there's duplicates and no ascending order

1
2
3
4
5
6
void cast() {
   for (int i = 0; i < 20; i++) {
      int num = 2 + (2 * rand() % 100);
      cout << num << endl;
   }
}
I think you are supposed to...use the array to search...not another variable...
Maybe you should use vector
1
2
3
4
5
6
7
8
9
10
void cast(std::vector<int>& v)
{
     for (auto& a : v) std::cout << a << std::endl;
     /* same as
      for (int i = 0: i < v.size(); ++i)
      { 
          std::cout << v.at(i) << std::endl;
      }
     */
}
I'm just a little lost on the project, can't seem to find a template that would at least help me know the structure of the project and where I need to begin
@Flaze07

Seems as if my compiler is throwing all sorts of errors with that
I see...I am not on my computer so I cannot make the template...how about learning array again and then come back to this exercise after you have understood

I'm just a little lost on the project, can't seem to find a template that would at least help me know the structure of the project and where I need to begin

The assignment instructions are what you should be following. Break the instructions up into functions. This will help with the structure of the program. For example, for the first instruction, just focus on filling the array with random numbers. Then figure out a way to remove or prevent duplicates in the array.
Have a look at the example here: http://www.cplusplus.com/reference/cstdlib/rand/
1
2
3
4
5
// will randomly fill arr with sz numbers [1, 100)
void fill_array( int arr[], int sz )
{

}

Last edited on
Topic archived. No new replies allowed.