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...
#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;
}
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.
constint 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
}
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;
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.
if (A[i] > max)
{
max = A[i];
max_i = i
}
elseif (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 ;
}
}
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.
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
// 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;
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
#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
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;
}
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