What does this code mean?

My professor gave us this code last class as an example and I am still confused as to what the main function is doing and what the code even does. Can someone please give me an explanation of what is happening in this code especially in the main. I need to be able to understand it to do our homework problem. We are dealing with pointers currently.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int average(const int* array, int size)
{
	int intTotal = 0;
	for (int i = 0; i < size; i++)
	{
		intTotal = intTotal + *(array + i);
	}
	return intTotal / size;
}

double average(const double* array, int size)
{
	int doubleTotal = 0;
	for (int i = 0; i < size; i++)
	{
		doubleTotal = doubleTotal + *(array + i);
	}
	return doubleTotal / size;
}

int main()
{
	const int SIZE = 10;
	double list2[SIZE];

	cout << "Enter ten numbers: ";

	for (int i = 0; i < SIZE; i++)
	{
		cin >> list2[i];
	}

	cout << "list2's total is " << average(list2, SIZE) << endl;

	return 0;
}
My professor gave us this code last class as an example

There at least two errors in the code.

line 18 should read
 
	double doubleTotal = 0;


line 38 should read
 
	cout << "list2's average is " << average(list2, SIZE) << endl;

haha I guess he gave us code with an error. What does list2 mean? What is the purpose of this? Also why do I have to output list2 in a for loop?
It seems this program is all about arrays and pointers.

It might be a good idea to go through the tutorial pages on those topics - it should illuminate some of the apparently mysterious parts of the code.
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/pointers/

Now considering the main() function in this program.

Line 28 defines a constant - this is a bit like a variable except it cannot be changed once it has been given an initial value.
Line 29 defines an array named list2 which contains 10 individual elements of type double.

Lines 33 to 36 display a prompt and request the user enters the value to be stored in each of those array elements.

Line 38 both outputs some text and also calls the function named average() at line 16. The result of that function is also displayed.

Note. There are two functions named average(). They differ only in the parameter list (one expects an array of integers, the other an array of doubles). The compiler selects the correct one based in the type of parameters passed to it from main(). This is called function overloading.

One interesting point is the syntax *(array + i) (line 21). That is using pointer syntax to access an array element. The corresponding array syntax would be array[i]. Both achieve the same result, accessing the ith element of the array (counting from zero as the first element).

Last edited on
Understand that arrays are pointers, essentially. In fact, an array name is a constant pointer to the first element of the array.
Last edited on
wow this was actually very helpful. Thank you so much!
Understand that arrays are pointers, essentially. In fact, an array name is a constant pointer to the first element of the array.

It's useful to know that an array identifier can act as a pointer in many situations, but an array is an array - not a pointer, const or otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iomanip>
#include <iostream>
#include <typeinfo>

int main() {
    int a[256];
    int* const a_ptr = a;
	
    std::cout << std::boolalpha;
    std::cout << "sizeof(a) = " << sizeof(a) << '\n';
    std::cout << "sizeof(a_ptr) = " << sizeof(a_ptr) << '\n';
    std::cout << "a is the same type as a_ptr? " << (typeid(a) == typeid(a_ptr)) << '\n';
}
closed account (48T7M4Gy)
http://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer-in-c
Topic archived. No new replies allowed.