Question

In: Computer Science

In C++, create two vectors, one a vector of ints and one a vector of strings....

In C++, create two vectors, one a vector of ints and one a vector of strings. The user will fill each one with data, and then your program will display the values.

  1. Copy the template file /home/cs165new/check10a.cpp to your working directory.
  2. For this assignment, in order to focus our practice on the syntax of the vectors, you can put all of your code in main.
  3. Create and display a vector of ints as follows:
    1. Create a vector of ints.
    2. Prompt the user for ints until they enter 0, and store them in your vector.
    3. Loop through all the numbers in the vector and display each one.
    4. When you loop through, make sure to use the size of your vector in your condition (not a separate integer from above).
  4. Create and display a vector of strings as follows:
    1. Create a vector of strings.
    2. Prompt the user for strings until they enter "quit", and store them in your vector.
    3. Loop through all the strings in the vector and display each one.
    4. When you loop through, make sure to use the size of your vector in your condition (not a separate integer from above).
  5. You do not have to use iterators for this assignment (but you are welcome to if you would like). It can be done using the [] operator.

Sample Output

The following is an example of output for this program:

Enter int: 3

Enter int: 12

Enter int: 4

Enter int: 0

Your list is:

3

12

4

Enter string: The

Enter string: quick

Enter string: brown

Enter string: fox

Enter string: jumps

Enter string: over

Enter string: the

Enter string: lazy

Enter string: dog

Enter string: quit

Your list is:

The

quick

brown

fox

jumps

over

the

lazy

dog

Solutions

Expert Solution

#include <iostream>

#include <vector>

using namespace std;

int main()

{

vector<int> nums;

vector<string> words;

int inpNum;

string inpWord;

cout << endl;

do

{

cout << "Enter int: ";

cin >> inpNum;

if(inpNum == 0)

break;

else

nums.push_back(inpNum);

}while(inpNum != 0);

// display all the integers

cout << "Your list is:\n";

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

{

cout << nums[i] << endl;

}

cout << endl;

do

{

cout << "Enter string: ";

cin >> inpWord;

if(inpWord.compare("quit") == 0)

break;

else

words.push_back(inpWord);

}while(inpWord.compare("quit") != 0);

// display all the strings

cout << "Your list is:\n";

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

{

cout << words[i] << endl;

}

cout << endl;

return 0;

}

****************************************************************** SCREENSHOT *********************************************************


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
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
C++ Suppose the vector v = [4 -6 7]. Create three vectors: 1. p, which is...
C++ Suppose the vector v = [4 -6 7]. Create three vectors: 1. p, which is twice as long as v and points in the same direction as v 2. q, which has the same length as v and points in the opposite direction of v 3. r, which is three quarters the length of v and points in the same direction as v Print out the results of each vector calculation.
A) Create two vectors in R3 ( 3D vectors) such that they are orthogonal. b) Using...
A) Create two vectors in R3 ( 3D vectors) such that they are orthogonal. b) Using the two vectors from a) above, determine the cross product of the two vectors c)Is it possible to write the vector [0,0,1] using scalar multiples of these vectors?
1.The Vector Product or otherwise know Cross Product of two vectors is itself another vector. When...
1.The Vector Product or otherwise know Cross Product of two vectors is itself another vector. When is the product zero? 2.How does one find the resultant vector of multiple vectors added together? 3.The change in displacement divided by the time interval over which the change in displacement occurs is known as what? 4.The change in velocity divided by the time interval in which the change in velocity occurs is known as what? 5.The constant acceleration of a freely falling object...
C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like...
C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like this alphabetically? vector words {"September", "test" "seven", "apple"} The output should be: "apple, seven, September, test"
Let a and b be non-parallel vectors (algebraically a1b2 −a2b1 /= 0). For a vector c...
Let a and b be non-parallel vectors (algebraically a1b2 −a2b1 /= 0). For a vector c there are unique λ, µ real numbers such that c = λ· a+µ·b. proof?
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
using c language: Add two vectors of doubles. Name this function vect_add(). Subtract two vectors of...
using c language: Add two vectors of doubles. Name this function vect_add(). Subtract two vectors of doubles. Name this function vect_sub(). Multiplies element by element two vectors. Name this function vect_prod(). Compute and return the dot product of two vectors. Name this function vect_dot_prod(). The dot product operation is the result of taking two vectors(single dimension arrays) and multiplying corresponding element by element and then adding up the sum of all the products. For example, if I have 2 vectors...
C++ Create a vector of student records (minimum 15+ records) which is unsorted (For Vector, refer...
C++ Create a vector of student records (minimum 15+ records) which is unsorted (For Vector, refer to Chapter 7.11 or 17.3, or you may use Array of structures) - You should read student records from a file into the vector in C++ or in Java program. - Display the unsorted 10+ student records (entire record, not just ID) and associated test scores. - Each Student record should include Student ID, Name, GPA, Student Address, and a pointer which points to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT