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?
1
2
3
4
5
6
7
8
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.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<cstdlib>
#include<ctime>
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: "<<total<<endl;
    cout<<"Average: "<<avg<<endl;
    cout<<"Smallest element: "<<arr[minIdx]<<" at "<<minIdx<<endl;
    cout<<"Largest element: "<<arr[maxIdx]<<" at "<<maxIdx<<endl;
        
    cin.get();
        return 0;
}
 
void createArray(int arr[],int size)
{
    for(int i=0;i<size;i++)
	{
		arr[i]=rand()%5000 +1;
    }
        return;
}
 
int sum(int arr[],int size)
{
        int total=0;
        for(int i=0;i<size;i++)
		{
                total+=arr[i];
        }
        return total;
}
 
double average(int arr[],int size)
{
        int total=sum(arr,size);
        return (double)total/(double)size;
}
 
int smallestAt(int arr[],int size)
{
        int small=0;
        for(int i=1;i<size;i++)
		{
                if(arr[small]>arr[i])
                        small=i;
        }
        return small;
}
 
int largestAt(int arr[],int size)
{
        int large=0;
        for(int i=1;i<size;i++)
		{
                if(arr[large]<arr[i])
                        large=i;
        }
        return large;
}
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.