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...
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...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT