Uploaded by devp9980

ASIGNMENT DSA 1 MC21143 sem 2

advertisement
TASK : 1
CODE : #include<bits/stdc++.h>
using namespace std;
int search(int a[] , int b , int n)
{
int count = 0;
for(int i=0 ; i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i != j && ((a[i]+a[j]) % b )==0 )
{
count += 1;
} }
}
if(count%2==0)
count /=2;
else count = (count-1)/2 +1;
return count;
}
int main()
{
int a[100];
int n,b;
cout<<"enter how many number you have to enter in the arrray : "<<endl;
cin>>n;
cout<<"enter the numbers : "<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter the number : "<<endl;
cin>>b;
cout<<"the number of the pairs possible is "<<search(a , b, n);
return 0;
}
OUTPUT: enter how many number you have to enter in the arrray :
6
enter the numbers :
2
2
1
7
5
3
enter the number :
4
the number of the pairs possible is 5
--------------------------------
Process exited after 22.49 seconds with return value 0
Press any key to continue . . .
TASK : 2
CODE:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,n,SUM=0;
cout<<"Enter size of the array :";
cin>>n;
int a[n];
cout<<"ENTER MESSAGE BITS : " ;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++){
if(a[i]==1)
SUM++;
}
cout<<SUM%2<<" ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
OUTPUT:
Enter size of the array :6
ENTER MESSAGE BITS : 1 0 1 0 1 1
0 1 0 1 0 1 1
--------------------------------
Process exited after 6.717 seconds with return value 0
Press any key to continue . . .
TASK : 3
CODE:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,j,a,b,c,d,e;
for(i=0;i<10;i++)
{
a=i;
b=(a+1)/2;
c=10-b;
d=b+1;
e=14-c;
if((a+b+c+d+e)==30)
{
cout<<a<<b<<c<<d<<e;
}
}
return 0;
}
OUTPUT: 74658
--------------------------------
Process exited after 0.08334 seconds with return value 0
Press any key to continue . . .
TASK : 4
CODE:
#include<bits/stdc++.h>
using namespace std;
int sorting(int a[],int n ,int l,int s)
{
int count[20];
int k;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]<a[j])
{
k = a[i];
a[i] = a[j];
a[j] = k;
}
}
}
cout<<"The ith largest element is : "<<endl;
cout<<a[l-1]<<endl;
cout<<"The jth smallest element is : "<<endl;
cout<<a[n-s]<<endl;
}
int main()
{
int a[20];
int n;
int i,j;
cout<<"Enter the number of element you want to enter in the array : "<<endl;
cin>>n;
cout<<"Enter the array elements : "<<endl;
for(int k=0;k<n;k++)
{
cin>>a[k];
}
cout<<"Enter the value of i : "<<endl;
cin>>i;
cout<<"Enter the value of j : "<<endl;
cin>>j;
sorting(a,n,i,j);
return 0; }OUTPUT: Enter the number of element you want to enter in the array :
6
Enter the array elements :
5
7
9
10
2
3
Enter the value of i :
2
Enter the value of j :
3
The ith largest element is :
9
The jth smallest element is :
5
--------------------------------
Process exited after 26.48 seconds with return value 0
Press any key to continue . . .
TASK : 5
CODE:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,num;
cout<<"Enter the size of array " ;
cin>>n;
int a[n],f[10];
for(i=0;i<n;i++)
{
cout<<"element - "<< i <<" ";
cin>>a[i];
cout<<endl;
}
for(i=0;i<n;i++)
{
num=0;
for(j=0;j<n;j++)
{
if(a[i]==a[j])
{
num++;
}
}
f[i]=num;
}
for (int i=0; i<n; i++)
{
int j;
for (j=0; j<i; j++)
if (a[i] == a[j])
break;
if (i == j)
cout << a[i] <<" occurs "<< f[i]<< " times."<<endl;
}
return 0;
}
OUTPUT: Enter the size of array 6
element - 0 25
element - 1 12
element - 2 43
element - 3 25
element - 4 43
element - 5 25
25 occurs 3 times.
12 occurs 1 times.
43 occurs 2 times.
--------------------------------
Process exited after 40.45 seconds with re
TASK : 6
CODE:
#include<bits/stdc++.h>
using namespace std;
int search (int a[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[j]>a[i])
{
a[i]=a[j];
}
}
}
cout<<"The elements of the array are : "<<endl;
for(int i=0 ; i<=(n-1);i++)
{
if(i ==(n-1))
{
cout<<" 0"<<endl;
break;
}
cout<<" "<<a[i];
}
}
int main()
{
int n;
int a[20];
cout<<"Enter number of element you want to enter in the array : "<<endl;
cin>>n;
cout<<"Enter the array element : "<<endl;
for(int i= 0 ;i<n;i++)
{
cin>>a[i];
}
search(a,n);
return 0;
}
OUTPUT: Enter number of element you want to enter in the array :
10
Enter the array element :
7
5
8
9
6
8
5
7
4
6
The elements of the array are :
9 9 9 9 8 8 7 7 6 0
--------------------------------
Process exited after 19.3 seconds with return value 0
Press any key to continue . . .
TASK : 7
CODE:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[100],i,n,j,num=0;
cout<<"Enter the no. of element in Array " ;
cin>>n ;
cout<<"Enter Array Elememt " ;
for(i=0;i<n;i++)
{
cin>>a[i] ;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]>a[j]&&a[i]-a[j]==5)
{
cout<<"("<<a[i]<<","<<a[j]<<") " ;
num++;
}
}
}
cout<<endl<<"Number of distinct pairs for difference 5 are : "<<num ;
return 0 ;
}
OUTPUT: Enter the no. of element in Array 8
Enter Array Elememt 5 2 3 7 6 4 8 9
(7,2) (8,3) (9,4)
Number of distinct pairs for difference 5 are : 3
--------------------------------
Process exited after 24.98 seconds with return value 0
Press any key to continue . . .
TASK : 8
CODE:
#include<bits/stdc++.h>
using namespace std;
int peak(int a[], int n )
{
int j=0;
int count[20];
for(int i=0;i<n;i++)
{
if(a[i]>a[i-1] && a[i]>a[i+1])
{
count[j]=a[i];
j +=1;
}
}
cout<<"Peak elements of the array are : "<<endl;
for(int i=0;i<j;i++)
{
cout<<count[i]<<endl;
}
}
int main()
{
int a[20];
int n;
cout<<"Enter the number of element you want to enter in the array : "<<endl;
cin>>n;
cout<<"Enter the array element : "<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
peak(a,n);
}
OUTPUT: Enter the number of element you want to enter in the array :
7
Enter the array element :
10
20
15
2
23
90
68
Peak elements of the array are :
20
90
--------------------------------
Process exited after 18.97 seconds with return value 0
Press any key to continue . . .
TASK : 9
CODE:
#include <bits/stdc++.h>
using namespace std;
class Term {
public:
int x, y;
Term(int x, int y)
: x(x), y(y)
{
}
};
bool cmp(Term a, Term b)
{
return a.x * b.y < b.x * a.y;
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void farey(int n)
{
vector<Term> v;
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j)
if (gcd(i, j) == 1)
v.push_back(Term(i, j));
}
sort(v.begin(), v.end(), cmp);
cout << "0/1 ";
for (int i = 0; i < v.size(); ++i)
cout << v[i].x << "/" << v[i].y << " ";
cout << "1/1";
}
int main()
{
int n ;
cout<<"Enter the number : "<<endl;
cin>>n;
cout << "Farey Sequence of order " << n << " is\n";
farey(n);
return 0;
}
OUTPUT: Enter the number :
7
Farey Sequence of order 7 is
0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1
--------------------------------
Process exited after 0.5851 seconds with return value 0
Press any key to continue . . TASK : 10
CODE:
#include<bits/stdc++.h>
using namespace std;
int absolute(int a[],int n,int d)
{
int num;
int s,m;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
s = (a[i]-d);
if(s<0)
{
s=s*(-1);
}
m = (a[j]-d);
if(m<0)
{
m *=(-1);
}
if(s>m)
{
num = a[i];
a[i]= a[j];
a[j]= num;
}
}
}
cout<<"The sorted array is : ";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";;
}
}
int main()
{
int a[20];
int n;
int d;
cout<<"Enter the size of array : "<<endl;
cin>>n;
cout<<"Enter the array element : "<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the number you want to sort according : "<<endl;
cin>>d;
absolute(a,n,d);
return 0;
}
OUTPUT: Enter the size of array :
5
Enter the array element :
9 1 12 4 2
Enter the number you want to sort according :
6
The sorted array is : 4 9 2 1 12
--------------------------------
Process exited after 17.39 seconds with return value 0
Press any key to continue . . . 
Download