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

Complete the project Insertlnorder so that the user can enter positive integers

ID: 3834842 • Letter: C

Question

Complete the project Insertlnorder so that the user can enter positive integers to be stored in ascending order in an array of capacity 5. The project already contains code to declare an array and insert the first number. Once the first number is inserted a while loop start to iterate until the array is full. The loop will insert the elements in the array and print the array after each insertion. Once the array is full, a message will be displayed to end the program. Your job is to complete the while loop. Pseudocode Ask the user which number to insert and store the number in a variable. IF the number to insert is larger than the last number in the array Insert the number at the end of the array and increment the number of elements. WHILE the location is not found and the end of the array has not been reached IF the number to insert is smaller or equal the element at this index Use a FOR loop to shift elements Insert the element increment number of elements ELSE Move forward How do you insert an element in the array? You shift elements to the left to "make space for the new number. If you want to insert 34, you should insert it at index 2. Starting from the end of the list, shift each element to its right. Overwrite element at index 2 with element to insert. Test your program by inserting: A number smaller than the first element A number larger than the last element A number in between the first and last element A number already in the array

Explanation / Answer

//Program to introduce an array and store the elements in ascending order. Then for inserting an element, sorting is performed to insert the element at proper location in ascending order.

#include<iostream>
using namespace std;
void main()
{
    int a[20],n,x,i,pos=0;
    cout<<"Enter size of array:";
    cin>>n;
    cout<<"Enter the array in ascending order: ";
    for(i=0;i<n;++i){
        cin>>a[i];
    }
    cout<<" Enter element to insert:";
    cin>>x;
    for(i=0;i<n;++i){
        if(a[i]<=x&&x<a[i+1]){
            pos=i+1;
            break;
        }
    for(i=n+1;i>pos;--i){
        a[i]=a[i-1];
    }
    a[pos]=x;
    cout<<" Array after inserting element: ";
    for(i=0;i<n+1;i++){
        cout<<a[i]<<" ";
    }
}

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