Question

In: Computer Science

Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...

Write a  program in C++ using a vector to create the following output.

  • Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values.

Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10

vector capacity (array size)

changes and is dependent on

the compiler.

The size of the list is now 5.

The vector capacity (array size) is 6.

The back element is: 10

The front element is: 2

Now deleting the value at the end of the list . . .

The size of the list is now 4.

Note:  The swap() function is defined in the <algorithm> header, and is not a vector function.  No object calls the swap() function.  Do it like this:

swap(numbers.first(), numbers.last());

After deleting, here is the list:   2, 4, 6, 8

Now swapping the first number with the last number.

After swapping, here is the list:   8, 4, 6, 2   

Now inserting 0 at the beginning of the list.

After inserting, here is the list:   0, 8, 4, 6, 8

The size of the list is now 5.

The value of the third element (index 2) is:  4

Now removing all list items . . .

The size of the list is now 0.

The vector capacity (array size) is 6.      

Solutions

Expert Solution

#include<bits/stdc++.h>// <bits/stdc++.h> header file includes all the headers required such as vector, algorithm, etc
using namespace std;

int main(){
vector<int> numbers;
numbers.push_back(2);
numbers.push_back(4);
numbers.push_back(6);
numbers.push_back(8);
numbers.push_back(10);
cout<<"Size of list:- "<<numbers.size()<<endl;
cout<<"Values of list are:- \n";
for(int i=0;i<numbers.size();i++)
cout<<numbers[i]<<" ";
cout<<endl;
cout<<"Back element in list:- "<<numbers[numbers.size()-1]<<endl;
cout<<"Front element in list:- "<<numbers[0]<<endl;
numbers.pop_back();
cout<<"After deleting back element from list, size of the list is:- "<<numbers.size()<<endl;
cout<<"List elements after deleting back element are:- \n";
for(int i=0;i<numbers.size();i++){
cout<<numbers[i]<<" ";
}
cout<<endl;
cout<<"Swapping first and last element of the list:- \n";
swap(numbers[0], numbers[numbers.size()-1]);
cout<<endl;
cout<<"After swapping first and last element, list elements are:- \n";
for(int i=0;i<numbers.size();i++){
cout<<numbers[i]<<" ";
}
cout<<endl;
cout<<"Inserting 0 at the begining of the list:- \n";
auto it = numbers.insert(numbers.begin(), 0);
cout<<"After inserting, list elements are:- \n";
for(int i=0;i<numbers.size();i++){
cout<<numbers[i]<<" ";
}
cout<<endl;
cout<<"Size of list:- "<<numbers.size()<<endl;
cout<<"The value of the third element (index 2) is:- "<<numbers[2]<<endl;
  
cout<<"Now removing all list items . . ."<<endl;
numbers.clear();
cout<<"Size of list:- "<<numbers.size()<<endl;
return 0;
}
Note: Capacity of list is compiler dependent, it is not necessarily equal to the vector size. It can be equal to or greater, with the extra space

Please refer to the screenshot of the code to understand the indentation of the code.

Please refer to the screenshot of the Output to understand in a better way.


Related Solutions

C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
In C++, Write the following program using a vector: A common game is the lottery card....
In C++, Write the following program using a vector: A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
using C# Write a program named RectangleArea tocalculate the area of a rectangle with a...
using C# Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is theRectangleArea holding the Main(), and the other one is Rectangle. The Rectangleclass has three members: a property of length, a property of width, and a method with a name of CalArea() to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT