64.8k views
2 votes
Integers numEmployees, firstEmployee, middleEmployee, and lastEmployee are read from input. First, declare a vector of integers named runningNumbers with a size of numEmployees. Then, initialize the first, middle, and last element in runningNumbers to firstEmployee, middleEmployee, and lastEmployee, respectively.

Ex: If the input is 10 186 173 180, then the output is:

186 0 0 0 0 173 0 0 0 180

#include
#include
using namespace std;

int main() {
int numEmployees;
int firstEmployee;
int middleEmployee;
int lastEmployee;
unsigned int i;

cin >> numEmployees;
cin >> firstEmployee;
cin >> middleEmployee;
cin >> lastEmployee;

/* Your code goes here */

for (i = 0; i < runningNumbers.size(); ++i) {
cout << runningNumbers.at(i) << " ";
}
cout << endl;

return 0;
}

C++ please

User Szczepanpp
by
7.5k points

1 Answer

4 votes

Final answer:

The task requires adding code to a C++ program to initialize specific elements of a vector named runningNumbers. The required code sets the first, middle, and last elements of the vector based on input values, filling other positions with zeroes.

Step-by-step explanation:

The subject matter involves writing a piece of C++ code. Specifically, the task is to define a vector and initialize its first, middle, and last elements with specific values provided from input. Here's how you can modify the provided code snippet to accomplish this task:

vector runningNumbers(numEmployees, 0); // Initializes all elements to 0
runningNumbers.at(0) = firstEmployee; // Sets the first element
unningNumbers.at((numEmployees - 1) / 2) = middleEmployee; // Sets the middle element
runningNumbers.at(numEmployees - 1) = lastEmployee; // Sets the last element

After this modification, the program reads the input values, initializes the vector accordingly, and then prints out each element of the vector runningNumbers in sequence, resulting in the desired output.

User DanielBarbarian
by
8.3k points