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...
#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()
usingnamespace std;
int main()
{
constint 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
}
elseif (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;
}
// 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.
// 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
}
elseif (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;