Please provide the answers for #1(part (a) and (b) and #2 part (a) and (b) 1. 1.
ID: 3623684 • Letter: P
Question
Please provide the answers for #1(part (a) and (b) and #2 part (a) and (b)
1. 1. The function insert does not work quite the way it is supposed to.
(a) Step through the code shown below and write down the output of the current
version of the program.
#include <iostream>
using namespace std;
int Insert(int [], int, int, int);
const int SIZE = 10;
int main()
{
int array[SIZE], x=0, position=1, aSize=4;
// array[0]=1, array[1]=3, array[2]=5, array[3]=7
for (int i=0; i<aSize; i++)
array[i] = 2*i+1;
// insert value 0 in position 1 (x is 0, position is 1)
aSize = Insert(array, x, position, aSize);
// display values after the insertion
for (int i=0; i<aSize; i++)
cout << array[i] << " ";
cout << endl;
return 0;
}
// this function inserts “element” in the given “position” in array “arr”.
//It returns the new array size
int Insert(int arr[], int element, int position, int size)
{
for (int i=position; i<size; i++)
arr[i+1] = arr[i];
arr[position]= element;
++size;
return size;
}
(b) How would you modify the for loop in function Insert to correctly insert an
element into the array at position "position"? For example, before insertion, array
looks like this: 5 7 9 2. After the insertion of element 6 at position 2, array looks
like this: 5 7 6 9 2
Show program output here:
2
2. (a) In this program, function Delete does not work quite the way it is supposed to. Step
through the code shown below and write down the output of the current version of the
program.
#include <iostream>
using namespace std;
int Delete(int [], int, int);
int main()
{
int values[10]={4, 16, 3, 8, 20};
int size=5;
int k;
int position = 1;
// Delete the item at position in the array values
size = Delete(values, position, size);
for (k=0; k<size; k++)
cout << values[k] << " ";
cout << endl;
return 0;
}
// This function deletes the element at location “position” in array A.
// If the position is < 0 or greater than the size of the array, nothing will be done.
// Otherwise, the value is deleted, and all the values below will be shifted up one position.
// It returns the new array size.
int Delete(int array[], int position, int size)
{
for (int k=size; k>=position; k--)
array[k] = array[k-1];
size--;
return (size);
}
(b) Modify function Delete to correctly delete an element at position "position" from the array.
For example, before deletion, the content of the array looks like this: 5 7 9 2
After the deletion of element at position 1, the content of the array looks like this: 5 9 2
Explanation / Answer
please rate - thanks
1a)
#include <iostream>
using namespace std;
int Insert(int [], int, int, int);
const int SIZE = 10;
int main()
{
int array[SIZE], x=0, position=1, aSize=4;
// array[0]=1, array[1]=3, array[2]=5, array[3]=7
for (int i=0; i<aSize; i++)
array[i] = 2*i+1;
// insert value 0 in position 1 (x is 0, position is 1)
aSize = Insert(array, x, position, aSize);
// display values after the insertion
for (int i=0; i<aSize; i++)
cout << array[i] << " ";
cout << endl;
return 0;
}
// this function inserts “element” in the given “position” in array “arr”.
//It returns the new array size
int Insert(int arr[], int element, int position, int size)
{
for (int i=position; i<size; i++) //i goes from 1 to 4
arr[i+1] = arr[i]; //array 2 = array 1 array 2=array 2 duplicating the data
arr[position]= element; // so the array you get is 1 0 3 3 3
++size;
return size;
}
correct code
1b)
#include <iostream>
using namespace std;
int Insert(int [], int, int, int);
const int SIZE = 10;
int main()
{
int array[SIZE], x=0, position=1, aSize=4;
// array[0]=1, array[1]=3, array[2]=5, array[3]=7
for (int i=0; i<aSize; i++)
array[i] = 2*i+1;
// insert value 0 in position 1 (x is 0, position is 1)
aSize = Insert(array, x, position, aSize);
// display values after the insertion
for (int i=0; i<aSize; i++)
cout << array[i] << " ";
cout << endl;
system("pause");
return 0;
}
// this function inserts “element” in the given “position” in array “arr”.
//It returns the new array size
int Insert(int arr[], int element, int position, int size)
{
for (int i=size ; i>=position; i--)
arr[i] = arr[i-1];
arr[position]= element;
++size;
return size;
}
2a
#include <iostream>
using namespace std;
int Delete(int [], int, int);
int main()
{
int values[10]={4, 16, 3, 8, 20};
int size=5;
int k;
int position = 1;
// Delete the item at position in the array values
size = Delete(values, position, size);
for (k=0; k<size; k++)
cout << values[k] << " ";
cout << endl;
return 0;
}
// This function deletes the element at location “position” in array A.
// If the position is < 0 or greater than the size of the array, nothing will be done.
// Otherwise, the value is deleted, and all the values below will be shifted up one position.
// It returns the new array size.
int Delete(int array[], int position, int size)
{
for (int k=size; k>=position; k--) //loop goes from 5 to 1
array[k] = array[k-1]; // a[5]=a[4] a[4]=a[3] shifting the wrong direction
size--; //giving the output 4 4 16 3
return (size);
}
correction 2b)
#include <iostream>
using namespace std;
int Delete(int [], int, int);
int main()
{
int values[10]={4, 16, 3, 8, 20};
int size=5;
int k;
int position = 1;
// Delete the item at position in the array values
size = Delete(values, position, size);
for (k=0; k<size; k++)
cout << values[k] << " ";
cout << endl;
system("pause");
return 0;
}
// This function deletes the element at location “position” in array A.
// If the position is < 0 or greater than the size of the array, nothing will be done.
// Otherwise, the value is deleted, and all the values below will be shifted up one position.
// It returns the new array size.
int Delete(int array[], int position, int size)
{
for (int k=position; k<size; k++)
array[k] = array[k+1];
size--;
return (size);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.