Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ 1. Write a function with the following prototype: bool insert (int value, in

ID: 3885310 • Letter: C

Question

c++

1. Write a function with the following prototype:

bool insert (int value, int intArray[ ], int & numberOfValidEnties, int size);

Precondition is that the first numberOfValidEntries values in intArray (length given by size) are in sorted order. If numberOfValidEntries is 0 or 1, obviously, there is no ordering, but if numberOfValidEntries is greater than 1, the valid numbers in the array in sorted order from smallest to largest.

If there is still room in the array, the function will insert value so that the sorted order is preserved, increment numberOfValidEntries by 1, and return true. If it is not possible to add another value into the array, the array and numberOfValidEntries will remain unchanged. and the function will return false.

2. Write a function with the following prototype:

bool delete (int value, int intArray[ ], int & numberOfValidEnties);

The precondition is the same as for the insert function.

The delete function will look for value in the array, and if found, it will delete it by moving all valid entries below it up by one, decrement numberOfValidEnties, and return true. If the value is not found, it returns false and the array and numberOfValidEntries remain unchanged.

3. Write a function called print with 2 parameters, an integer array and the number of valid entries in the array, and prints out the valid entries

4. Write a main program that starts out with an integer array of size 10 with no valid entries. Write an interactive menu in which the user can keep choose to insert, delete, or print until the choice to quit is chosen.

Use your program to thoroughly test your functions.

Explanation / Answer

#include<iostream>

using namespace std;

bool insert (int value, int intArray[ ], int & numberOfValidEnties, int size){

if(numberOfValidEnties >=size){

return false;

}

if(numberOfValidEnties == 0){

intArray[numberOfValidEnties] = value;

numberOfValidEnties++;

return true;

}

int i=0;

while((value > intArray[i]) && i<numberOfValidEnties){

i++;

}

if(i == numberOfValidEnties){

intArray[i] = value;

numberOfValidEnties++;

return true;

}

if(i==size){

return false;

}

for(int j=numberOfValidEnties; j>=i; j--){

intArray[j+1] = intArray[j];

}

intArray[i] = value;

numberOfValidEnties++;

return true;

}

bool deleteElement (int value, int intArray[ ], int & numberOfValidEnties){

bool flag = false;

int i;

for(i=0; i<numberOfValidEnties;i++){

if(intArray[i]==value){

flag = true;

break; // found at index i

}

}

if(flag){

for(int j=numberOfValidEnties-1;j>i;j--){

intArray[j-1] = intArray[j];

}

numberOfValidEnties--;

return true;

}

return false;

}

void print(int intArray[ ], int & numberOfValidEnties){

for(int i=0; i<numberOfValidEnties;i++){

cout<<intArray[i]<<" ";

}

cout<<endl;

}

int main(){

int array[10]; // Initial Size as 10

int validEnties = 0; // Initially 0 valid entries

insert(5,array,validEnties,10); // Inserting 5

insert(2,array,validEnties,10); // Insert 2

insert(9,array,validEnties,10); //insert 9

insert(7,array,validEnties,10); //insert 7

cout<<"After Insertion "<<endl;

print(array,validEnties);

cout<<"Deleting 7 "<<endl;

deleteElement(7,array,validEnties);

print(array,validEnties);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote