Array's minimum value returning as 0.

I am writing a code that uses functions to determine the minimum and maximum values in an array. The maximum function works fine, but the minimum keeps returning as 0. Looking for any clues as to where my mistake might be. 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
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
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <locale.h>
#include <stdio.h>
#include <functional>
#include <sstream>
#include <cstdlib>

using namespace std;

int maxValue(int lists[],int i,int max);
int minValue(int lists[],int i,int min);
int main(int argc, char** argv) 
{//top of main

    //Menu Driven Program
    
    int lists[7]={3,4,1,5,2,33,7};
    int n,i,max,min;
    do
    {//top of do
        cout<<"1. Minimum Value."<<"\n";
        cout<<"2. Maximum Value."<<"\n";
        cout<<"3. Quit."<<"\n";
        cout<<"Please Enter Selection: ";
        cin>>n;
        switch(n)
        {//top of switch
            case 1:
                cout<<"You entered a 1."<<"\n";
                max=maxValue(lists,max,i);
                cout<<"The maximum value is "<<max<<"\n";
                break;
            case 2:
                cout<<"You entered a 2."<<"\n";
                min=minValue(lists,min,i);
                cout<<"The minimum value is "<<min<<"\n";
                break;
            case 3:
                cout<<"You entered a 3."<<"\n";
                break;
            default:
                cout<<"Invalid entry."<<"\n";
                break;
        }//end of switch
    }//end of do
    while(n!=3);
    return 0;
}//end of main
int maxValue(int lists[],int i,int max)//max value function
{//top of max function
    for(int i=0;i<7;i++)
    {
        if(lists[i]>max)
        {
            max=lists[i];
        }
    }
    return max;
}//end of max function
int minValue(int lists[],int i,int min)//min value function
{//top of min function
        for(int i=0;i<7;i++)
    {
        if(lists[i]<min)
        {
            min=lists[i];
        }
    }
    return min;
}//end of min function 
Last edited on
Figured it out. Just initialized int min/int max within their respective functions, rather than in the main. I would still love any notes. Thanks.

Also, how do I post a segment of code without copy-and-pasting?
Well, you are passing unnecessary parameters to the functions - but not passing the essential parameter which will specify the array size.

Cutting out the unused stuff, here is an example of finding the maximum.

Use the first array element as the initial value.

The minimum function would be almost identical except for the comparison operator < or >.

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

using namespace std;

int maxValue(int lists[], int size);

int main()
{
    const int size = 7;
    
    int lists[size] = {3, 4, 1, 5, 2, 33, 7};
    
    int n = 0;
    
    do {
        cout << "1. Maximum Value.\n";
        cout << "3. Quit.\n";
        
        cout << "Please Enter Selection: ";
        cin >> n;
        
        switch(n)
        {
            case 1:
                cout << "You entered a 1.\n";
                cout << "The maximum value is " << maxValue(lists, size) << "\n";
                break;
                                
            case 3:
                cout << "You entered a 3.\n";
                break;
                
            default:
                cout << "Invalid entry.\n";
                break;
                
        }
    } while (n!=3);
    
    return 0;
}

int maxValue(int lists[], int size)
{
    int max = lists[0];
    
    for (int i=1; i<size; i++)
    {
        if (lists[i] > max)
        {
            max = lists[i];
        }
    }
    
    return max;
}
Hello al bama cpp,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Starting at the top. Why all the header files? All you need is "iostream" for this
program. The rest are never used and more than half are the C++ version of a C header file.

Line 30) int n, i, max, min; you need to initialize your variables. Always a good idea to initialize your variables where ever they my be, i.e., local or global. Also a good idea to initialize "min" to a value greater than what could be in the array.

Your menu choices are backwards compared to the case statements or the other way around.

In case 2 the function call minValue(lists, min, i); you are passing "min" and "i", but in the function definition minValue(int lists[],int i, int min) notice the difference in the order of the variables. Since "i" is zero when passed in the function "min" becomes zero in the function then in the if statement nothing is less than zero so zero is returned at the end of the function.

In the end the variable "i" does not need to be passed to either function because it is never used in either function and the for loops create the local variable "i" for the use in the loop.

After that I added some "\n"s to the cout statements so the output does not run together.

Hope that helps,

Andy
Sorry about all of the header files. I adjusted my default main to include them when I start a new project until I grasp when I do and don't need them. I will remember to edit future posts. Thank you for pointing out the backwards menu choices, and the tip for code tags.

So to be clear, if int i NEEDED to be passed, I should take care of the order I pass my parameters so I don't inadvertently define subsequent variables. However, in this case passing it at all only creates clutter and room for error?
Here is my revised function, which is working well and easier for me to follow, thank you guys for the advice.

1
2
3
4
5
6
7
8
9
10
11
int minValue(int lists[],int size)//min value function
{//top of min function
    int min=lists[0];
    for(int i=0;i<size;i++)
    {
        if(lists[i]<min)
        {
            min=lists[i];
        }
    }
    return min;

The follow up question then: Is int i being declared in the initialization of the for loop? Since i don't declare it anywhere else.
1
2
3
4
5
    const int size=7;
    int lists[7]={3,4,1,5,2,33,7};
    int n=0;
    int min=lists[0];
    int max=lists[0];
The follow up question then: Is int i being declared in the initialization of the for loop? Since i don't declare it anywhere else.


Yes, it is declared in the first line of the for loop:
 
    for(int i=0;i<size;i++)


If there was some reason to declare i outside the loop. you might do it like this:
1
2
    int i = 0;
    for(  ; i<size; i++)
but there is no good reason to do so here, once the loop ends, i is no longer needed.

Since you set max within the function maxValue()
 
int max = lists[0];
then there is no need to also set the initial value in main() too. It serves no purpose.

In fact the variable max is not needed at all inside main() - as I showed
1
2
3
4
            case 1:
                cout << "You entered a 1.\n";
                cout << "The maximum value is " << maxValue(lists, size) << "\n";
                break;
notice there is no variable named max there.

Or if you really wanted to declare it, you might do it like this:
1
2
3
4
5
6
7
            case 1:
            {
                cout << "You entered a 1.\n";
                int max = maxValue(lists, size);
                cout << "The maximum value is " <<  max << "\n";
                break;
            }

Last edited on
Thank you for all of your insight.
Topic archived. No new replies allowed.