n program that inserts an element IT ar Array) for the following data. (Siz 1000,2000,5000,4000
Here is the documented code to solve this assignment:
chegg.cpp:
#include <iostream>
#define SIZE 10
void show(const int (&A)[SIZE],
const int N) {
for(int i = 0; i < N; ++i) {
std::cout << A[i] << ", ";
}
std::cout << "\b\b " << std::endl;
}
int Find_Location_To_Insert(const int (&A)[SIZE],
const int N,
int &K,
const int ITEM) {
int LB = 0, UB = N - 1;
for(K = LB; K <= UB; ++K) {
if(A[K] > ITEM) {
// insert at front or in middle
return K;
}
}
// insert at the end of array
return K;
}
void INSERT(int (&A)[SIZE],
int &N,
int K,
const int ITEM) {
// initialize counter
int J = N - 1;
while(J >= K) {
// move the Jth element downward
A[J + 1] = A[J];
// decrease counter
J = J - 1;
}
// insert element
A[K] = ITEM;
// reset N
N = N + 1;
}
int main() {
int A[SIZE];
int N = 0;
int ITEM;
int K;
while(true) {
show(A, N);
// overflow check
if(N == SIZE) {
std::cout << "Overflow, Array is Full." << std::endl;
break;
}
Step by step
Solved in 3 steps with 1 images