C++ Programming involving Loops, Functions, and Arrays.(Please answer all four p
ID: 3886153 • Letter: C
Question
C++ Programming involving Loops, Functions, and Arrays.(Please answer all four problems) Thank you!
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.
Explanation / Answer
#include<iostream>
using namespace std;
bool insert(int value, int intArray[], int &numberOfValidEnties, int size){
if (numberOfValidEnties < size){
intArray[numberOfValidEnties] = value;
numberOfValidEnties++;
return true;
}
return false;
}
bool delete1(int value, int intArray[], int &numberOfValidEnties){
for (int i = 0; i<numberOfValidEnties; i++){
if (intArray[i] == value){
for (int j = i; j<numberOfValidEnties-1; j++){
intArray[j] = intArray[j+1];
}
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 choice;
int n;
int data[10];
int num_entries = 0;;
do {
cout << "1.Insert ";
cout << "2.Delete ";
cout << "3.Print ";
cout << "4.Quit ";
cout << "Enter your choice : ";
cin >> choice;
switch(choice) {
case 1:
cout << "Enter number to be inserted :";
cin >> n;
if (insert(n, data, num_entries,10))
cout << "Inserted"<<endl;
else
cout << "Inserted failed"<<endl;
break;
case 2:
cout << "Enter number to be deleted :";
cin >> n;
if (delete1(n, data, num_entries))
cout << "deleted"<<endl;
else
cout << "deletetion failed"<<endl;
break;
case 3:
print(data,num_entries);
break;
}
} while (choice != 4);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.