Showing posts with label popback. Show all posts
Showing posts with label popback. Show all posts

Friday, June 1, 2018

Total Grade Using Your Own Inputs

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

int main()
{
    vector <double> data;

    double input, input2;
    double total = 0, total2 = 0;

    cout << "\aEnter your score for each quiz, tests and/or homework (enter 0 to end): " << endl;

    do{
        cin >> input;
        data.push_back (input);

        total += input;

    } while (input != 0); // once input is 0, loop ends

    cout << "\aEnter max score for each quiz, tests and/or homework (enter 0 to end): " << endl;

    do{
        cin >> input2;
        data.push_back (input2);

        total2 += input2;

    } while (input2 != 0);

    data.pop_back();    // prevents counting 0, removes 1 array
                        // since both do loops uses data, the pop_back applies to both. If you want to use a different pop_back
                        // then you will need to change the variable, in this case: data

 //   int dataSize = int(data.size());
/* if and else to prevent dividing by 0 */
    if (total2 == 0)
        cout << "Undefined!" << endl;
    else
    cout << "Your grade is: " << (total/total2)*100 << "%" << endl;

}

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

}