Thursday, December 28, 2017

Write a program that reads in a sequence of numbers until it reads in a value zero. The program needs to write out the smallest value in the input sequence, with the exception of the last element equal to zero.

#include <iostream>
#include <vector>

int main()

{
    std::vector<double> data;   // std:: added & no value was added to the element, e.g. data(#)

    double input;               // variable defined

    do {

    std::cin >> input;          // your input value

    data.push_back(input);      // Adds a new element at the end of the vector, after its current last element;
                                // format: (element).push_back(variable name)

    } while (input != 0);       /* while it's not equal to 0 */

    data.pop_back();            // Removes the last element in the vector, effectively reducing the container size by one
                                // and prevents reading the terminating 0


        int dataSize = int(data.size()); // number of elements of the array data

        double min = data[0];

            for (int i = 1; i < dataSize; i++)
            {
                if (data[i] < min)
                {
                    min = data[i];
                }
            }

    if (data[0] == 0)
    {
        std::cout << "List is empty, terminating..." << std::endl;
    }

    else
        std::cout << "The minimum is: " << min << std::endl;    // must enter 0 for the program to end

}

No comments:

Post a Comment