25.7k views
2 votes
Run the program and observe the output to be: 55 4 250 19. Modify the numsInsert function to insert each item in sorted order. The new program should output: 4 19 55 250.

#include
#include
using namespace std;
void numsInsert(vector& numsList, int newNum) {
unsigned int i;
for (i = 0; i < numsList.size(); ++i) {
if (newNum < numsList.at(i)) {
// FIXME: insert newNum at element i
break; // Exits the for loop
}
}
// FIXME: change so executes if higher number NOT found
// Change "true" to "i == ??" (determine what ?? should be)
if (true) { // No higher number was found, so append to end
numsList.push_back(newNum);
}
}
void numsPrint(const vector& numsList) {
unsigned int i;
for (i = 0; i < numsList.size(); ++i) {
cout << " " << numsList.at(i) << endl;
}
}

int main() {
vector numsList;
numsInsert(numsList, 55);
numsInsert(numsList, 4);
numsInsert(numsList, 250);
numsInsert(numsList, 19);
numsPrint (numsList);

return 0;
}

User JerKimball
by
4.1k points

1 Answer

3 votes

Answer:

#include <iostream>

#include <vector>

using namespace std;

void numsInsert(vector<int>& numsList, int newNum) {

unsigned int i;

for (i = 0; i < numsList.size(); ++i) {

if (newNum < numsList.at(i)) {

// FIXME: insert newNum at element i

numsList.insert(numsList.begin() + i, newNum);

break; // Exits the for loop

}

}

// FIXME: change so executes if higher number NOT found

// Change "true" to "i == ??" (determine what ?? should be)

if (i == numsList.size()) { // No higher number was found, so append to end

numsList.push_back(newNum);

}

}

void numsPrint(const vector<int>& numsList) {

unsigned int i;

for (i = 0; i < numsList.size(); ++i) {

cout << " " << numsList.at(i) << endl;

}

}

int main() {

vector<int> numsList;

numsInsert(numsList, 55);

numsInsert(numsList, 4);

numsInsert(numsList, 250);

numsInsert(numsList, 19);

numsPrint(numsList);

return 0;

}

Step-by-step explanation:

User Liz Barrett
by
5.2k points