Min and Max of an Array

I have made a program that makes an array of 25 random number between 3 and 7. It prints it, and also the reverse. I also have attempted to return the min and max values, but I am getting a few errors and I cannot understand them. Anyone know what I am doing wrong?

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
84
85
86
87
88
89
90
91
92
93
94
95
 #include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( float a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "
int  lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int  highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)


// **************************************************************************************************************************************
int main ()
{
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    float randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}";

    // **********************************************
    // Calling the function for max and min
    int returnMax = highest(randvalue, 25);
    cout << "Highest value is: " << returnMax << endl;
    int returnMin = lowest (randvalue, 25);
    cout << "Lowest value is: " << returnMin << endl;
    // ***********************************************






    return 0;
}

// Function definition for main array
void showArray ( float a[ ], int size )
{
     for(int i = 0; i < size; i++)
        return PLACEHOLDER;
}
// Function definition for lowest value
int  lowest ( int a[ ], int size )
{
int min
min = a[0];

    for (int i=0; i <25;i++)
    {
     if min < a[i]
        {
            min = a[i]
        }
    }
    return min;
}
// Function definition for highest value
int  highest ( int a[ ], int size )
{
int max;
max=a[0];

    for(int i=0;i<25;i++)
	{
	 if(max>a[i])
        {
			max = a[i];
		}
	}
	return max;
}
Last edited on
Hi,
Firstly :
1
2
3
4
if min < a[i]
        {
            min = a[i]
        }

Missing a semi-colon ( ; ) at the end of the statement.

==>
1
2
3
4
if min < a[i]
        {
            min = a[i];
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Function definition for lowest value
float  lowest ( float a[ ], int size ) // function accepts FLOAT array and returns a FLOAT
{
    float min; // min is of type FLOAT
    min = a[0]; // min is the 0th element

    for (int i=0; i <size;i++)
    {
        if( a[i] < min) // if i-th element is less than min
        {
            min = a[i]; // make that our new min
        }
    }
    return min;
}


Same logic follows for finding max. I trust you'll be able to do that on your own. You can post it here to get feedback.

EDIT: thank you closed account for the correction.
Last edited on
@Arslan7041
You forgot to use the "size" variable.
for (int i = 0; i < 25; i++)

Should be :
for (int i = 0; i < size; i++)
Like so?

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
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( float a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "
float  lowest ( float a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
float  highest ( float a[ ], int size ); // finds and returns the highest value in the array (should be 3)
int  sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array
float averageVal ( int a[ ], int size ); // calculates and returns the average of all values in the array
// **************************************************************************************************************************************
int main ()
{
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    int randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}";

    // **********************************************
    // Calling the function for max and min
    int returnMax = highest(randvalue, 25);
    cout << "Highest value is: " << returnMax << endl;
    int returnMin = lowest (randvalue, 25);
    cout << "Lowest value is: " << returnMin << endl;
    // ***********************************************






    return 0;
}

// Function definition for main array
void showArray ( float a[ ], int size )
{
     for(int i = 0; i < size; i++)
        return PLACEHOLDER;
}
// Function definition for lowest value
float  lowest ( float a[ ], int size ) // function accepts FLOAT array and returns a FLOAT
{
    float min; // min is of type FLOAT
    min = a[0]; // min is the 0th element

    for (int i = 0; i < size; i++)
    {
        if( a[i] < min) // if i-th element is less than min
        {
            min = a[i]; // make that our new min
        }
    }
    return min;
}

// Function definition for highest value
float highest ( float a[ ], int size )
{
    float max;
    max=a[0];

    for (int i = 0; i < size; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}


My function definition for my main array is still missing a return. I was not sure what goes there. Thanks for the min and max help btw, both of you @Arslan7041 @closed account 5a8Ym39o6
Last edited on
You have a number of compile errors.

Line 28,41: You're trying to pass an array of ints to showArray(). However, showArray() is declared to take an array of floats.

Line 46: Ditto for highest. Additionally highest returns a float. You're trying to assign the return value to an int.

Line 48: Ditto for lowest.

Line 64: PLACEHOLDER is undefined. showArray() is going to exit on the first iteration.



@AbstractionAnon Thanks! Here is my updated code. I still am uncertain what to put for "PLACEHOLDER". That's why it is undeclared. Any ideas? I am trying to get something like this:
Making an array of 25 random integers from 3 to 7!
Original array a [ ] = { 3, 7, 5, 6, 3, 4, 4, 3, 5, 5, 6, 5, 5, 7, 3, 5, 3, 6, 4, 5, 7, 4, 7, 3, 5 }
Reversed array a [ ] = { 5, 3, 7, 4, 7, 5, 4, 6, 3, 5, 3, 7, 5, 5, 6, 5, 5, 3, 4, 4, 3, 6, 5, 7, 3 }
Lowest value is 3
Highest value is 7

It worked before, but then I messed it up when I added the min/max :p

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "
int  lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int  highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)
int  sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array
float averageVal ( int a[ ], int size ); // calculates and returns the average of all values in the array
// **************************************************************************************************************************************
int main ()
{
// Array and reverse array
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    int randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}";
// ***********************************************
// Calling the function for max and min
    int returnMax = highest(randvalue, 25);
    cout << "Highest value is: " << returnMax << endl;
    int returnMin = lowest (randvalue, 25);
    cout << "Lowest value is: " << returnMin << endl;
// ***********************************************
// Calling the function for the sum




// ***********************************************
// Calling the function for the average





// ***********************************************



    return 0;
}

// Function definition for main array
void showArray ( float a[ ], int size )
{
     for(int i = 0; i < size; i++)
        return // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< What goes here?
}
// Function definition for lowest value
int  lowest ( int a[ ], int size )
{
    int min;
    min = a[0];

    for (int i = 0; i < size; i++)
    {
        if( a[i] < min) // if i-th element is less than min
        {
            min = a[i]; // make that our new min
        }
    }
    return min;
}

// Function definition for highest value
int highest ( float a[ ], int size )
{
    int max;
    max=a[0];

    for (int i = 0; i < size; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}
Last edited on
You fixed your function prototypes, but you didn't fix your implementations of showArray, and highest. You're going to get linker errors.

71
72
73
74
void showArray ( int a[ ], int size )  // Changed to accept int array
{   for(int i = 0; i < size; i++)
        cout <<  a[i] << endl;          // cout each element.  No return
}



Thank you!! It works. Will post when I make it look more pretty.
@AbstractionAnon How would I separate each number in the array by a comma and a space?

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "
int  lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int  highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)
int  sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array
float averageVal ( int a[ ], int size ); // calculates and returns the average of all values in the array
// **************************************************************************************************************************************
int main ()
{
// Array and reverse array
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    int randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;
// ***********************************************
// Calling the function for max and min
    int returnMax = highest(randvalue, 25);
    cout << "Highest value is: " << returnMax << endl;
    int returnMin = lowest (randvalue, 25);
    cout << "Lowest value is: " << returnMin;
// ***********************************************
// Calling the function for the sum




// ***********************************************
// Calling the function for the average





// ***********************************************



    return 0;
}

// Function definition for main array
void showArray ( int a[ ], int size )
{
     for(int i = 0; i < size; i++)
        cout << a[i];
}
// Function definition for lowest value
int  lowest ( int a[ ], int size )
{
    int min;
    min = a[0];

    for (int i = 0; i < size; i++)
    {
        if( a[i] < min) // if i-th element is less than min
        {
            min = a[i]; // make that our new min
        }
    }
    return min;
}

// Function definition for highest value
int highest ( int a[ ], int size )
{
    int max;
    max=a[0];

    for (int i = 0; i < size; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}
1
2
3
4
5
6
7
8
void showArray ( int a[ ], int size )
{
     for(int i = 0; i < size; i++)
     {
        cout << a[i];
        if( i < size-1 ) cout << ", ";
     }
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194951/
Topic archived. No new replies allowed.