populate array with random numbers

I'm trying to populate an array with random numbers ranging from 1-200 (repeats acceptable) but I don't know how to do this. I also want to sort them and output the sorted values (not the unsorted ones). I know I currently have it outputting the unsorted values but I'm kind of lost.

Any guidance and/or pointing in the right direction for tutorials is greatly appreciated.

This is my code so far.

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
#include <iostream>
#include <cstdlib> // for rand and srand
#include <ctime> // for the time function
#include <fstream> //for output file

using namespace std;

//Function prototypes
void populateArray (int [], int); //generates random numbers
void selectionSort (int [], int); //performs an ascending order selection sort on array. size is the number of elements
int sequentialSearch (const int [], int, int); //also known as linear search
int binarySearch (const int [], int, int); //performs binary search


int main()
{
    int randomNum;
    
    //seed the random number generator
    srand(static_cast<unsigned int>(time(NULL)));
    
    
    const int SIZE = 200;
    int randNumArray [SIZE]={};
    
    //generate random numbers and store them in array
    populateArray(randNumArray, SIZE);
    
    //sort the values
    selectionSort(randNumArray, SIZE);
}
  
void populateArray (int array[], int size)
{
    
    for (int count = 1; count < size; count++)
    {
        array[count]=1+rand()% size;
        cout <<array[count]<<"\n";
    }
}

void selectionSort (int array[], int size)
{int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (size - 1); startScan++)
    {minIndex = startScan;minValue = array[startScan];
        
        for (int index = startScan + 1; index < size; index++)
        {
            if (array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}
int sequentialSearch (const int array[], int size, int value)
{
    int index = 0, position = -1;
    bool found = false;
    
    while (!found && index < size)
    {
        if (array [index] == value)
        {
            found = true;
            position = index;
        }
        index++;
    }
    return position;
}

int binarySearch (const int array[], int size, int value)
{
    //first array element, last array element, midpoint of search and position   of search value
    int first = 0, last = size -1, middle, position = -1;
    
    //flag
    bool found = false;
    
    while (!found && first <= last)
    {
        middle = (first + last)/2;      //calculates midpoint
        if (array [middle] == value)    //if value is found at midpoint
        {
            found = true;
            position = middle;
        }
        else if (array[middle] > value) //if value is found in lower half
        {
            last = middle - 1;
        }
        else                            //if value is found in upper half
        {
            first  = middle + 1;
        }
    }
    return position;
}


Line 36: Array starts at 0, count should do the same.

As for your output problem, move the cout from line 39 to sometime after your sort.
Thanks. I still get the same unsorted output though.
bump
The only output you have anywhere in your code is in the populateArray function. Why would you expect to see sorted output?

Write a function that prints arrays so you can call it whenever you want to see what your array looks like.
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 <iostream>   //std::cout
#include <algorithm>  //std::swap
#include <cstdlib>    //std::srand, std::rand
#include <ctime>      //std::time

void populateArray(int arr[], std::size_t size)
{
    for(int i = 0; i < size; ++i)
    {
        arr[i] = ( std::rand() % 200 ) + 1;
    }
}

void printArray(int arr[], std::size_t size)
{
    for(int i = 0; i < size; ++i)
    {
        std::cout << arr[i] << ' ';
    }
    std::cout << '\n';
}

void selectionSort(int arr[], std::size_t size)
{
    for(int i = 0; i < (size - 1); ++i)
    {
        int minIdx = i;
        for(int j = (i + 1); j < size; ++j)
        {
            if(arr[j] < arr[minIdx]) minIdx = j;
        }
        if(minIdx != i) std::swap(arr[i], arr[minIdx]);
    }
}

int main()
{
    std::srand(std::time(nullptr));

    const int ARRAY_SIZE = 5;
    int arr[ARRAY_SIZE];

    populateArray(arr, ARRAY_SIZE);
    printArray(arr, ARRAY_SIZE);

    selectionSort(arr, ARRAY_SIZE);
    printArray(arr, ARRAY_SIZE);

    return 0;
}
Last edited on
Thanks booradley60 this helped a lot. I think I've just been staring at this problem too long.
Topic archived. No new replies allowed.