Question
The goal is to insert two integers into an int array: #include using namespace std; void insertItem(int list[], int& count, int val, int pos); int
The goal is to insert two integers into an int array:
#include
void insertItem(int list[], int& count, int val, int pos);
int main() { const int NUM_VALS = 10; int list[NUM_VALS] = { 7, 12, 34, 15, 9, 10, 3, 0, 0, 0}; int i = 0, count = 7; cout
void insertItem(int list[], int& count, int val, int pos) { // THE SOLUTION WILL BE IN THIS FUNCTION
}
The method they want us to use is:I tried doing this method several times but cannot get the correct output.
The output should be:
"List before inserts: 7 12 34 15 9 10 3
List after 2 inserts: 3 4 7 12 34 15 9 10 3"
Here is my code:
#include
void insertItem(int list[], int& count, int val, int pos);
int main() { const int NUM_VALS = 10; int list[NUM_VALS] = { 7, 12, 34, 15, 9, 10, 3, 0, 0, 0}; int i = 0, count = 7; cout
void insertItem(int list[], int& count, int val, int pos) { int i; /* Your solution goes here */ /*Use a for loop to shift items to insert the item in the right position*/ int newIndex = 0; for(i = count; i
count++;
Here is what it is outputting:
The question is what should I do with my code to get the correct output
Table 11.12.6: Shifting algorithm to insert a value. Using a for loop, initialize the loop index to count and loop backwards to the newlndex +1 1. copy the element at position [index - 1] to [index] - decrement index 2. Write the new value into the newlndex 3. Increment count so count now reflects the new size of the array sh -c make -s List before inserts: 7123415910 main List after 2 inserts: 34341591030004850161Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started