Passing array into function

Ok so i have the counter working but the output for max and min is a long random number. can anyone give me some hints?

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
83
 /*
Problem description:  Write a program that reads an unspecified number (no more than 20) of 
    positive integers, finds, and prints the maximal and minimal values using an array. 
    The input ends with the 0. For example, if you entered 1, 100, 20, 2, and 0, 
    the program should print 100 and 1.

This exercise is for learning the array concept with a proper use of reference and value parameters.
*/

#include <iostream>
using namespace std;

//function prototype of a function returning max & min values of an integer array

//function prototype of getBoundary function returning max, min values of an void void 
   void getBoundary(int value[], int size, int& max, int& min);

    int main( )
{
    const int    CAPACITY = 20;    //array capacity
    int        value[CAPACITY];
    int        max, min;   // maximum and minimum values stored in the array value
    int        intNum;   //stores the number of integer read
    
    // read values from the input until it reads 0 and store the values in the array except 0


         int i=0;
       for(int j=0; j<CAPACITY; j++)
          {
               value[i]=0;
          }



               int num;

               intNum=0;
              cin>>num;
   while(num!=0 && num<=20)
       {
              value[i]=num;
               intNum++;
                 i++;
                cin>>num;
        }


getBoundary(value, intNum, max, min);

    cout << intNum <<" integers have been read." << endl
         << "The maximal value is " << max << endl
         << "The minimal value is " << min << endl;

return 0; 

}

void getBoundary ( int value[], int size, int&max, int&min)

{

min=0;
max=0;

for(int i=0;i<20;i++)
	{
		if(min>value[i])
		{
			min=value[i];
		}
		else if(max<value[i])
		{
			max = value[i];
		}
	}


}



}
Last edited on
anyone?
You don't ever print those values; how do you know they are random numbers? You don't ever call getBoundary() either.
sorry i left out the call by accident when i posted this. the program tells me what function prototype to use. other then that not sure why it keeps giving me 13407 for the max value of a file with the numbers 1 0.
Last edited on
I'm guessing your printing the memory address rather than an actual value. You left out how your passing the arguments to your function and how you are printing your values. Plus while(num!=0) is bad because it allows the user to potentially go past the array boundary.
Last edited on
1
2
3
 cout << intNum <<" integers have been read." << endl
         << "The maximal value is " << max << endl
         << "The minimal value is " << min << endl;


this is precoded by the program.

and would while(num!=0 && num<=20) work?

er i could replace 20 with CAPACITY.
Last edited on
You would want intNum < CAPACITY as the extra condition on the input.

The problem is you are looping from 0 to 20 through your array, but the user may not have input that may numbers into the array. The chances are good that the random garbage in that unused portion is a positive or negative number with a much larger magnitude than anything input by the user.
Ok this program is a bit retarded so bear with me.

It uses cin for when it has input from files, thats just how it does it. I've complained about it before but alas. so its not input from a keyboard but actually from a file thats linked to the program. just wanted to mention that.

and so youre saying its getting noise from the unused space in the array? i actually initialized it with 0 to check that but it didnt change the output?
Last edited on
Try posting the entire updated code (preferably with some indentation) so we can see what you are using now.
I updated it. for some reason it wont let me tab to indent though :/
Most browsers use tab for other purposes than inserting the tab character, so you'll have to use spaces or copy the code in from an editor with the tab character in it.

In any case, the program works fine for me when I fix the errors I described (use of intNum, CAPACITY in main(); size in function call).
could you show me what you changed codewise?

I think i changed it correctly but, mine still doesnt work.

1
2
3
4
5
6
7
8
9
10
    intNum=0;
    cin>>num;

 while(num!=0 && intNum < CAPACITY)
 {
 value[i]=num;
 intNum++;
  i++;
  cin>>num;
 }
Last edited on
Now you should make sure are passing the right size to your function and that you are actually using that size in the function.
ugh. ok now minimal is only outputting 0 but max is working.

AHA min has to be set to 1, wait no. nvm
Last edited on
Thanks so much! I finally got it done!
Last edited on
Topic archived. No new replies allowed.