0% found this document useful (0 votes)
65 views

C++ Program To Demonstrate Basic Array Function Operation

This code implements various array operations like append, insert, delete, edit, display, and search in C++. It defines a main array and menu driven program that allows the user to choose an operation to perform on the array such as appending a new element, inserting a new element at a specified index, deleting an element by value, editing an element, displaying all elements, or searching for an element. Functions are defined to implement each array operation selected by the user.

Uploaded by

joooking
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

C++ Program To Demonstrate Basic Array Function Operation

This code implements various array operations like append, insert, delete, edit, display, and search in C++. It defines a main array and menu driven program that allows the user to choose an operation to perform on the array such as appending a new element, inserting a new element at a specified index, deleting an element by value, editing an element, displaying all elements, or searching for an element. Functions are defined to implement each array operation selected by the user.

Uploaded by

joooking
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

//C++ Program to demonstrate basic array function operation

#include<iostream.h>
const int ARRAY_SIZE = 5;

void LoadArray(int a[ ], int size);


void DisplayFirstToLast(int a[ ], int size);
void DisplayLastToFirst(int a[ ], int size);
int FindMinimumValue(int a[ ], int size);
int FindMinimumIndex(int a[ ], int size);
int FindMaximumValue(int a[ ], int size);
int FindMaximumIndex(int a[ ], int size);

int main()
{
int temp[ARRAY_SIZE];
int i;
int maxValue = -1;
int maxIndex = -1;
int minValue = -1;
int minIndex = -1;

LoadArray(temp, ARRAY_SIZE);
DisplayFirstToLast(temp, ARRAY_SIZE);
minValue = FindMinimumValue(temp, ARRAY_SIZE);
minIndex = FindMinimumIndex(temp, ARRAY_SIZE);
cout << "\n The smallest value in the array is " << minValue << endl;
cout << "\n The index of the smallest value is " << minIndex << endl;
maxValue = FindMaximumValue(temp, ARRAY_SIZE);
maxIndex = FindMaximumIndex(temp, ARRAY_SIZE);
cout << "\n The largest value in the array is " << maxValue << endl;
cout << "\n The index of the largest value is " << maxIndex << endl;
DisplayLastToFirst(temp, ARRAY_SIZE);

return(0);
}
//**************************LoadArray*******************************************
void LoadArray(int a[], int size)
//function prompts user to enter values
//values are stored in consecutive positions in the array
{
//your code goes here
}
//**************************DisplayFirstToLast**********************************
void DisplayFirstToLast(int a[], int size)
//function outputs elements in order of first to last
//note: function does not re-order elements within array
// (positions of array elements remain unchanged)
{
//your code goes here
}
//**************************DisplayLastToFirst**********************************

1
void DisplayLastToFirst(int a[], int size)
//function outputs elements in order of last to first
//note: function does not re-order elements within array
// (positions of array elements remain unchanged)
{
//your code goes here
}
//**************************FindMinimumValue************************************
int FindMinimumValue(int a[], int size)
//function searches array to find minimum value stored in array
//function returns minimum value to caller
{
//your code goes here
}
//*************************FindMinimumIndex*************************************
int FindMinimumIndex(int a[], int size)
//function searches array to find index of minimum value stored in array
//function returns index of minimum value to caller
{
//your code goes here
}
//*************************FindMaximumValue*************************************
int FindMaximumValue(int a[], int size)
//function searches array to find maximum value stored in array
//function returns maximum value to caller
{
//your code goes here
}
//*************************FindMaximumIndex*************************************
int FindMaximumIndex(int a[], int size)
//function searches array to find index of maximum value stored in array
//function returns index of maximum value to caller
{
//your code goes here
}

2
Code for Program that performs array operations like insert,delete,
search, sort, merge and display in C++ Programming

// Program that performs array operations// Array classclass array{


int arrA[MAX],location,item,nA;
int arrB[MAX],nB;
int arr_merge[MAX+MAX],nM;
public:
array(){
location=0;item=0;nA=0;
nB=0;nM=0;
}
void init(); //initial data assignmentvoid traverse(); //process is display
(assumed)void insert();
void del();
void search();
void sort();
void merge();
};

void array :: init(){


clrscr();
cout<<"\n\n*****Initialize Array*****\n";
int choice,i;
while(1){
cout<<"\n\n1) Array A\n";
cout<<"2) Array B\n";
cout<<"3) Array Merge\n";
cout<<"4) Return\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice){
case 1 : cout<<"\n\n--Array A--\n";
cout<<"Total elements to be inserted : ";
cin>>nA;
for(i=0;i<nA;i++)
cin>>arrA[i];
break;
case 2 : cout<<"\n\n--Array B--\n";
cout<<"Total elements to be inserted : ";
cin>>nB;
for(i=0;i<nB;i++)
cin>>arrB[i];
break;
case 3 : cout<<"\n\n--Array Merge--\n";
cout<<"Total elements to be inserted : ";
cin>>nM;
for(i=0;i<nM;i++)
cin>>arr_merge[i];
break;
case 4 : goto end;
default : cout<<"\n\nInvalid Position\n";
}
}
end:
}

void array :: traverse(){


clrscr();
cout<<"\n\n*****Display Process during Traversing*****\n";
int choice,i;

3
while(1){
cout<<"\n\n1) Array A\n";
cout<<"2) Array B\n";
cout<<"3) Array Merge\n";
cout<<"4) Return\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice){
case 1 : cout<<"\n\n--Array A--\n";
for(i=0;i<nA;i++)
cout<<setw(5)<<arrA[i];
break;
case 2 : cout<<"\n\n--Array B--\n";
for(i=0;i<nB;i++)
cout<<setw(5)<<arrB[i];
break;
case 3 : cout<<"\n\n--Array Merge--\n";
for(i=0;i<nM;i++)
cout<<setw(5)<<arr_merge[i];
break;
case 4 : goto end;
default : cout<<"\n\nInvalid Position\n";
}
getch();
}
end:
}

/*All Operations Except Merging are performed on arrA*/


void array :: insert(){
clrscr();
int i;
cout<<"\n\n*****Inserting Element*****\n";
if(nA >= MAX){
cout<<"\nArray is Full\nInsertion Not Possible\n";
goto end;
}
cout<<"\nEnter Location of insertion : ";
cin>>location;
location--;
if(location<0 || location>=nA)
{
cout<<"\n\nInvalid Position\n";
goto end;
}
cout<<"Enter Item value to be inserted : ";
cin>>item;
for(i=nA-1;i>=location;i--){
arrA[i+1] = arrA[i];
}
arrA[location]=item;
nA++;
cout<<"\nItem is Inserted\n";
end:
getch();
}

void array :: del(){


clrscr();
int i;
cout<<"\n\n*****Deleting Element*****\n";

4
if(nA < 0){
cout<<"\nArray is Empty\nDeletion Not Possible\n";
goto end;
}
cout<<"\nEnter Location of deletion : ";
cin>>location;
location--;
if(location<0 || location>=nA)
{
cout<<"\n\nInvalid Position\n";
goto end;
}
cout<<"\nItem deleted is : "<<arrA[location];
for(i=location;i<nA;i++){
arrA[i] = arrA[i+1];
}
arrA[nA-1]=0;
nA--;
end:
getch();
}

void array :: search(){


clrscr();
int i,found=-1;
cout<<"\n\n*****Searching Element*****\n";
cout<<"\nEnter Item value to be search : ";
cin>>item;
for(i=0;i<nA;i++){
if(arrA[i] == item){
found=i+1;
break;
}
}
if(found==-1)
cout<<"\nSearch NOT FOUND\n";
else
cout<<"\nSearch is FOUND at "<<found<<" location\n";
getch();
}

void array :: sort(){


clrscr();
int i,j,temp;
cout<<"\n\n*****Sorting Element*****\n";
for(i=0;i<nA;i++){
for(j=i;j<nA;j++){
if(arrA[i] > arrA[j]){
temp = arrA[i];
arrA[i] = arrA[j];
arrA[j] = temp;
}
}
}
cout<<"\nData are Sorted\n";
getch();
}

void array :: merge(){


clrscr();
int i,j;
cout<<"\n\n*****Merging Arrays*****\n";

5
for(i=0;i<nA;i++)
arr_merge[i]=arrA[i];

for(j=0;j<nB;j++,i++)
arr_merge[i]=arrB[j];

nM=nA+nB;
cout<<"\nArrays are Merged\n";
getch();
}

// Program that accesses array class and displays result.

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define MAX 10
#include "array.cpp"void main()
{
int choice;
array obj;
while(1){
clrscr();
cout<<"\t\tALL ARRAY OPERATIONS\n\n";
cout<<"\t\t1) Initialise Data\n";
cout<<"\t\t2) Traverse (Display Process)\n";
cout<<"\t\t3) Insert\n";
cout<<"\t\t4) Delete\n";
cout<<"\t\t5) Search\n";
cout<<"\t\t6) Sort\n";
cout<<"\t\t7) Merge\n";
cout<<"\t\t8) Exit\n";
cout<<"\t\tEnter your Choice : ";
cin>>choice;
switch(choice){
case 1 : obj.init();
break;
case 2 : obj.traverse();
break;
case 3 : obj.insert();
break;
case 4 : obj.del();
break;
case 5 : obj.search();
break;
case 6 : obj.sort();
break;
case 7 : obj.merge();
break;
case 8 : gotoout;
default: cout<<"\n\n\t\tInvalid Choice\n\n";
getch();
break;
}
}
out:
}

6
Code for Program to perform array operations like append, insert,
delete, edit, display and search and element in C++ Programming
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

main()
{
int array[15];
int no_el;
clrscr();
cout<<"Enter the no of element :";
cin>>no_el;
for(int i=0;i<no_el;i++)
{
cout<<"Enter the element : ";
cin>>array[i];
}
while(1)
{
clrscr();
cout<<endl<<"1. Append";
cout<<endl<<"2. Insert";
cout<<endl<<"3. delete by value";
cout<<endl<<"4. edit";
cout<<endl<<"5. display";
cout<<endl<<"6. search";
cout<<endl<<"7. exit";
cout<<endl<<"Enter your choice : ";
int choice;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the new element : ";
int new_el;
cin>>new_el;
array[no_el]=new_el;
no_el++;
break;
case 2:
cout<<"Enter the position at which you want to insert : ";
int pos;
cin>>pos;
cout<<"Enter the new element : ";
cin>>new_el;
pos--;
for(i=no_el-1;i>=pos;i--)
array[i+1]=array[i];
array[pos]=new_el;
no_el++;
break;

7
case 3:
cout<<"Enter the value to be search : ";
int key;
cin>>key;
for(pos=0;pos<no_el;pos++)
{
if(array[pos]==key)
break;
}
if(pos==no_el)
{
cout<<"Search key not found";
break;
}
for(i=pos;i<no_el;i++)
array[i]=array[i+1];
no_el--;
break;
case 4:
cout<<"Enter the position to be edit : ";
cin>>pos;
cout<<"Enter the new value for old position : ";
cin>>array[pos-1];
break;
case 5:
cout<<endl;
for(i=0;i<no_el;i++)
cout<<endl<<"The element is : "<<array[i];
break;
case 6:
cout<<"Enter the value to be search : ";
cin>>key;
for(pos=0;pos<no_el;pos++)
{
if(array[pos]==key)
break;
}
if(pos==no_el)
{
cout<<"Search key not found";
break;
}
cout<<"Search key found at : "<<pos+1;
break;
case 7:
return(0);
break;
}
getch();
}
}

You might also like