### Array() function help

The program I am trying to write but am very confused on how to do needs to generate 10000 random numbers between 1 and 5000 and produce some statistics on them. The program should fill the array with random numbers, and then output the sum, average, largest value, and smallest value in the array. The following functions are required for this program: a createArray() function that will fill the array with the random numbers, a sum() function that will compute the sum of the values in an array, an average() function that will compute the average value in the array, a smallestAt() function that will return the location of the smallest value in the array, a largestAt() function that will return the location of the largest value in the array .. do not hard code the size of the array into the functions.

Appreciate any help!

Do you need an array (like int x[10];) or you can use other containers as well? Can you use std::array?
No I cant use std::array.
What about standard library functions?
 ``12345678`` ``````void createArray(int x[], size_t size) { //std::generate(x, x + size, []{return (std::rand() % 5000) + 1;}); //^^Can you use that? If not, use the code below for (size_t i = 0; i < size; ++i){ x[i] = (std::rand() % 5000) + 1; } }``````
Last edited on
Im not used to the whole std:: The other program the I used the datafile for wont let me submit it because it has so many errors. I posted the errors on the that topic.
So use bottom code (remove comments).

Please show what you got or post where exactly do you have problems. Most people will not do your homework for you but they more than willing to help with your problems and explain things.
Here's what Ive figured out.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include #include #include using namespace std; void createArray(int arr[],int size); int sum(int arr[],int size); double average(int arr[],int size); int smallestAt(int arr[],int size); int largestAt(int arr[],int size); int main() { const int SIZE=10000; int arr[SIZE]; int total, minIdx,maxIdx; double avg; srand(time(0)); createArray(arr,SIZE); total=sum(arr,SIZE); avg=average(arr,SIZE); minIdx=smallestAt(arr,SIZE); maxIdx=largestAt(arr,SIZE); cout<<"Total: "<arr[i]) small=i; } return small; } int largestAt(int arr[],int size) { int large=0; for(int i=1;i
`using namespace std;` is a bad practice in C++
If you want to continue study programming after school, stop using it now. It will save you much time later.

`(double)total/(double)size;` Do not use C-style casts in C++: `static_cast<double>(total) / size;` (You only need to cast at least one of operands)

I congrats you on completing this assigment. Was it hard to do it yourself? Did you learn something from it?
Note for future: Most people will not even try to help with homework if you will not give at least some proof that you had done it and encountered some real problems (code you have already written, question "how to determine array size in function?" instead of "how to do everything?" for example). Also rules of this forum:
 Don't post homework questions Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.
Last edited on
Topic archived. No new replies allowed.