Answer to Question #24430 in C++ for adrian thomas

Question #24430
1.Write a program to insert a particular number in an array.
1
Expert's answer
2013-02-18T08:44:49-0500
#include <conio.h>
#include <iostream>

using namespace std;

void insert_element(int* array, int arraySize, int pos, int element)
{
if (pos < 0 || pos >= arraySize)
{
cout << "Invalid insertion position: " << pos << endl;
return;
}

/* Shift array elements right starting from the $pos index */
for (int k = arraySize - 1; k > pos; k--)
array[k] = array[k - 1];
array[pos] = element;
}

void print_array(int* array, int arraySize)
{
for (int c = 0; c < arraySize; c++)
cout << " " << array[c];
cout << endl;
}

int main()
{
int array[5] = {3, 17, 24, 6, 0};

cout << "The inial array:" << endl;
print_array(array, 5);
cout << endl;

insert_element(array, 5, 3, -8);
cout << "The -8 element has been inserted into the position 3:" << endl;
print_array(array, 5);

_getch();
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