Name-Gurkirat Singh Roll No. - 832025 PSCP Assignment-5 (Section-C) Due Date: 07/02/2021
Name-Gurkirat Singh Roll No. - 832025 PSCP Assignment-5 (Section-C) Due Date: 07/02/2021
1. Given an array A[] and positive integer K, write a program to count total number of pairs
in the array whose sum is divisible by K.
Input : A[] = {2, 2, 1, 7, 5, 3}, K = 4
Output : 5
There are five pairs possible whose sum is divisible by '4' i.e., (2, 2), (1, 7), (7, 5), (1, 3)
and (5, 3)
Ans.
#include <iostream>
using namespace std;
int main(void){
int k = 4;
int length_of_array=6;
int array[length_of_array] = {2, 2, 1, 7, 5, 3};
int counter=0;
for(int i=0; i<length_of_array; i++){
for(int j=0; j<length_of_array; j++){
if(i<=j){
continue;
}
int sum=array[i]+array[j];
if(sum % k == 0){
counter++;
}
}
}
cout<<counter<<endl;
return 0;
}
2. Write a program to compute even parity check bit (pc), for the given message word M =
m0m1m2...mn−1. The message and parity check bits are binary (0 or 1). The parity check bit
is generated based on the number of ones present in message. If the number of ones in the
message is odd, then parity check bit is 1 otherwise it is zero. Assume the message is loaded
into an array m such that each array element carries one message bit (m[0] = m0, m[1] =
m1, ...), and size of the message is specified through user input via keyboard. After
generating the parity check bit from the message, the code word will be generated by
inserting the parity check bit at the beginning of the array (codeword C = pcm0m1m2. . .
..mn−1). Note: You should not use any additional array. A sample input and corresponding
output are given below.
Input: Enter the size of message = 6
Enter the message bits 1 0 1 0 1 1
Output: Codeword = 0101011
Ans.
#include <iostream>
using namespace std;
int main(void){
int size_of_array;
cin >> size_of_array;
int array[size_of_array+1];
for(int i=1; i<=size_of_array; i++){
cin>>array[i];
}
array[0]=0;
if(check_1(array, size_of_array) % 2 == 0){
array[0]=0;
}
else{
array[0]=1;
}
for(int i=0; i<=size_of_array; i++){
cout<<array[i]<<" ";
}
cout<<endl;
return 0;
}
3. A man wanted to get into his work building, but he had forgotten his code. However, he
did remember five clues. These are what those clues were: The fifth number plus the third
number equals fourteen. The fourth number is one more than the second number. The first
number is one less than twice the second number. The second number plus the third number
equals ten. The sum of all five numbers is 30. What were the five numbers and in what
order? Write a program to display the five numbers and their order.
Ans.
#include <iostream>
using namespace std;
int main(void){
int length=5;
int code[length];
code[1]=4;
code[0]=2*code[1]+1;
code[3]=1+code[1];
code[2]=10-code[1];
code[4]=code[1]+4;
for(int i=0; i<length; i++){
cout<<code[i]<<" ";
}
cout<<endl;
}
4. Write a program to find ith largest and jth smallest element in the given array
Ans.
#include <iostream>
using namespace std;
int main(void){
int length;
cin>>length;
int ith_largest;
int jth_smallest;
cin>>ith_largest>>jth_smallest;
if (ith_largest>length || jth_smallest>length){
cout<<"enter valid number"<<endl;
return 1;
}
int array[length];
for(int i=0; i<length; i++){
cin>>array[i];
}
for(int i=0; i<length; i++){
for(int j= i+1; j<length; j++) {
if(array[j]<array[i]){
int temp= array[i];
array[i]=array[j];
array[j]=temp;
}
}
return 0;
}
5. Write a program to count the frequency of each element of an array.
Example :
Input the number of elements to be stored in the array :5
Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43
element – 3 : 25
element – 4: 43
Output :
The frequency of all elements of an array :
25 occurs 2 times
12 occurs 1 times
43 occurs 2 times
Ans.
#include <iostream>
using namespace std;
int main(void){
int length;
cout<<"Input the number of elements to be stored in the array: ";
cin>>length;
cout<<endl;
int array[length];
for(int i=0; i<length; i++){
cout<<"element - "<<i<<" : ";
cin>>array[i];
cout<<endl;
}
int number_of_zeroes=0;
for(int i=0; i<length; i++){
if(array[i]==0){
number_of_zeroes++;
}
}
if(number_of_zeroes>0){
cout<<"0 occurs "<<number_of_zeroes<<" times"<<endl;
}
for(int i=0; i<length; i++){
int counter=1;
if(array[i]!=0){
for(int j=0; j<length; j++){
if(array[i] == array[j] && i!=j){
counter++;
array[j] = 0;
}
}
}
else{
continue;
}
cout<<array[i]<<" occurs "<<counter<<" times"<<endl;
}
}
6. Write a program to replace every element in an array with the greatest element on its
right side.
The given array is : 7 5 8 9 6 8 5 7 4 6
After replacement, the modified array is: 9 9 9 8 8 7 7 6 6 0
Ans.
#include <iostream>
using namespace std;
int main(void){
int length;
cin>>length;
int array[length];
for(int i=0; i<length; i++){
cin>>array[i];
}
for(int i=0; i<length; i++){
if(i!=length-1){
int maximum = array[i+1];
for(int j=i+1; j<length; j++){
maximum = max(array[j], maximum);
}
array[i]=maximum;
}
else{
array[i]=0;
}
}
for(int i=0; i<length; i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
7. Write a program to count all distinct pairs for a specific difference in an array.
Example:
The given array is:
52376498
The distinct pairs for difference 5 are: [7, 2] [8, 3] [9, 4]
Number of distinct pairs for difference 5 are:
Ans.
#include <iostream>
using namespace std;
int main(void){
int length;
cin>>length;
int difference;
cout<<"Enter difference: ";
cin>>difference;
int array[length];
for(int i=0; i<length; i++){
cin>>array[i];
}
int counter=0;
for(int i=0; i<length; i++){
for(int j=i; j<length; j++){
if(difference==abs(array[i]-array[j])){
cout<<"["<<array[i]<<","<<array[j]<<"]"<<endl;
counter++;
}
}
}
cout<<"NUmber of distinct pairs for difference "<<difference<<" are "<<counter<<endl;
8. Given an array of integers. Find a peak element in it. An array element is peak if it is
NOT smaller than its neighbors. For corner elements, we need to consider only one
neighbor. For example, for input array {5, 10, 20, 15}, 20 is the only peak element. For
input array {10, 20, 15, 2, 23, 90, 67}, there are two peak elements: 20 and 90. Write a
program to print all the peak elements in a given array.
Ans.
#include <iostream>
using namespace std;
int main(void){
int length;
cin>>length;
int array[length];
for(int i=0; i<length; i++){
cin>>array[i];
}
for(int i=0; i<length; i++){
int counter=0;
if(i==0){
if(array[i]>array[i+1]){
counter++;
}
}
else if(i==length-1){
if(array[i]>array[i-1]){
counter++;
}
}
else{
if(array[i]>array[i+1] && array[i]>array[i-1]){
counter++;
}
}
if(counter>0){
cout<<array[i]<<endl;
}
}
return 0;
}
9. The ascending sequence of all reduced fractions between 0 and 1 that have denominators
≤ n is called the “Farey series of order n”. For example, the Farey series of order 7 is
Ans.
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;
int main(void){
int n;
stringstream ss;
cin>>n;
vector<string> g1;
vector<float> g2;
for(int i = 1; i<=n; i++){
for(int j = 0; j<=i; j++){
int numerator = j;
int denomenator = i;
//converting fraction to lowest form
for(int k=2; k<=denomenator; k++){
while(numerator%k==0 && denomenator%k==0){
numerator=numerator/k;
denomenator=denomenator/k;
}
}
//converting int numerator to string
ss<<numerator;
string numerator_str;
ss>>numerator_str;
//clearing string stream to add new number
ss.clear();
//converting int denomenator to string
ss<<denomenator;
string denomenator_str;
ss>>denomenator_str;
//clearing string stream to add new number
ss.clear();
float fraction = (float)numerator/(float)denomenator;
string fraction_str = numerator_str + "/" + denomenator_str;
//checking if element is already present in the vector
if(find(g1.begin(), g1.end(), fraction_str) != g1.end()){
continue;
}
else{
g1.push_back(fraction_str);
g2.push_back(fraction);
}
}
}
//sorting vectors
for(int i=0; i<g2.size(); i++){
for(int j= i+1; j<g2.size(); j++) {
if(g2[j]<g2[i]){
float temp_1=g2[i];
g2[i]=g2[j];
g2[j]=temp_1;
string temp= g1[i];
g1[i]=g1[j];
g1[j]=temp;
}
}
}
//printing vectors
for(int i = 0; i<g1.size(); i++){
cout<<g1[i]<<" ";
}
cout<<endl;
return 0;
}
10. The absolute distance between two integers x1 and x2 is given by | x2 – x1 |. Write a
program which sorts an array x[ ] of n integers in ascending order of their absolute
distances with a given number z. For example, given x[ ] = {9, 1, 12, 4, 2} and z = 6, the
sorted array will be x[ ] = {4, 9, 2, 1, 12}. Note that 4 is closest to 6, and 12 is farthest
from 6, in terms of absolute distances.
Ans.
#include <iostream>
using namespace std;
int main(void){
int length;
int z;
cin>>length;
cin>>z;
int array[length];
for(int i = 0; i < length; i++){
cin>>array[i];
}
int check_mod_use[length];
for(int i=0; i<length; i++){
if(z-array[i]>=0){
array[i]=z-array[i];
check_mod_use[i]=0;
}
else{
array[i]=array[i]-z;
check_mod_use[i]=1;
}
}
for(int i=0; i<length; i++){
for(int j= i+1; j<length; j++) {
if(array[j]<array[i]){
int temp= array[i];
int temp_for_check_mod=check_mod_use[i];
array[i]=array[j];
check_mod_use[i]=check_mod_use[j];
array[j]=temp;
check_mod_use[j]=temp_for_check_mod;
}
}