简体   繁体   中英

Finding the max and min values in array of five times

I'm supposed to find the minimum and maximum values in the array, but I can't seem to figure out why the answers aren't correct. For example if I entered "1 2 3 4 5" as my five times, it told me 1 was my maximum and 0 was the minimum. For some reason, whatever the first number is, it calls it the max and it also assigns 0 as the min.

#include <iostream>
using namespace std;

int find_distance(int j); //a function that returns a distance based on the choice j
int intmax, intmin;
int main( )
{

int i =0;
int distance[6]; 
double data[6][5]; 
for(int j = 0; j < 6; j++)
{
    distance[j] = find_distance(j);
    cout << "\nEnter 5 of your best running times for \n " << distance[j] << " m \n";
    for(int i = 0; i < 5; i++)
    {
        cout << "Enter a time \n"; cin >> data[j][i];
    }

}
cout << "Here is your best 5 times: ";
for(int j = 0; j < 6; j++)
{
cout << "\nDistance : " << distance[j] << " m \n";

for(int i = 0; i < 5; i++)
{
    system ("pause");
    cout << data[j][i] << "\t"; } cout << endl;

    if (data[j][i] < intmin) 
    intmin = data[j][i]; 
    else if (data[j][i] > intmax) 
    intmax = data[j][i]; 

    cout << "The maximum time is: " << intmax << endl;
    cout << "The minimum time is: "<< intmin << endl;
}
return 0;
}
int find_distance(int j)
{
switch (j)
{ case 0: // 100 meter 
return 100;
break;
case 1: // 150 meter 
return 150;
break;
case 2: // 200 meter 
return 200;
break;
case 3: // 400 meter 
return 400;
break;
case 4: // 500 meter 
return 800;
break;
default: // 1600 meter
    return 1600;
    }
}

The minimum value is 0 because when you initialize intmin, it is set to 0 by default. You never enter a negative time, so in your comparisons it is always less than the compared value.
The maximum value is off because your for loop ends in an odd place and the comparison code is improperly executed. Change this code:

for(int j = 0; j < 6; j++)
{
  cout << "\nDistance : " << distance[j] << " m \n";

for(int i = 0; i < 5; i++)
{
system ("pause");
cout << data[j][i] << "\t"; } cout << endl; //why does the for loop end here?

if (data[j][i] < intmin) 
intmin = data[j][i]; 
else if (data[j][i] > intmax) 
intmax = data[j][i]; 

            //move the end bracket to this line and it should work

cout << "The maximum time is: " << intmax << endl;
cout << "The minimum time is: "<< intmin << endl;
}

Just to practice:

#include <iostream>
#include <algorithm>
#include <string>
#include <boost/regex.hpp>

int main () {
  using namespace std;

  string input;
  boost::regex re("-?\\d+");
  vector<int> integers;

  cout << "enter sequence of integers: ";
  getline(cin, input);

  boost::sregex_token_iterator begin(input.begin(), input.end(), re, 0);
  boost::sregex_token_iterator end;
  while (begin != end) {
    integers.push_back(stoi(*begin));
    ++begin;
  }

  if (integers.size()) {
    auto pair = minmax_element(integers.begin(), integers.end());
    cout << "min: " << *pair.first << " max: " << *pair.second << endl;
  } else {
    cout << "you didn't enter any integers." << endl;
  }
  return 0;
}

This is how to compile and to run:

$ g++ -o lab_2 -std=c++11 -lboost_regex lab_2.cpp
$ ./lab_2 
$ enter sequence of integers: -10 34 75 101 2 43
$ min: -10 max: 101

Requires boost installed because STL regular expressions aren't functional yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM