I've been trying to do this problem for awhile now, but im not getting any luck. im just not completely understanding how single dimensional arrays work.
any help would be great
here's what i have so far, and then the problem is below.

include <iostream>
include <iomanip>
using namespace std; const double ARRAY_SIZE = 100;

void getElements(double arr[], int SIZE) {

for(int i = 0; i < SIZE;i++)
arr[i] = (rand() % 51) + 25;

} double mean(double arr[], int SIZE) {

double arrAvg = 0;
double total = 0;
for(int i = 0; i < ARRAY_SIZE; i++)
{
total +=arr[i];
}
arrAvg = total / ARRAY_SIZE;

return mean; } int getArray(double arr[], int size) {

cout << "Enter the Elements of the Array: \  ";
int i = 0;
for(i = 0; i < size; i++)
{
cin >> arr[i];
if(arr[i] == -1)
return i;
}
return i;

} int main() {

int arr[ARRAY_SIZE];
double size = getArray(arr, ARRAY_SIZE);
getElements( arr, size);
cout<<"The arithmetic mean of the array is: \  "<<mean(arr,size)<<endl;
return 0;

}

Write a program that consists of 3 functions. The function getElements() will be passed an array and the size of the array. The number of elements in the array will be supplied by the user in main() but may be up to 100. getElements() will populate the array with elements between the values of 25 and 75 inclusive using the random number generator. The function mean( ) will be passed the array with random integers created by getElements() and will pass back the arithmetic mean (the “average” – the sum of all the numbers divided by the number of elements – this will be a double) of the numbers in the array. The function main( ) will get the size of the array from the user and then call getElements(). main( ) will then call the function mean( ) to calculate the arithmetic mean; that number will be passed back to main( ) and assigned to a variable. main( ) will then display the arithmetic mean on the screen with an appropriate label.

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
2
Views
7 Years
Discussion Span
Last Post by Lerner

Welcome to the board. Please read about using code tags when posting code to this board so that you syntax formatting is maintained. The information you need is in both the announcement section and the read me before posting post at the top of the board.

The function getElements() will be passed an array and the size of the array. The number of elements in the array will be supplied by the user in main() but may be up to 100.

That means this:

int arr[ARRAY_SIZE];
double size = getArray(arr, ARRAY_SIZE);
getElements( arr, size);

should look more like this:

int arr[ARRAY_SIZE];
double size;
cout << "Enter the Number of Elements to enter into the Array: \ ";
cin >> size;

and you should get rid of getArray() completely. getElements() and mean should both be passed arr, which is an array of ints, not an array of doubles.

You should seed the random number generator, rand(), by calling srand() using an appropriate parameter, in main() before starting everything off.

Edited by Lerner: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.