Functions and Array

Hi, I have an assignment that I am really stuck on. Question asks us to use a function and an array to have the user enter 10 numbers into the array, then in the function determine the highest and lowest numbers. I cant seem to understand the whole pointer thing and I keep getting an error saying subscript requires array or pointer type. I know that in the function (minMax) I have to use a loop to get through each number but I guess I just don't understand how to do that exactly. I'm pretty lost so any feedback would be appreciated. Thanks!

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
#include <iostream>
#include <string>


using namespace std;



int main()
{

	const int size = 10;
	int listnum[size];
	int number = 0;
	void minMax(int number);


	for (int i = 0; i < 10; i +1)
	{
		cout << "Enter number" << i++ << "  ";
		cin >> listnum[i];
		minMax(number);
	}


	return 0;
}


void minMax(int value)
{

	for (int i = 0; i < 10; i + 1)
	{

	}

}
See "Arrays as parameters" in the tutorial:
http://www.cplusplus.com/doc/tutorial/arrays/

By the way, you went to the trouble of defining a constant,
 
    const int size = 10;

... that's good. Now you should make full use of it wherever applicable.

Hence instead of
 
    for (int i = 0; i < 10; i +1)
put
 
    for (int i = 0; i < size; i++)


... ah - I just spotted an error which I didn't notice at first.
in the third part of the for statement, you have i +1 which calculates the sum of i + 1 and discards the answer. You need to store it. In the long form that would be i = i + 1. It can be more concisely written as i++. (some people prefer ++i, and with good reason, but it doesn't matter here).





Last edited on
When I had it as i++ every time it would ask for the numbers it would only ask for numbers 0,2,4,6,8 then exit and crash. I wasn't really sure why but this seemed to work.
Here, there are a couple of problems:
1
2
3
4
5
6
    for (int i = 0; i < 10; i +1)
    {
        cout << "Enter number" << i++ << "  ";
        cin >> listnum[i];
        minMax(number);
    }

When it is run, the output is:
Enter number0

Notice that the index printed is zero, which is what one would expect. The i++ increments the value after accessing its current value.
So when the execution gets to the next line,
 
    cin >> listnum[i];
the value of i is now 1 (one). So the user input is stored in the second element of the array. Remember arrays are indexed starting from zero. So the first element listnum[0] is not used. More of a problem, what happents to the tenth number input by the user? It ought to go in listnum[9] but instead goes into listnum[10]. That's a serious problem, it is outside the array, so will corrupt some other area of memory with unpredictable results.


I guess what you wanted to do was to display to the user the values from 1 to 10, since that's the way humans count, but at the same time the computer needs to count from 0 to 9.
Here's one way to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    const int size = 10;
    int listnum[size];

    for (int i = 0; i < size; i++)
    {
        cout << "Enter number " << i+1 << "  ";
        cin >> listnum[i];
    }

    cout << "\n\nContents of array\n";
    for (int i = 0; i < size; ++i)
    {
        cout <<  listnum[i] << ' ';
    }
    
    cout << '\n';



I added the next part to display the array as an aid to checking that everything has worked so far.

Haven't yet go to the function minMax(). That's next, now the start of the program is debugged.
Here's the same thing again, but using a function. It may give you a bit of a start on the next part.
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
#include <iostream>

using namespace std;

void display(int * numbers, int size);

int main()
{
    const int size = 10;
    int listnum[size];
    for (int i = 0; i < size; ++i)
    {
        cout << "Enter number " << i+1 << "  ";
        cin >> listnum[i];
    }

    cout << "\n\nContents of array\n";
    display(listnum, size);    

    return 0;
}

void display(int * numbers, int size)
{
    for (int i = 0; i < size; ++i)
    {
        cout <<  numbers[i] << ' ';
    }
    
    cout << '\n';
}
Topic archived. No new replies allowed.