Answer to Question #266475 in C++ for Korea

Question #266475

write a program using array 1,2,3,4,5 and insert midle 5,6 and delete 3 ?

1
Expert's answer
2021-11-15T17:27:39-0500
//write a program using array 1,2,3,4,5 and insert midle 5,6 and delete 3 ?
#include <iostream>


using namespace std;


int main()
{
    int n=5;
    int num=5;
    int num2=6;
    int arr[n]={1,2,3,4,5};
    int loc=3;
    int loc2=4;
    for (int i = n; i >= loc; i--) {
        arr[i] = arr[i - 1];
    }
    n++;
    arr[loc - 1] = num;
    for (int i = n; i >= loc2; i--) {
        arr[i] = arr[i - 1];
    }
    n++;
    arr[loc2 - 1] = num2;


    cout << "\nArray elements after insertion:\n";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    
    cout << "\nArray elements after deletion:\n";
    
    int el=3;
    for(int i=0; i<n; i++)
    {
        if(arr[i]==el)
        {
            for(int j=i; j<(n-1); j++)
                arr[j] = arr[j+1];
            i--;
            n--;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS