Is this the proper way to solve this?

I'm writing a program to find the minimum and average values of an array. The code runs fine but a lot of it was written based on scouring Google. I feel like I have a decent idea how it works but I'm still a beginner when it comes to calling functions and such. Any help would be greatly appreciated.

Thanks, Jeremy.

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
  //Lab 3-2: Array with 10 elements with algorithm for computing average value, 
//finding minimum value and showing corresponding index. 

#include <iostream>
using namespace std;

//get function print into memory
double average(double firstarray[], int numofNums);
double minimum(double firstarray[], int numofNums);



int main()
{
	double myarray[] = { 100, 27, 64, 55, 32, 73, 89, 14, 6, 44 };
	const int numofNums = 10;

	cout << "Average equals " << average(myarray, numofNums) << endl;
	cout << "Minimum equals " << minimum(myarray, numofNums) << endl;

	cin.get();
	return 0;
}

double average(double myarray[], int numofNums){
	double sum = myarray[0];

	for (int i = 1; i < numofNums; i++) {

		sum += myarray[i];

	}

	return sum / numofNums;
}

double minimum(double myarray[], int numofNums){
	int min = 0;

	for (int i = 1; i < numofNums; i++) {

		if (myarray[min] > myarray[i]){
			min = i;
		}
		
	}

	return myarray[min];
}
This is a fine code. Personally I would change some things, but it is a matter of preference.
This seems good. Do you have any specific questions?

At line 16, the number of items in an array is sizeof(array)/sizeof(array[0]). If you use this expression to set numOfNums, then you can change the number of items in myarray without worrying about breaking things.

Notice that the average and minimum require that the array has at least one item. This may not be worth fixing for a beginner problem, but I'll point it out anyway. Also, you're passing numOfNums in both functions as an int. What if someone passes -10? It would be better to make these unsigned int instead to better express the idea that they cannot be negative. Then i in the for loops would be unsigned also.

One important note: by default, C++ passes parameters by value except for arrays. Arrays are passed by reference by default.
I guess I just don't understand what you mean at line 16? Are you saying I should replace what is there with what you wrote? And I'm not sure how to make it an unsigned int instead... I'm sorry I'm just having trouble wrapping my head around all of it. For the most part I don't fully understand how the for loops are working. I have a pretty good idea but I'm still second guessing it. Thank you for your help.
I guess I just don't understand what you mean at line 16?
Construct sizeof(array)/sizeof(array[0]) is a common way to know array size. However it works properly only with values of array type (not decayed to pointers). Because of that it is usually wrapped in template function which would give you an error with incorrecy use.
You may replace it here and it would be the good thing as it makes code more robust.

And I'm not sure how to make it an unsigned int instead
You do not have to. Ability to pass invalid size is left to user and if somebody will do it, it would be their fault for violation of function contract. If you want to be really standard conforming and be able to receive all (theoretically) possible arrays, replace all int related to array size/index with std::size_t, as it by definition unsigned type, large enough to contain maximum possible array size.

If you still struggling with for loops, this might help you:
http://www.learncpp.com/cpp-tutorial/57-for-statements/
http://www.cplusplus.com/doc/tutorial/control/
Thanks again for all of your guys help. This is a great community!
Topic archived. No new replies allowed.