asked 176k views
1 vote
Integer numElements is read from input. The remaining input alternates between integers and strings. Declare integer vector numberList and string vector furnitureList, each with numElements elements. Then, read and store all the integers into numberList and all the strings into furnitureList. Ex: If the input is: 4 22 table 72 bed 93 chair 78 cubby Then the output is: Number: 22, Furniture: table Number: 72, Furniture: bed Number: 93, Furniture: chair Number: 78, Furniture: cubby

1 Answer

3 votes

Final answer:

To solve the problem, declare two C++ vectors for integers and strings. Fill them in a loop based on input, and then output their elements in a formatted manner as per the instructions given in the question.

Step-by-step explanation:

To store integers and strings into separate vectors based on the given numElements input, you can declare two vectors in C++: one for integers and one for strings. Here's how you can do it:

std::vector<int> numberList(numElements);
std::vector<std::string> furnitureList(numElements);

for (int i = 0; i < numElements; ++i) {
std::cin >> numberList[i];
std::cin >> furnitureList[i];
}

After populating both vectors, you can print the elements with a loop:

for (int i = 0; i < numElements; ++i) {
std::cout << "Number: " << numberList[i] << ", Furniture: " << furnitureList[i] << std::endl;
}

Make sure you have the correct input sequence of integers and strings as mentioned. The above code reads numElements integers into numberList and numElements strings into furnitureList, then outputs them in the specified format.

answered
User Jerrylow
by
7.9k points