Solutions

Minimum Value Algorithm

Exercise 1:

#include <iostream>
using namespace std;
 
int main() 
{ 
    double minimum;
    double position = 1;
    double inputValue;    
    cout << "Please type in 7 numbers: " << endl;
    cin >> minimum;
    for (double i=2;i<=7;i++)
        {
        cin >> inputValue;
        if (inputValue < minimum) 
            {
            minimum = inputValue;
            position = i;
            }
        }
    cout << "The minimum is: " << minimum << "at position: " 
         << position << endl;
}

Exercise 2:

#include <iostream>
#include <vector>
using namespace std;

int main()
{                
    double inputValue;
    cout << "Please type in 7 numbers: " << endl;
    cin >> inputValue;
    double min = inputValue;
    double max = inputValue;    
    for (double i=2; i<=7; i++)
        {
        cin >> inputValue;
        if (inputValue < min)
            min = inputValue;        
        if (inputValue > max)
            max = inputValue;
        }
    cout << "The smallest value: " << min << endl;
    cout << "The greatest value: " << max << endl;            
}

Back to exercises