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:

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
An example to get you started:
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
# include <iostream>
# include <iomanip>
# include <ctime>
# include <cstdlib>
# include <algorithm>

// NOTE: Do not import the standard namespace, nor any other namespaces.  
//   `using namespace _;' can introduce subtle bugs.   
/* using namespace std; */
int main() {
    // NOTE: std::rand() shouldn't be used in favor of the C++11 random library <random>
    //   Using rand() anyways for simplicity.
    std::srand(std::time(nullptr)); // seed the random number generator.
    
    // Get a random integer in the closed interval [1, 100]
    // This will become the size of our array.
    static constexpr int max_size = 100;
    int const size = (std::rand() % max_size) + 1;
    
    // Create an array to hold the max number of elements.
    double rand_nums[max_size]; 
    
    // generate `size' random numbers in the closed interval [0, upper_bound]:
    static constexpr double upper_bound = 100.0; 
    for (int i = 0; i < size; ++i)
        rand_nums[i] = ((double)std::rand() / RAND_MAX) * upper_bound;
    
    // probably you should sort this on your own using your function sort_array()
    // but there is a built-in sort:
    std::sort(rand_nums, rand_nums + size); 
    
    // print the elements of the array
    for (int i = 0; i < size; ++i)
        std::cout << std::fixed << std::showpoint 
                  << std::setprecision(4) << std::setw(8) << rand_nums[i] << "\n";
}

See a live demo here:
http://coliru.stacked-crooked.com/a/88a1816caf36874f
Last edited on
And I discovered the double-post only after spending time doing this ...
http://www.cplusplus.com/forum/beginner/211673/
Topic archived. No new replies allowed.