Process a data set of random numeric information

It should start by creating an array that can hold a maximum of 100 double or float elements. There should also be at least one integer that will hold the number of values that are placed into the array.

In the program, I need the functions:

1
2
3
4
double randDouble();
int buildArray( double array[]);
void printArray( double array[], int numberOfValues);
void sortArray( double array[], int numberOfValues);


Call the buildArray function to fill the array with a random number of random values. The value that is returned from the function should be displayed in the integer variable.

Display the number of values that were placed in the array with an appropriate label.

Display a simple title such as "Unsorted Numbers" and then call the printArray function that is described below to display the array of unsorted values.

Call the sortArray function that is described below to put the values in the array in ascending order.

Finally, display a title for the sorted data and then call the printArray function a second time to display the sorted values.

Also, I only want the float or double to only have 4 decimal places after. i.e. 0.1234

I am really struggling with this assignment and I was wondering if someone can help explain what I need to do. I don't have much but this is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;


int main(int argc, char *argv[])

{
srand((unsigned int)time(0));

    double a = 100.0;
    for (int i = 0; i <= 100.0; i++)
        printf("%f\n", ((double)rand()/(double)(RAND_MAX)) * a);
    return 0;
}



The output should look like:

Run 1 (using srand(0))

Number of values: 58

Unsorted Numbers
23.5572 64.8152 7.4374 27.0241 36.0027 25.5287 98.5290
31.8918 93.4233 17.8625 85.7570 3.4852 0.8576 62.6759
48.5885 27.2988 80.2179 9.1464 44.8012 64.0156 97.3266
66.0878 79.0613 56.3280 4.0376 88.1405 6.8361 29.6793
98.5076 7.4648 1.8006 2.5636 56.7248 51.5976 64.8122
72.0573 38.5052 38.0139 2.6460 90.1303 20.9906 86.1324
54.5885 96.4293 62.8193 37.2692 94.9461 23.1269 52.0829
23.2826 89.7366 37.4737 41.2152 74.4285 35.5510 37.6263
21.9001 7.1139

Sorted Numbers
0.8576 1.8006 2.5636 2.6460 3.4852 4.0376 6.8361
7.1139 7.4374 7.4648 9.1464 17.8625 20.9906 21.9001
23.1269 23.2826 23.5572 25.5287 27.0241 27.2988 29.6793
31.8918 35.5510 36.0027 37.2692 37.4737 37.6263 38.0139
38.5052 41.2152 44.8012 48.5885 51.5976 52.0829 54.5885
56.3280 56.7248 62.6759 62.8193 64.0156 64.8122 64.8152
66.0878 72.0573 74.4285 79.0613 80.2179 85.7570 86.1324
88.1405 89.7366 90.1303 93.4233 94.9461 96.4293 97.3266
98.5076 98.5290
Last edited on
so the steps in your program would be:
1. declare double myArray[100] which is passed to buildArray
2. within buildArray() first generate a integer (or more correctly a size_t) variable b/w 0 and 100, lets call it fillNum
3. fill myArray with fillNum random numbers b/w lower and upper bound
4. return fillNum
5. proceed to printArray(), sortArray() ...
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
#include <iostream>
#include <random>
#include <chrono>
#include <algorithm>

constexpr auto SIZE = 100;

size_t buildArray(double myArray[])
{
    auto seedInt = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seedInt);//engine
    std::uniform_int_distribution<size_t> di(1,SIZE);//distribution
    auto fillNum = di(dre);
    auto seedDouble = std::chrono::system_clock::now().time_since_epoch().count();//seed
    for (size_t i = 0; i < fillNum; ++i)
    {
        std::default_random_engine dre(seedDouble);//engine
        std::uniform_real_distribution<double> di(0,SIZE);//distribution
        std::generate(myArray, myArray+fillNum, [&dre, &di]{return di(dre);});
    }
    return fillNum;
}
void printArray(const double myArray[], const size_t fillNum)
{
    for (size_t i = 0; i < fillNum; ++i)std::cout << myArray[i] << "\n";
}
void sortArray(double myArray[], size_t fillNum)
{
    for (size_t i = 0; i < fillNum; ++i)
    {
        for (size_t j = i+1; j < fillNum; ++j)
        {
            if (myArray[j] < myArray[i])std::swap(myArray[j],myArray[i]);
        }
    }
}

int main()
{
    double myArray[SIZE];
    auto fillNum = buildArray(myArray);
    std::cout << "Numbers filled: " << fillNum << "\n"; 
    printArray(myArray, fillNum);
    std::cout << "\nSorted Array:\n\n";
    sortArray(myArray, fillNum);
    printArray(myArray, fillNum);
    //  reason why the second call to printArray() works is below ...
    //  http://stackoverflow.com/questions/19646512/passing-an-array-by-reference-in-c
}
Topic archived. No new replies allowed.