Generate random numbers to fill an array

removed
Last edited on
been working on this for hours and am running out of ideas on how to solve my issues
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <ctime>

void populateArray( int ar[], /*const*/ int n )
{
    for( int i = 0 ; i < n ; ++i ) ar[i] = std::rand() % 50 + 1 ;
}

int main()
{
    // http://en.cppreference.com/w/cpp/numeric/random/srand
    std::srand( std::time(nullptr) ) ; // **** important ****

    const int ARRAY_SIZE = 50;
    int ar[ARRAY_SIZE] = {0} ;

    populateArray( ar, ARRAY_SIZE ) ;

    for( int v : ar ) std::cout << v << ' ' ;
}

http://coliru.stacked-crooked.com/a/964716bb4975cb75
thank you! got the generation to work, but am still stuck on finding the average of the generated numbers
If you were going to average 50 numbers with either paper and pencil or a calculator, how would you do it?
the sum divided by the number of numbers being added. My confusion is how to call the sum found in the previous function into my function that computes the average.
So in main(), where did you store the sum? (Which variable name?)

What is the name of the constant which is the "number of numbers"?
Last edited on
in main(), the sum is stored in sumOfElements.

Then using it in the function:
double average(const int ar[], const int n) {

double sumOfElements;
return sumOfElements/n;
}

it states that sumOfElements is uninitialized. I don't know how to pass the sum to the function without just hardcoding the value of my sum.
You do not need a function average. (Unless this is a requirement of the assignment.).

In main(), just use:
 
ave = SumOfElecments / ARRAY_SIZE;


Last edited on
Thanks, got that worked out. Ran into one last issue, the findElementsGreaterThan function is for finding elements that are greater than the average. After locating these elements, they are to be stored in an array and displayed.

My function I have been working on is

void findElementsGreaterThan(double threshold, const int ar[], const int n, int foundElements[], int& count) {
int a = 0;
int sum = 0;

for (a = 0; a < n; a++)
{
sum+=ar[a];
}

double average = sum/n;
threshold = average;

for(int i = 0; i < n; i++) {
if (ar[i] > threshold) {
foundElements[count + 1] = ar[i];
}
}
}

After compiling, this function doesn't evaluate
From the first post:
The assignment was given with the function definitions and the main. The goal is to make the functions work.

From the previous post:
I don't know how to pass the sum to the function without just hardcoding the value of my sum.


Just call it from average(). A version:
1
2
3
4
5
double average(const int ar[], const int n) {

    double sumOfElements = sum(ar, n);
    return sumOfElements/n;
 }

Shorter version:
1
2
3
4
5
double average(const int ar[], const int n) {

    return (double) sum(ar, n) / n;
 
 }


Edit: Please make sure you understand what is "wrong" or "less than ideal" about this version:
1
2
3
4
5
double average(const int ar[], const int n) {

    return sum(ar, n) / n;
 
 }


Last edited on
Still with one last issue, the findElementsGreaterThan function is for finding elements that are greater than the average. After locating these elements, they are to be stored in an array and displayed.

My function I have been working on is

void findElementsGreaterThan(double threshold, const int ar[], const int n, int foundElements[], int& count) {
int a = 0;
int sum = 0;

for (a = 0; a < n; a++)
{
sum+=ar[a];
}

double average = sum/n;
threshold = average;

for(int i = 0; i < n; i++) {
if (ar[i] > threshold) {
foundElements[count + 1] = ar[i];
}
}
}

After compiling, this function doesn't evaluate
1. Do not remove older posts. That is rude.

2. Please use the code tags and sane indentation. They make code easier to read and comment on.

3. What were you thinking, when writing that code for findElementsGreaterThan? Explain every detail.

4.
The assignment was given with the function definitions and the main.

Please show again what was given.
Topic archived. No new replies allowed.