We often have occasion to work with an array that is only partially filled; furt
ID: 3565229 • Letter: W
Question
We often have occasion to work with an array that is only partially filled; furthermore, all the array positions that are used are consecutive, from element 0 onwards. In such a case, the array has a maximum size (the total number of elements that can be stored in the array ), as well as the number of array positions actually being used (this would typically be stored by the programmer in a variable ). This exercise asks you to write a function that, in essence, allows one to insert an element into the filled portion of the array . The new value is inserted, rather than replacing the value already there, thus all values from that position to the end of the array must be slid over, i.e., moved one position to the right: Write the definition for a function, insert , that accepts the following parameters (in this order): an array of integers (filled with 0 or more elements starting at index 0), the number of elements currently in the array , the maxmum number of elements the array can hold, the position at which to insert the value , and the value to be inserted. If the array is full or the position is illegal (too small or too large), the function returns false ; otherwise, the value is inserted at the specified position (moving the rest of the array one position to the right), the number of elements in the array is updated, and the function returns true . Elements may only be inserted within the filled portion of the array or at the position after the last filled element . Thus, if the array has three elements , you can insert at positions 0, 1, 2, 3, and 4.
Explanation / Answer
#include<iostream>
using namespace std;
// Write the definition for a function, insert , that accepts the following parameters (in this order): an array of integers (filled with 0 or more elements starting at index 0), the number of elements currently in the array , the maxmum number of elements the array can hold, the position at which to insert the value , and the value to be inserted.
bool insert(int array[], int& current_elements, int max_elements, int pos_to_insert, int value_to_be_inserted){
// If the array is full or the position is illegal (too small or too large), the function returns false ;
if(current_elements == max_elements || pos_to_insert<0 || pos_to_insert>max_elements) return false;
if(pos_to_insert < current_elements){
for(int i=max_elements-1; i>=pos_to_insert; i--)
array[i+1] = array[i]; //move elements so that array[2] = array[1] ..and so on.
array[pos_to_insert] = value_to_be_inserted;
current_elements++;
}
else{
if(pos_to_insert >= current_elements)
array[current_elements] = value_to_be_inserted;
current_elements++;
}
return true;
// otherwise, the value is inserted at the specified position (moving the rest of the array one position to the right), the number of elements in the array is updated, and the function returns true . Elements may only be inserted within the filled portion of the array or at the position after the last filled element . Thus, if the array has three elements , you can insert at positions 0, 1, 2, 3, and 4.
}
int main(){
int array[10] = {1,4,5};
int filled = 3;
for(int i=0; i<filled; i++)
cout << array[i] << " ";
cout << endl;
//insert 2 at position 1. thus
insert(array, filled, 10, 1, 2);
for(int i=0; i<filled; i++)
cout << array[i] << " ";
cout << endl;
//insert 9 at position 7 or end of the array.
insert(array, filled, 10, 7, 9);
for(int i=0; i<filled; i++)
cout << array[i] << " ";
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.