write a program to insert an element into an array
Answer Posted / kingkill
// The Process of adding new element into an array is known as insertion. Array can be inserted in the beginning or the end if the array is unsorted.
//Insertion can only be performed in an array if the memory space initially allocated is not full, i.e the number of elements in the array is less then the size of the array.
#include <iostream>
using namespace std;
#define MAX 20
int main ()
{
int A[MAX],i,pos,j,size,item;
cout<<" Enter the number of elements in the array : ";
cin>>size;
cout<<" Array size defined is "<<size<<"\n";
if(size>MAX) // Checks the array size ( defined )
{
cout<<" The Maximum Size is 20 \n";
}
cout<< " Enter the elements in sorted order: \n";
for(i=0;i<size;i++)// Elements inserted equal to size
{
cin>>A[i];
}
cout<<" Enter the element to be inserted : ";
cin>>item;
if(size==MAX) // Checks if free array space is free
{
cout<<"The Aray is Full \n";
}
for(i=0;i<size;i++)
{
if(item<A[i])
{
pos=i;
break;
}
}
if(i==size)
pos=size;
for(j=size;j>pos;j--)
A[j]=A[j-1];
A[j]=item;
size++;
cout<<" Array elements after insertion : ";
for(i=0;i<size;i++)
{
cout<<A[i];
}
cout<<"\n";
return 0;
| Is This Answer Correct ? | 1 Yes | 4 No |
Post New Answer View All Answers
Why do we use the using declaration?
What is the role of copy constructor in copying of thrown objects?
Write a program in c++ to print the numbers from n to n2 except 5 and its multiples
Explain the static member function.
What is a set in c++?
Why is that unsafe to deal locate the memory using free( ) if it has been allocated using new?
When is dynamic checking necessary?
What ANSI C++ function clears the screen a) clrscr() b) clear() c) Its not defined by the ANSI C++ standard
What is c++ manipulator?
Write a program which uses Command Line Arguments
Why is c++ still popular?
Is c++ built on c?
What is a breakpoint?
A prime number is a number which is divisible only by itself and 1. Examples of the first few primes are 2, 3, 5, 7, 11. Consider writing a program which can generate prime numbers for you. Your program should read in and set a maximum prime to generate and a minimum number to start with when looking for primes. This program should be able to perform the following tasks: 1. Read the maximum number from user (keyboard input) to look for primes. The program should not return any primes greater than this number. 2. Read the minimum number from user (keyboard input) to look for primes. The program should not return any primes less than this number. 3. Generate and print out every prime number between the maximum prime and minimum number specified by the user.
Can we use this pointer in a class specific, operator-overloading function for new operator?