KARNATAKA PUC COMPUTER SCIENCE STUDY MATERIALS (SECOND EDITION)

Image
VISIT MY WORDPRESS BLOG FOR REVISED AND LATEST STUDY MATERIALS FIRST PUC FIRST PUC LAB MAUAL CHAPTER 1 COMPUTER SYSTEM CHAPTER 2 ENCODING SCHEMES AND NUMBER SYSTEM CHAPTER 3  EMERGING TRENDS CHAPTER 4 INTRODUCTION TO PROBLEM SOLVING CHAPTER 5 GETTING STARTED WITH PYTHON CHAPTER 6 FLOW CONTROL CHAPTER 7 FUNCTIONS CHAPTER 8 STRINGS CHAPTER 9 LISTS CHAPTER 10 TUPLES AND DICTIONARIES CHAPTER 11 SOCIETAL IMPACT SECOND PUC SECOND PUC LAB MANUAL CHAPTER 1 EXCEPTION HANDLING IN PYTHON CHAPTER 2 FILE HANDLING IN PYTHON CHAPTER 3 STACK CHAPTER 4 QUEUE CHAPTER 5 SORTING CHAPTER 6 SEARCHING CHAPTER 7  UNDERSTANDING DATA CHAPTER 8 DATABASE  CONCEPTS CHAPTER 9 SQL NOTES CHAPTER 9 SQL Q&A CHAPTER 10 COMPUTER NETWORKS CHAPTER 11 DATA COMMUNICATION CHAPTER 12 SECURITY ASPECTS

C++ Program: Insert an Element into an Array

 // 2 C++ program - insert an element into an array at a given position.


#include<iostream.h>

#include<conio.h>

#include<stdlib.h>


class Insertion

{

private:

int a[10], n, pos, ele, i; //Data Members

public:

void readdata( );

void insert( ); // Member Function Declaration

void display( );

};


void Insertion::readdata( )

{

cout<<"Enter the size of the array"<<endl;

cin>>n;

cout<<"Enter the elements for the array"<<endl;


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

cin>>a[i];


cout<<"Enter the position of the element in the array"<<endl;

cin>>pos;


cout<<"Enter the element to be inserted"<<endl;

cin>>ele;

}

void Insertion::insert( )

{

if(pos>n)

{

cout<<"Out of array limits!!!";

getch( );

exit(0);

}

else

{

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

a[i+1] = a[i]; // Shift array elements


a[pos] = ele; // Insert the given element

n = n+1; // Size of the array is incremented by 1

}

}


void Insertion::display( )

{

cout<<"Array elements after insertion are:"<<endl;


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

cout<<a[i]<<"\t";

}


void main( )

{


Insertion i;


clrscr( );


i.readdata( );

i.insert( );

i.display( );

getch( );

}

Comments

Popular posts from this blog

KARNATAKA PUC COMPUTER SCIENCE STUDY MATERIALS (SECOND EDITION)

Karnataka I PUC Computer Science 2024 Study Material | SECOND PUC HANDBOOK EXAM 2025