why do my min and max values always return the first element of array?

I can't seem to figure out or find the right resources in my book or online to find the min and max values of an array of ten random numbers. This is technically homework but I'm new to C++ and any help would be appreciated. I really have no clue on how to get this to work...

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
#include <iostream>	// for cin, cout, fixed
#include <iomanip>	// for setw, setprecision
#include <cstdlib>	// for srand(), rand()
#include <ctime>	// for time() as a seed for srand()

using namespace std;

int main()
{
	const int SIZE = 10; // set size of the array

	// print header	
	cout << "Student: <your_name>, Assignment #4 of CPSC 342\n";
	cout << "============================================================\n";
	cout << "This program will find the sum, maximum and minimum elements\n";
	cout << "in an integer array.\n\n";

	// seed the rand() by using the time function.
	int t = static_cast<int> (time(0)); // convert the return value to int to avoid a warning
	srand( t );	// seed random number generator

	int A[SIZE];	// declare an array of size 10
	int i, j;		// index variables
	int sum = 0;	// for the total

	cout << "Generate some random numbers ([1-1000]) for Array of A.\n\n";
	cout << "Element #" << setw(20) << "Element Value" << endl;
	cout << "~~~~~~~~~" << setw(20) << "~~~~~~~~~~~~~" << endl;
	
	// assign values into the array and print them out to screen.
	for (i = 0; i < SIZE; i++)
	{
		A[i] = 1 + rand() % 1000;
		cout << setw(9) << i << setw(20) << A[i] << endl;
	}

	cout << "\nNow find the sum of all elements in this array.\n";
	for (i = 0; i < SIZE; i++)
		sum = sum + A[i];

	cout << "The sum is ==> " << sum << endl;

	cout << "\nNow find the maximum value in this array.\n";

	//int max, min, max_i, min_i;	// for max value and its inder q1
	int max, min

	// Hint: assume the first is the max and min at the beginning
	max_i = 0; // use to store the index of the max element
	min_i = 0; // use to store the index of the min element
	max = A[0]; 
	min = A[0];

	// Your code here..
	
	max_i = min_i = A[0];
	for (i = 0; i < SIZE; i++) {
	if (A[i] < min_i) min_i = A[i];
	if (A[i] > max_i) max_i = A[i];
	
	
	if (A[i] > max)
		{
			max = A[i];
			max_i = i
		}
	else if (A[i] < min)
		{
			min = A[i];
			min_i = i 
		}
	
	
	
	
}

	cout << "The maximum value in this array is ==> A[" << max_i << "] = " << max << endl << endl;
	cout << "The minimum value in this array is ==> A[" << min_i << "] = " << min << endl << endl;

	// The following is for extra credits. There are a few ways to sort a group of data. 
	// You may use your common sense or consult any resource and pick up your favoriate.
	//
	cout << "Now sort the array in asending order.\n";

	// Your code here...
	//

	// print out time stamp
	cout << "\n-------------------------------------------------\n";
	cout << "Time Stamp of Running: \n";
	system("time /T");
	system("date /T");
	cout << endl;
	system("pause");

	return 0; 
}


It always shows my min and max as the same number which is the first element A[0] in the print out of the array.
max_i = min_i = A[0];
This is wrong...the indexes should be 0 and not the value of the first element...
I removed that line of code, that was something I had found on an unuseful website.

Isn't this my index's set to 0?
1
2
3
4
max_i = 0; // use to store the index of the max element
min_i = 0; // use to store the index of the min element
max = A[0]; 
min = A[0];




corrected this line too
1
2
3
for (i = 0; i < SIZE; i++)
//is now 
for (i = 1; i < 10; i++)

Still the same outcome as before. min and max are equal and are somehow setting to A[0].


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
const int SIZE = 10;
int t = static_cast<int> (time(0)); // convert the return value to int to avoid a warning
	srand( t );	// seed random number generator

	int A[SIZE];	// declare an array of size 10
	int i, j;		// index variables
	int sum = 0;	// for the total

	cout << "Generate some random numbers ([1-1000]) for Array of A.\n\n";
	cout << "Element #" << setw(20) << "Element Value" << endl;
	cout << "~~~~~~~~~" << setw(20) << "~~~~~~~~~~~~~" << endl;
	
	// assign values into the array and print them out to screen.
	for (i = 0; i < SIZE; i++)
	{
		A[i] = 1 + rand() % 1000;
		cout << setw(9) << i << setw(20) << A[i] << endl;
	}

	cout << "\nNow find the sum of all elements in this array.\n";
	for (i = 0; i < SIZE; i++)
		sum = sum + A[i];

	cout << "The sum is ==> " << sum << endl;

	cout << "\nNow find the maximum value in this array.\n";

	int max, min, max_i, min_i;	// for max value and its inder q1


	// Hint: assume the first is the max and min at the beginning
	max_i = 0; // use to store the index of the max element
	min_i = 0; // use to store the index of the min element
	max = A[0]; 
	min = A[0];

	// Your code here..
	
	for (i = 1; i < 10; i++) {
	if (A[i] < min_i) min_i = A[i];
	if (A[i] > max_i) max_i = A[i];
	
	
	if (A[i] > max)
		{
			max = A[i];
			max_i = j
		}
	else if (A[i] < min)
		{
			min = A[i];
			min_i = i 
		}
	
	
	
	
}

	cout << "The maximum value in this array is ==> A[" << max_i << "] = " << max << endl << endl;
	cout << "The minimum value in this array is ==> A[" << min_i << "] = " << min << endl << endl;
Look at your code from line 40, there's a set of mistakes...I wonder have you ever figured out the different between the index and real elements?Here's my code, i hope it's helpful for you:)

for (i = 1; i < 10; i++) {
if (A[i] > max)
{
max = A[i];
max_i = j
}
else if (A[i] < min)
{
min = A[i];
min_i = i
}
} if there's still any problem about your program ,just reply and i'll help you.
Last edited on
So you just copy-paste code that you found on the street.
Your crap doesn't compile.
Hi

Depends on what you want

1-) If you need only max and min values than

remove the following from yor code

1
2
3
4
5
6
7
8
9
10
if (A[i] > max)
		{
			max = A[i];
			max_i = i
		}
	else if (A[i] < min)
		{
			min = A[i];
			min_i = i 
		}

and change lines 60-61 into

1
2
cout << "The maximum value in this array is "<<  max_i << endl;
cout << "The minimum value in this array is "<<  min_i <<endl;


2-)if you need not only the max min values, but also you need the index of max and min values than

use this
1
2
3
4
5
6
7
8
9
10
11
12
max = min = A[0];
	max_i = min_i = 0 ;
	for (int i = 0; i < SIZE; i++) {
		if (A[i] < min) {
			min = A[i];
			min_i = i;
               }
		if (A[i] > max) {
			max = A[i] ;
			max_i = i ;
                }
}


hope it helps
Last edited on
Followed helpful instructions by all and tried a few different things but the min and max just keep resorting back to A[0] and not giving me the true min or max values, I have corrected some mistakes in the code, and I still can't figure out why it's resorting back to this index only.

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
int main()
{
	const int SIZE = 10; // set size of the array

	// print header	
	cout << "Student: <your_name>, Assignment #4 of CPSC 342\n";
	cout << "============================================================\n";
	cout << "This program will find the sum, maximum and minimum elements\n";
	cout << "in an integer array.\n\n";

	// seed the rand() by using the time function.
	int t = static_cast<int> (time(0)); // convert the return value to int to avoid a warning
	srand( t );	// seed random number generator

	int A[SIZE];	// declare an array of size 10
	int i, j;		// index variables
	int sum = 0;	// for the total

	cout << "Generate some random numbers ([1-1000]) for Array of A.\n\n";
	cout << "Element #" << setw(20) << "Element Value" << endl;
	cout << "~~~~~~~~~" << setw(20) << "~~~~~~~~~~~~~" << endl;
	
	// assign values into the array and print them out to screen.
	for (i = 0; i < SIZE; i++)
	{
		A[i] = 1 + rand() % 1000;
		cout << setw(9) << i << setw(20) << A[i] << endl;
	}

	cout << "\nNow find the sum of all elements in this array.\n";
	for (i = 0; i < SIZE; i++)
		sum = sum + A[i];

	cout << "The sum is ==> " << sum << endl;

	cout << "\nNow find the maximum value in this array.\n";

	int max, min, max_i, min_i;	// for max value and its inder q1


	// Hint: assume the first is the max and min at the beginning
	max_i = 0; // use to store the index of the max element
	min_i = 0; // use to store the index of the min element
	max = A[0]; 
	min = A[0];
	
	// Your code here..
max = min = A[0]	
	max_i = min_i= 0;
	for (int i = 0; i < SIZE; i++) {      // or is it for (i = 1; i <10; i++) ??
	
	
	if (A[i] < min) {
			min = A[i];
			min_i = i; 
	}
	if (A[i] > max) {
			max = A[i];
			max_i = i;
	}
	
	
	
}
	cout << "The maximum value in this array is ==> A[" << max_i << "] = " << max << endl << endl;
	cout << "The minimum value in this array is ==> A[" << min_i << "] = " << min << endl << endl;


the result is supposed to look like this:

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
Student: <your_name>, Assignment #4 of CPSC 342
============================================================
This program will find the sum, maximum and minimum elements
in an integer array.

Generate some random numbers ([1-1000]) for Array of A.

Element #       Element Value
~~~~~~~~~       ~~~~~~~~~~~~~
        0                 782
        1                 473
        2                 429
        3                 997
        4                 206
        5                 950
        6                 260
        7                  49
        8                 499
        9                 709

Now find the sum of all elements in this array.
The sum is ==> 5354

Now find the maximum value in this array.
The maximum value in this array is ==> A[3] = 997
The minimum value in this array is ==> A[7] = 49
Hi

Having changed your code, I got the correct results

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 <iostream>	// for cin, cout, fixed
#include <iomanip>	// for setw, setprecision
#include <cstdlib>	// for srand(), rand()
#include <ctime>	// for time() as a seed for srand()

using namespace std;

int main()
{
	const int SIZE = 10; // set size of the array

	// print header
	cout << "Student: <your_name>, Assignment #4 of CPSC 342\n";
	cout << "============================================================\n";
	cout << "This program will find the sum, maximum and minimum elements\n";
	cout << "in an integer array.\n\n";

	// seed the rand() by using the time function.
	int t = static_cast<int> (time(0)); // convert the return value to int to avoid a warning
	srand( t );	// seed random number generator

	int A[SIZE];	// declare an array of size 10
	int i, j;		// index variables
	int sum = 0;	// for the total

	cout << "Generate some random numbers ([1-1000]) for Array of A.\n\n";
	cout << "Element #" << setw(20) << "Element Value" << endl;
	cout << "~~~~~~~~~" << setw(20) << "~~~~~~~~~~~~~" << endl;

	// assign values into the array and print them out to screen.
	for (i = 0; i < SIZE; i++)
	{
		A[i] = 1 + rand() % 1000;
		cout << setw(9) << i << setw(20) << A[i] << endl;
	}

	cout << "\nNow find the sum of all elements in this array.\n";
	for (i = 0; i < SIZE; i++)
		sum = sum + A[i];

	cout << "The sum is ==> " << sum << endl;

	cout << "\nNow find the maximum value in this array.\n";

	//int max, min, max_i, min_i;	// for max value and its inder q1
	int max, min ;

	// Hint: assume the first is the max and min at the beginning
	int max_i = 0; // use to store the index of the max element
	int min_i = 0; // use to store the index of the min element
	max = A[0];
	min = A[0];

	// Your code here..

	max_i = min_i = A[0];
	for (i = 0; i < SIZE; i++) {


	if (A[i] > max)
		{
			max = A[i];
			max_i = i ;
		}
	if (A[i] < min)
		{
			min = A[i];
			min_i = i ;
		}




}

	cout << "The maximum value in this array is ==> A[" << max_i << "] = " << max << endl << endl;
	cout << "The minimum value in this array is ==> A[" << min_i << "] = " << min << endl << endl;

	// The following is for extra credits. There are a few ways to sort a group of data.
	// You may use your common sense or consult any resource and pick up your favoriate.
	//
	cout << "Now sort the array in asending order.\n";

	// Your code here...
	//

	// print out time stamp
	cout << "\n-------------------------------------------------\n";
	cout << "Time Stamp of Running: \n";
	system("time /T");
	system("date /T");
	cout << endl;
	system("pause");

	return 0;
}
Last edited on
Got it, it wasn't all me ...I was having a problem with visual studio 2008 since that's what my instructor requires for this class..I now have min and max values with indexes, thanks for all the help....now to bubble sort and I'm done
Let's enjoy coding~
Topic archived. No new replies allowed.