Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 7.12, Problem 7.31CP
Program Plan Intro
The Standard Template Library (STL) includes the collection of data types and
- A special data type called vector is offered by STL which is useful for standard arrays.
A vector stores the elements or values in sequence order and the values are stored in continuous memory locations.
- The subscript operator “[]” in the vector helps to read the content of each element.
Syntax:
Syntax to define a vector is as follows:
//Declaring an empty vector
vector <data_type> name_of_vector;
In the above line,
- The keyword “vector” represents the vector declaration.
- The “<data_type>” represents the type of data the vector can hold; the data types such as “int”, “float”, “string”, “char”, and “double”.
- The “name_of_vector” represents the name of the vector.
Storing values in a vector:
To store a value in a vector, a special member function called “push_back()” has been used.
Consider the following format to store value in a vector:
//store the value in vector
Name_of_vector.push_back(element_for_insertion);
In the above line,
- The “Name_of_vector” represents the vector name.
- The “Push_back()” is a member function which is used to store values in a vector.
- The “element-for-insertion” is the parameter which holds the value to be stored in the vector.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Assume numbers is a vector that contains these integers:2 4 6 8 10Then the following code executes:numbers. resize (9) :numbers. resize (11, 5) ;What does the vector contain now?O 11 1111 11 11 0 0 0 0 2 4 6 8 10000002468 10 5 502 4 6 8 10 0 0 0 0 11 11 11 11 1105500002468 100246810 000055Assume vec1 is a vector that contains these integers:10 20 30 40 50 60 70 80 90 100And vec2 is a vector that contains these integers3 6 9 12 15Then the following code executes:vecl.resize(7):vecl.swap (vec2) ;What do the vectors contain now?Cvec1 contains 3 6 9 12 15vec2 contains 10 20 30 40 50 60 70vec1 contains 3 6 9 12 15 0 0Ovec2 contains 10 20 30 40 50 60 70vec1 contains 3 6 9 12 15O vec2 contains 50 60 70 80 90 100vec1 contains 0 0 3 6 9 12 15O vec2 contains 50 60 70 80 90 100This code creates a compiler error.
Write a program that lets the user enter the total rainfall for each of 12 months into a vector of doubles. The program will also have a vector of 12 strings to hold the names of the months. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
Part 1 Write main(). In main do the following:(a) Declare a vector of doubles and a vector of strings.Suggested variable name for vector of doubles is rainfall. Suggested variable name for vector of strings is monthNames(b) Store the months in your string vector in the following order: January, February, March, April, May, June, July, August, September, October, November, December. Make sure you write out the month names fully (do not use abbreviations).(c) Get input for 12 doubles from the user to place into the vector of doubles. Each double will be the amount of rain for the month parallel to the string vector of month names.(d) Display the…
Write a program that lets the user enter the total rainfall for each of 12 months into a vector of doubles. The program will also have a vector of 12 strings to hold the names of the months. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
Part 1 Write main(). In main do the following:(a) Declare a vector of doubles and a vector of strings.Suggested variable name for vector of doubles is rainfall. Suggested variable name for vector of strings is monthNames(b) Store the months in your string vector in the following order: January, February, March, April, May, June, July, August, September, October, November, December. Make sure you write out the month names fully (do not use abbreviations).(c) Get input for 12 doubles from the user to place into the vector of doubles. Each double will be the amount of rain for the month parallel to the string vector of month names.(d) Display the…
Chapter 7 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Ch. 7.3 - Define the following arrays: A) empNums, a...Ch. 7.3 - Prob. 7.2CPCh. 7.3 - Prob. 7.3CPCh. 7.3 - Prob. 7.4CPCh. 7.3 - What is array bounds checking? Does C++ perform...Ch. 7.3 - What is the output of the following code? int...Ch. 7.3 - The following program skeleton contains a...Ch. 7.7 - Given the following array definition: int values[]...Ch. 7.7 - Given the following array definition: int nums[5]...Ch. 7.7 - Prob. 7.12CP
Ch. 7.7 - What is the output of the following code? (You may...Ch. 7.8 - Prob. 7.14CPCh. 7.8 - Prob. 7.15CPCh. 7.8 - When used as function arguments, are arrays passed...Ch. 7.8 - What is the output of the following program? (You...Ch. 7.8 - The following program skeleton, when completed,...Ch. 7.10 - Prob. 7.19CPCh. 7.10 - How many elements are in the following array?...Ch. 7.10 - Write a statement that assigns the value 56893.12...Ch. 7.10 - Prob. 7.22CPCh. 7.10 - Prob. 7.23CPCh. 7.10 - Fill in the table below so that it shows the...Ch. 7.10 - Write a function called displayArray7. The...Ch. 7.10 - A video rental store keeps DVDs on 50 racks with...Ch. 7.12 - Prob. 7.27CPCh. 7.12 - Write a definition statement for a vector named...Ch. 7.12 - Prob. 7.29CPCh. 7.12 - Write a definition statement for a vector named...Ch. 7.12 - Prob. 7.31CPCh. 7.12 - snakes is a vector of doubles, with 10 elements....Ch. 7 - Prob. 1RQECh. 7 - Look at the following array definition: int...Ch. 7 - Why should a function that accepts an array as an...Ch. 7 - Prob. 4RQECh. 7 - Prob. 5RQECh. 7 - Prob. 6RQECh. 7 - Prob. 7RQECh. 7 - Assuming that numbers is an array of doubles, will...Ch. 7 - Prob. 9RQECh. 7 - Prob. 10RQECh. 7 - How do you establish a parallel relationship...Ch. 7 - Prob. 12RQECh. 7 - When writing a function that accepts a...Ch. 7 - What advantages does a vector offer over an array?Ch. 7 - Prob. 15RQECh. 7 - The size declarator must be a(n) ________ with a...Ch. 7 - Prob. 17RQECh. 7 - Prob. 18RQECh. 7 - The number inside the brackets of an array...Ch. 7 - C++ has no array ________ checking, which means...Ch. 7 - Starting values for an array may be specified with...Ch. 7 - If an array is partially initialized, the...Ch. 7 - If the size declarator of an array definition is...Ch. 7 - By using the same _________ for multiple arrays,...Ch. 7 - Prob. 25RQECh. 7 - Prob. 26RQECh. 7 - To pass an array to a function, pass the ________...Ch. 7 - A(n) _______ array is like several arrays of the...Ch. 7 - Its best to think of a two-dimensional array as...Ch. 7 - Prob. 30RQECh. 7 - Prob. 31RQECh. 7 - When a two-dimensional array is passed to a...Ch. 7 - The ________________ is a collection of...Ch. 7 - The two types of containers defined by the STL are...Ch. 7 - The vector data type is a(n) ____________...Ch. 7 - Prob. 36RQECh. 7 - To store a value in a vector that docs nor have a...Ch. 7 - To determine the number of elements in a vector,...Ch. 7 - Use the _______________ member function to remove...Ch. 7 - To completely clear the contents of a vector, use...Ch. 7 - Prob. 41RQECh. 7 - Prob. 42RQECh. 7 - In a program, you need to store the identification...Ch. 7 - Prob. 44RQECh. 7 - In a program, you need to store the populations of...Ch. 7 - The following code totals the values in two...Ch. 7 - Prob. 47RQECh. 7 - Prob. 48RQECh. 7 - Prob. 49RQECh. 7 - Prob. 50RQECh. 7 - Prob. 51RQECh. 7 - T F The individual elements of an array are...Ch. 7 - T F The first element in an array is accessed by...Ch. 7 - Prob. 54RQECh. 7 - Prob. 55RQECh. 7 - T F Subscript numbers may be stored in variables.Ch. 7 - T F You can write programs that use invalid...Ch. 7 - Prob. 58RQECh. 7 - T F The values in an initialization list are...Ch. 7 - T F C++ allows you to partially initialize an...Ch. 7 - T F If an array is partially initialized, the...Ch. 7 - T F If you leave an element uninitialized, you do...Ch. 7 - T F If you leave out the size declarator of an...Ch. 7 - T F The uninitialized elements of a string array...Ch. 7 - T F You cannot use the assignment operator to copy...Ch. 7 - Prob. 66RQECh. 7 - T F To pass an array to a function, pass the name...Ch. 7 - T F When defining a parameter variable to hold a...Ch. 7 - T F When an array is passed to a function, the...Ch. 7 - T F A two-dimensional array is like several...Ch. 7 - T F Its best to think of two-dimensional arrays as...Ch. 7 - T F The first size declarator (in the declaration...Ch. 7 - T F Two-dimensional arrays may be passed to...Ch. 7 - T F C++ allows you to create arrays with three or...Ch. 7 - Prob. 75RQECh. 7 - T F To use a vector, you must include the vector...Ch. 7 - T F vectors can report the number of elements they...Ch. 7 - T F You can use the [ ] operator to insert a value...Ch. 7 - T F If you add a value to a vector that is already...Ch. 7 - int sixe; double values [size];Ch. 7 - Prob. 81RQECh. 7 - Prob. 82RQECh. 7 - Prob. 83RQECh. 7 - int numbers[8] ={1,2, , ,4, , 5};Ch. 7 - float ratings[] ;Ch. 7 - Prob. 86RQECh. 7 - Prob. 87RQECh. 7 - Prob. 88RQECh. 7 - void showValues(int nums [4][]) { For (rows = 0;...Ch. 7 - Prob. 90RQECh. 7 - Largest/Smallest Array Values Write a program that...Ch. 7 - Rainfall Statistics Write a program that lets the...Ch. 7 - Chips and Salsa Write a program that lets a maker...Ch. 7 - Larger than n In a program, write a function that...Ch. 7 - Monkey Business A local zoo wants to keep track of...Ch. 7 - Rain or Shine An amateur meteorologist wants to...Ch. 7 - Number Analysis Program Write a program that asks...Ch. 7 - Lo Shu Magic Square The Lo Shu Magic Square is a...Ch. 7 - Payroll Write a program that uses the following...Ch. 7 - Drivers License Exam The local Drivers License...Ch. 7 - Prob. 11PCCh. 7 - Grade Book A teacher has five students who have...Ch. 7 - Lottery Application Write a program that simulates...Ch. 7 - vector Modification Modify the National Commerce...Ch. 7 - World Series Champions If you have downloaded this...Ch. 7 - Name Search If you have downloaded this books...Ch. 7 - Tic-Tac-Toe Game Write a program that allows two...Ch. 7 - 2D Array Operations Write a program that creates a...Ch. 7 - Prob. 20PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Assume numbers is a vector that contains these integers: 2 4 6 8 10 Then the following code executes: numbers.resize (9) ; numbers.resize (11, 5) ; What does the vector contain now? 11 11 11 11 11 0 0 0 0 2 4 6 8 10 0 0 0 0 2 4 6 8 10 5 5 2 4 6 8 10 0 0 0 0 11 11 11 11 11 5 5 0 0 0 0 2 4 6 8 10 O 2 4 6 8 10 0 0 0 0 5 5 Assume vec1 is a vector that contains these integers: 10 20 30 40 50 60 70 80 90 100 And vec2 is a vector that contains these integers: 3 6 9 12 15 Then the following code executes: vec1.resize (7); vec1.swap (vec2) ; What do the vectors contain now? vec1 contains 3 6 9 12 15 vec2 contains 10 20 30 40 50 60 70 vec1 contains 3 6 9 12 15 0 0 vec2 contains 10 20 30 40 50 60 70 vec1 contains 3 6 9 12 15 vec2 contains 50 60 70 80 90 100 vec1 contains 0 0 3 6 9 12 15 vec2 contains 50 60 70 80 90 100 O This code creates a compiler error.arrow_forward: Write down a code which detects the even numbers in the row vector containing the digits of your student number in order and converts even numbers to odd numbers and displays the modified output vector.arrow_forwardWrite in c++ pleasearrow_forward
- True or False: The initial loop is more efficient than its vecotized equivalent?arrow_forwardsame) with eleven elements that are all 4. 8. Create a vector, name it Afirst, that has 13 elements in which the first is 3, the increment is 4 and the last element is 51. Then, using the colon symbol, create a new vector, call it Asecond, that has seven elements. The first four elements are the first four elements of the vector Afirst, and the last three are the last three elements of the vector Afirst. Using the colon symbol, create a 3x5 matrix (assign it to a variable namedarrow_forwardDetect Hacker Given an array of positive integers that stores the runs scored by players of a team in a mobile game. Write a C++ program that returns true if the score of any player is greater than or equal to 250. If all the scores are less than 250 then return false. bool detectHack(vector&runs){ //write your code here }arrow_forward
- C++ Program Create a program that does the following: 1. Loads a vector with at least 60 randomly generated integers in the range of 0 - 49. Please see the attached for an example. 2. Create method to display the values in the vector. 8th Ed. Textbook - use an Iterator as described on page 1010. 9th Ed. Textbook - use an Iterator as described on page 1045. 3. Use the following STL templates and display the vector contents for each template used. sort() random_shuffle() Display the result of max_element() Display the result of min_element() Version 9 of the textbook does not include these algorithms. Refer to http://www.cplusplus.com/reference/algorithm/ for more examples. Deliverable is a working CPP program.arrow_forward4arrow_forwardString [] plants = newstring [] {“onions”, “carrots”, “peas”, “tomatoes”, “bananas”, “lemons”}; Using the array above , write code that 1. Prints the elements of the array in the order they appear in the array using a lambda function 2. Prints the elements of the array in descending order using a Linq function 3. Prints the elements of the array in ascending order using a linq fucntion 4. Prints the elements of the array that contain “ba” using a linq functionarrow_forward
- This one. in c++arrow_forwardVectors of string given below for code. THUMBS DOWN FOR INCORRECT.arrow_forwardC++ This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings in another int vector. Output these vectors (i.e., output the roster). Ex: Enter player 1's jersey number: 84 Enter player 1's rating: 7 Enter player 2's jersey number: 23 Enter player 2's rating: 4 Enter player 3's jersey number: 4 Enter player 3's rating: 5 Enter player 4's jersey number: 30 Enter player 4's rating: 2 Enter player 5's jersey number: 66 Enter player 5's rating: 9 ROSTER Player 1 -- Jersey number: 84, Rating: 7 Player 2 -- Jersey number: 23, Rating: 4 ... (2) Implement a menu of options for a user to modify the roster. Each option is represented by a single character. Following the initial 5 players' input and roster output, the program outputs the menu.…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education