186k views
5 votes
Create a partially filled array of CAPACITY 100 that is partially filled with the values 10 20 30 40 50 60 70 80 90 100. a) Create a print() function to print the array. b) Create an insertSorted() function which places an integer n into the array in sorted position (smallest to largest). c) Insert 25, 5 and 55 into the array in sorted position. d) Print the array before and after the elements have been inserted.

1 Answer

4 votes

Answer: provided in the explanation below

Step-by-step explanation:

follow this code to get the required solution, cheers.

#include<iostream>

using namespace std;

#define CAPACITY 100

void print_array(int arr[], int n);

void insert_array(int arr[], int &n, int val);

int main(){

int arr[CAPACITY] = {10,20,30,40,50,60,70,80,90,100};

int n = 10;

print_array(arr, n);

insert_array(arr, n, 25);

print_array(arr, n);

insert_array(arr, n, 5);

print_array(arr, n);

insert_array(arr, n, 55);

print_array(arr, n);

return 0;

}

void print_array(int arr[], int n){

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

cout<<arr[i]<<" ";

}

cout<<endl;

}

void insert_array(int arr[], int &n, int val){

int pos = -1;

int i=0;

while(arr[i]<val && i<n){

pos = i;

i++;

}

for(i=n; i>pos; i--){

arr[i] = arr[i-1];

}

arr[pos+1] = val;

n++;

}

cheers i hope this helped !!!1

User Wellie
by
6.0k points