Question

Write a program that uses a vector to store a set of integers. The program outputs...

Write a program that uses a vector to store a set of integers. The program outputs the smallest, largest, and average of the numbers. When declaring the numbers do not specify its size. Use the function push_back to insert elements into the array. You will have to declare the size of the vector somewhere in your program.

*Note: I need help to write out this problem in C++ while using Visual Studio 2017

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code to copy:

//Include required header files.

// Only use stdafx.h in visual studio

#include "stdafx.h"

#include <iostream>

#include <vector>

using namespace std;

int main() {

    // Initialize vector named programVec.

    vector<int> programVec;

    // inserting integers into vetor

    programVec.push_back(20);

    programVec.push_back(30);

    programVec.push_back(40);

    programVec.push_back(50);

    programVec.push_back(60);

    // declare required varables

    int min = programVec[0], max = programVec[0], sum = 0;

    for (int i = 0; i < programVec.size(); ++i) {

        if (programVec[i] > max) max = programVec[i];

        if (programVec[i] < min) min = programVec[i];

        sum += programVec[i];

    }

    double avg = sum/(double)programVec.size();

   

    // Display required outputs.

    cout << "The smallest number is : " << min << endl;

    cout << "The largest number is : " << max << endl;

    cout << "The average is : " << avg << endl;

    cout << "The size of Vector is " << programVec.size() << endl;

    // system("pause"); can only use in visual studio.

    system("pause");

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a program that uses a vector to store a set of integers. The program outputs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++: Write a program that reads a list of integers, and outputs the two smallest...

    In C++: Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The list is preceded by an integer indicating how many numbers are in the list. If the input is: 5 10 5 3 21 2, the output is: 2 3 To achieve the above, first read the integers into a vector.

  • LAB: How many negative numbers Write a program that generates a list of integers, and outputs...

    LAB: How many negative numbers Write a program that generates a list of integers, and outputs those integers, and then prints the number of negative elements. Create a method (static void fillArray(int [] array)) that uses Random to generate 50 values (Random.nextInt()), put each value in the array (make sure to pass the array in to this method as an argument). Write another method (static int printAndCountNeg(int [] array)) that prints each element’s value and count the number of negative...

  • Can someone please help? Thanks in advance! Simple Vector Modification C++ Your program Assignment Modify the...

    Can someone please help? Thanks in advance! Simple Vector Modification C++ Your program Assignment Modify the Simple Vector class template presented in this Chapter 16-11) to include the member functions push_back and pop_back. First the User will enter the numbers that are to be in the array. The push_back function should accept an argument and insert its value at the end of the array that has been created. The pop_back function should accept no argument and remove the last element...

  • 2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum...

    2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum of both of the integers and all the numbers in between Write a Marie program that determines the largest of a series of positive integers provided by a user. The user will enter a -1 when she is finished. Up to 10 numbers will be provided by the user. Write a Marie program that accepts two positive integers, multiples them by repeated addition, and...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • Please help C++ language Instructions Write a program that prompts the user to enter 50 integers...

    Please help C++ language Instructions Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs.

  • in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should...

    in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range 0 through 9 for each element in the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding element in the two arrays and keep a count of the digits that match. For...

  • in java Write a program that uses recursion to find the largest number in an array....

    in java Write a program that uses recursion to find the largest number in an array. Declare and initialize an array of 10 different numbers.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT