Lab Task: 1: Insert Any Element in Array Code

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab report 3

Lab Task: 1
Insert any element in array

Code

#include<iostream>
#include<conio.h> using namespace std;
int main(){
int arr[50], size, in, i, p;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];}
cout<<"Enter element to be in : ";
cin>>in;
cout<<"At which position or enter index number) ? ";
cin>>p;

for(i=size; i>p; i--)


{
arr[i]=arr[i-1];
}
arr[p]=in;
cout<<"Element aded \n";
cout<<"new array is \n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}}
Output

9/19/2019

1
Lab report 3

Lab Task: 2
Find smallest and largest number in array

Code

#include<iostream>
using namespace std;
int main ()
{
int arr[10], n, i, max, min;
cout << " size of the array : ";
cin >> n;
cout << "Enter the elements : ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++) {
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++) {
if (min > arr[i])
min = arr[i]; }
cout << "Largest : " << max;
cout << "\nSmallest : " << min; }

Output

9/19/2019

2
Lab report 3

Lab Task: 3
Merge two arrays in such a way that resultant array will store in third array

Code
#include<iostream>
#include<conio.h>

using namespace std;


int main(){
int hsn1[50], hsn2[50], sz1, sz2, sz, i, j, k, mrg[100];
cout<<" Array 1 size : ";
cin>>sz1;
cout<<" Array 1 elements : ";
for(i=0; i<sz1; i++){
cin>>hsn1[i];
}
cout<<" Array 2 zize : ";
cin>>sz2;
cout<<" Array 2 elements : ";
for(i=0; i<sz2; i++){
cin>>hsn2[i];}
for(i=0; i<sz1; i++) {
mrg[i]=hsn1[i];}
sz=sz1+sz2;
for(i=0, k=sz1; k<sz && i<sz2; i++, k++) {
mrg[k]=hsn2[i];
}
cout<<" merged array ....\n";
for(i=0; i<sz; i++) {
cout<<mrg[i]<<" ";
}
getch(); }
Output

9/19/2019

3
Lab report 3

Lab Task: 4 ,5
4- delete any element from array

5- search any element from array

Code
#include<iostream>
#include<process.h>
using namespace std;
int main({
int a[50],x,n,i,j,b[50];
cout<<"enter the size of array: ";
cin>>n;
cout<<"\nEnter elements of Array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to delete:";
cin>>x;
for(i=0,j=0;i<n;++i){
if(a[i]!=x)
b[j++]=a[i];
}if(j==n){
cout<<"\nSoory!!!Element is not in the Array";
exit(0);
}else {
cout<<"\nNew Array is ";
for(i=0;i<j;i++)
cout<<b[i]<<" ";}
return 0; }
Output

9/19/2019

You might also like