1

I'm trying to figure out a code that can determine the largest number, smallest number, second largest number, and second smallest number for ay set of numbers inputted. Here is my code. I am having issues mainly due to the initialization of variables. I'd appreciate any help!

My code:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


int main(){
    string buffer;
    unsigned n;
    double min = 1,  max = 0;
    double min2 = 1, max2 = 5;
    cout << "How many integers will you enter? ";
    cin >> n || die("Input failure ");
    cout << "OK type the " << n << " integers, non #'s to quit: ";
    for (unsigned count = 0; count < n; count++){
        double num;
        cin >> num;
        if (min > num){
            min = num;
        }
        else if (max < num){
            max = num;
        }
        else if (num >= max2 && num <= max){
            max2 = num;
        }
        else if (num >= min2 && num <= min ){
                min2 = num;
        }

    } cout << "The largest number is: " << max << endl;
    cout << "The smallest number is: " << min << endl;
    cout << "The second smallest number is: " << min2 << endl;
    cout << "The second largest number is: " << max2 << endl;

    cin >> buffer;
}
Bauss
  • 2,767
  • 24
  • 28
Beezy
  • 121
  • 2
  • 5
  • 15

3 Answers3

0

I'd suggest a slightly different approach.

C++ has a number of data structures such as std::set, std::map, and std::vector, that are useful for different scenarios. For example, if you know your values are unique, you could put them all in a std::set, which keeps values ordered in order of size. Then, taking the first two and last two would give you the highest and lowest values. If your values aren't unique, you could put them all in a std::vector, sort the vector, and then do the same thing: take the first two and last two elements.

Here's an example of what I mean, using std::set -- I haven't tested this yet, but it should get you started, if you decide to take this approach.

#import <set>

int main() {

  std::set<double> mySet;

  mySet.insert(0.0);
  mySet.insert(100.0);
  mySet.insert(10.0);
  mySet.insert(90.0);
  mySet.insert(20.0);
  mySet.insert(80.0);

  double min = *(mySet.begin());
  double min2 = *(mySet.begin()+1);
  double max = *(mySet.end()-1);
  double max2 = *(mySet.end()-2);

  return 0;
}

Hope that helps!

sudo make install
  • 5,629
  • 3
  • 36
  • 48
  • Hello! I am actually very new to C++, how would i apply the std::set or std::vector? Will I replace using namespace std; for using namespace std::set? – Beezy Jul 26 '15 at 08:57
  • Also is it possible to achieve the end result with values that aren't unique the way I did it above without using the set or vector? – Beezy Jul 26 '15 at 09:00
  • Hi @Breezy, and welcome to SO! You don't need change your `using namespace std` line--that just means that you can change `set` in my example to `std::set`. `std` is the namespace, and things like `set`, `cout`, and `cin` are all objects within that namespace. Personally, I don't put a `using namespace std` line, and prefix my standard library objects with `std`, just so I remember where they came from. :-) – sudo make install Jul 26 '15 at 09:04
  • Just corrected the import statement--I mistakenly imported `vector` instead of `set`. – sudo make install Jul 26 '15 at 09:06
  • @Breezy. Using STL containers(set, vector) is a nice and easy way to achieve the end objective here. I would ask you if you allow the user to enter duplicate values. If yes, you may want to use a multiset instead of a set since a set is a "Unique Associative Container" which means it discards any duplicates. Bookmark http://www.sgi.com/tech/stl/ and refer it whenever you feel you are need to use STL containers. – Faisal Jul 26 '15 at 12:28
0

I am having issues mainly due to the initialization of variables.

If you looking to initialize your variables to the limits of double (for example min and min2 could be initialized to the max limit of double, and vice versa for max and max2) take a look at this, minimum double value in C/C++.

Community
  • 1
  • 1
techolic
  • 358
  • 3
  • 14
0

Just read all the values into a std:vector<double> then sort that vector.

The first two values will be the two smallest values. The last two will be the two largest.

You will need to decide how to handle duplicate inputs. For example, if the same value is entered more than once.

Peter
  • 35,646
  • 4
  • 32
  • 74