Prac 6 CPP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

1) WAP in c++ to copy the elements of one array into another


array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int array1[10],array2[10],i;
cout<<"enter the value of array";
for(i=0;i<10;i++)
{
cin>>array1[i];
}
for(i=0;i<10;i++)
{
array2[i]=array1[i];
}
cout<<"the final array is";
for(i=0;i<10;i++)
cout<<array2[i]<<” “;
}

OUTPUT:

2) WAP to count frequency of each element in an array of 5 elements.


FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count,n[5],freq[5];
cout<<"enter the number of element in an array:";
for(i=0;i<5;i++)
{
cin>>a[i];
freq[i]=-1;
}
for(i=0;i<5;i++)
{
count=1;
for(j=i+1;j<5;j++)
{
if(a[i]==a[j])
{
count++;
freq[j]=0;
}
}
if(freq[i]!=0)
{
freq[i]=count;
}
}
cout<<"the frequency of the elements of thus array is:"<<endl;
for(i=0;i<5;i++)
{
if(freq[i]!=0)
{
cout<<a[i]<<"occurs"<<freq[i]<<"times"<<endl;
}
}

OUTPUT:
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

2) WAP to find maximum and minimum of an array of 10


elements.

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

void main()

clrscr();

int a[1000],i,n, min,max;

cout<<"Enter size of an array:";

cin>>n;

cout<<"Enter elements in array.";

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

cin>>a[i];

min=max=a[0];
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

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

if(min>a[i])

min=a[i];

if(max <a[i])

max=a[i];

cout<<"Minimum of array is :"<< min<<endl;

cout<<"maximum of array is :"<< max<<endl;

OUTPUT:

4) WAP to accept an array of 10 elements. Ask the user to enter a


number and check if it is present in the array.

#include<iostream.h>
#include<conio.h>
void main()
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

{
int a[10],size,i,num,flag=0;
cout<<"enter the size of array:";
cin>>size;
cout<<"enter"<<size<<"array elements one by one:";
for(i=0;i<size;i++)
{
cin>>a[i];
}
cout<<"enter the number you want to check";
cin>>num;
for(i=0;i<size;i++)
{
if(num==a[i])
{
flag++;
}
}
if(flag>0)
{
cout<<"the number"<<num<<"is present in the array:";
}
else
{
cout<<"the number"<<num<<"is not present in the array";
}
}
OUPUT:
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

5) WAP to accept an array of 10 elements.now check each element


and separate them into odd and even numbers array.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i;
cout<<"enter the elements of array";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"even number of array are:";
for(i=0;i<10;i++)
{
if(a[i]%2==0){
cout<<a[i]<<endl;
}
}
cout<<"odd number of array are";
for(i=0;i<=10;i++){
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

if(a[i]%2==1){
cout<<a[i]<<endl;
}
}
}

OUTPUT:

6) WAP to declare an array of 10 elements. But accept only 9


elements into this array. Now ask to user to enter another
element and the position where it should be inserted in the
array. Perform the insertion by shifting the other elements.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,pos,elem,tot;
cout<<"enter 9 array elements";
for(i=0;i<9;i++)
cin>>a[i];
cout<<"enter element to insert:";
cin>>elem;
cout<<"at what position?";
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

cin>>pos;
for(i=10;i>=pos;i--)
a[i]=a[i-1];
a[i]=elem;
tot++;
cout<<"the new array is:";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
}

OUTPUT:

7) WAP to delete an element from a desired position from an array


of 10 elements. [Accept the position from which element is to
be deleted. Delete the said element by shifting the other
elements appropriately]

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

int a[10],tot=10,i,elem,j,found;
cout<<"enter 10 elements";
for(i=0;i<tot;i++)
cin>>a[i];
for(i=0;i<tot;i++)
{
if(a[i]==elem)
{
for(j=1;j<(tot-1);j++)
a[j]==a[j+1];
found++;
i--;
tot--;
}
}
cout<<"the new array is:";
for(i=0;i<tot;i++)
cout<<a(i)<<" ";
}
}

OUTPUT:

8) WAP to delete an element from an array of 10 elements.


FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],tot=10,i,elem,j,found;
cout<<"Enter 10 elements:";
for(i=0;i<tot;i++)
cin>>arr[i];
cout<<"Enter elements to delete:";
cin>>elem;
for(i=0;i<tot;i++)
{
if(arr [i]==elem)
{
for(j=i;j<(tot-1);j++)
arr[i]=arr[j+1];
found=1;
i--;
tot--;
}
}
if(found==0)
cout<<"element not there in array";
else
cout<<"elements deleted successfully";
cout<<endl;
{
cout<<"the new array is:";
for(i=0;i<tot;i++)
cout<<arr[i]<<" ";
}
cout<<endl;
}
OUPUT:
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

9) WAP to print transpose of a 4x5 materix.


#include<iostream.h>
void main()
{
int a[4][5],trans[5][4],i,j;
cout<<"enter elements:";
for(i=0;i<4;i++){
for(j=0;j<5;j++){
cin>>a[i][j];
}
}
cout<<"entered materix"<<endl;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
cout<<" "<<a[i][j];
if(j==5-1)
cout<<endl;
}
}
for(i=0;i<4;i++){
for(j=0;j<5;j++){
trans[j][i]=a[i][j];
}
}
FYCS15 PRACTICAL 6 SIDDHESH CHAVAN

cout<<"transpose of materix is:";


for(i=0;i<5;i++){
for(j=0;j<4;j++){
cout<<" "<<trans[i][j];
if(j==4-1);
cout<<endl;
}
}
}
OUTPUT:

You might also like