Find min&max in array of 10 random numbers

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, or even fathom how to do the second part which is sort them in order from least to greatest...

code is:

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 just always displays the first randomly generated number as the min and max.
May be something like this...
1
2
3
4
5
6
7
8
9
10
void SortArray(int* Array[])
{
	int* copy[sizeof(Array)];
	Array::Copy(Array, copy, sizeof(Array));
	for(int i = 0; i < sizeof(copy); i++)
	{
			if((copy[i] > Array[i]) && (copy[i] < Array[i-1])) //if greater than original, but less than previous then replace
				Array[i] = copy[i];
	}
}
Last edited on
Something 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
27
	// Hint: assume the first is the max and min at the beginning
	max_i = A[0]; // use to store the index of the max element	
	min_i = A[0]; // use to store the index of the min element
	min = max = 0; 

	// Your code here..

	for (i = 1; i < 10; i++) 
	{
		if (A[i] < min_i) 
		{
			min_i = A[i];
			min =  i;
		}

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


	}

	//**************Your printout has been slightly corrected as well
	cout << "The maximum value in this array is ==> A[" << max << "] = " << max_i << endl << endl;
	cout << "The minimum value in this array is ==> A[" << min << "] = " << min_i << endl << endl;
it is totally unclear why you initially set max_i and min_i to 0

1
2
	max_i = 0; // use to store the index of the max element
	min_i = 0; // use to store the index of the min element 

and then after that you set them to A[0]?!

max_i = min_i = A[0];

In fact you need not to keep indexes of the minimum and maximum elements along with values of maximum and minimum, because if you know indexes you always can get corresponding values.

So you code can be simplified. Instead of

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
	// 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 
		}
	
	
	
	
}


you could write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	// 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

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

	cout << "The maximum value in this array is ==> A[" << max_i << "] = " << A[max_i] << endl << endl;
	cout << "The minimum value in this array is ==> A[" << min_i << "] = " << A[min_i] << endl << endl;
Topic archived. No new replies allowed.