-4

how do you find the maximum and minimum number from a column in a file? Then display the string associated with that number? ( without using arrays) example

column1: 10 15 105 7 45
string in colum 2: a b c d e

Lola
  • 31
  • 1
  • 4

1 Answers1

1

You just have to read the file line by line, remembering the minimum and maximum value and it's associated string you found till the end.

#include <fstream>
#include <limits>
#include <iostream>

void printMaxAndMin() {
  std::ifstream infile("file.txt");
  int curVal, 
      maxVal = std::numeric_limits<int>::min(), 
      minVal = std::numeric_limits<int>::max();
  std::string curStr, maxStr, minStr;

  while (infile >> curVal >> curStr) {
     maxStr = maxVal < curVal ? curStr : maxStr;
     maxVal = maxVal < curVal ? curVal : maxVal;

     minStr = minVal > curVal ? curStr : minStr;
     minVal = minVal > curVal ? curVal : minVal;
  }
  std::cout << "minimum: " << minVal << ", " << minStr << std::endl;
  std::cout << "maximum: " << maxVal << ", " << maxStr << std::endl;
}
jan
  • 81
  • 3