Question

Computer science Implement these below questions in C++: 1. Given two-bit strings of length n, find...

Computer science

Implement these below questions in C++:
1. Given two-bit strings of length n, find the bitwise AND, bitwise OR, and bitwise XOR of these strings.
2. Looking for positive integers that are not the sum of the cubes of nine different positive integers.
3. Given subsets A and B of a set with n elements, use bit strings to find A, A ∪ B, A ∩ B, A − B, and A ⊕ B.
4. Given a finite set, list all elements of its power set.
5. Given an ordered list of n integers and an integer x in the list, find the number of comparisons used to determine the position of x in the list using a linear search and using a binary search.
6. Given a list of n integers, find the first and last occurrences of the largest integer in the list.
7. Given a positive integer, find the prime factorization of this integer.
8. Given two positive integers, find their greatest common divisor using the Euclidean algorithm.
9. Given a square matrix, determine whether it is symmetric.
10. Given a 2n × 2n checkerboard with one square missing, construct a tiling of this checkerboard using right triominoes.

2 0
Add a comment Improve this question Transcribed image text
Answer #1

Comment down for any queries
Please give a thumbs up if you are satisfied with the answer

1)

#include<iostream>
using namespace std;

string BitwiseOR(string* array, int s1)
{
   string r;

   int ssize = min;
   for (int i = 0; i < s1; i++)
   {
       ssize = max(ssize, (int)array[i].size());
       reverse(array[i].begin(), array[i].end());
   }

   for (int i = 0; i < s; i++) {
       string s;
       for (int j = 0; j < ssize - array[i].size(); j++)
           s += '0';

       array[i] = array[i] + s;
   }
   for (int i = 0; i < ssize; i++) {
       int current = 0;
       for (int j = 0; j < s1; j++)
           current = current | (array[j][i] - '0');

       r += (current + '0');
   }
   reverse(r.begin(), r.end());
   return r;
}
int main()
{
   string array[] = { "10", "11", "1000001" };
   int s1 = sizeof(array) / sizeof(array[0]);
   cout << BitwiseOR(array, n);
   return 0;
}

2)

#include <iostream>
using namespace std;
int main()
{
int a, b, lcm;
cout<<" Enter First positive integer="<<endl;
cin>>a;
cout<<"Enter second Positive integer="<<endl;
cin>>b;
if(a>b)
{

lcm = a;
}
else
{

lcm = b;
}
while(1)
{
if( lcm%a==0 && lcm%b==0 )
   {
cout<<"The LCM of "<<a<<" and "<<b<<" is="<<lcm;
break;
}
  
lcm++;
}

return 0;
}

4)

#include <iostream>
#include <cmath>
using namespace std;
void printPowerSet(char *set, int set_size)
{
int pow_set_size = pow(2, set_size);
int j;
for(int i = 0; i < pow_set_size; i++)
{
for(j = 0; j < set_size; j++)
{
if(i & (1 << j))
{
cout << set[j];
}
}
cout << endl;
}
}
int main()

{
char set[] = {'a','b','c'};
printPowerSet(set, 3);
return 0;
}

5)

#include<iostream>
using namespace std;

int BinarySearch(int arr[], int left, int right, int elem)
{
   if (right >= left)
   {
       int mid = left + (right - left) / 2;
               if (arr[mid] == elem)
       {
           return mid;
       }
       if (arr[mid] > elem)
       {
           return BinarySearch(arr, left, mid - 1, elem);
       }
               return BinarySearch(arr, mid + 1, right, elem);
   }
   else
   {
       return -1; // when element is not present in array
   }
}
int LinearSearch(int arr1[], int n, int x)
{
   for (int i = 0; i < n; i++)
   {
       if (arr1[i] == x)
       {
           return i;
       }
   }
       return -1;
}
int main()
{
   int arr[] = { 2, 3, 4, 10, 40 };
   int size = sizeof(arr) / sizeof(arr[0]);
   int elem = 10;
   int result = BinarySearch(arr, 0, size - 1, elem);
   if (result == -1)
   {
       cout << "Element is not present in array";
   }
   else
   {
       cout << "Element is present at index " << result <<endl;
   }
   //Linear Search Calling.
   int arr1[] = { 2, 3, 4, 10, 40 };
   int x = 3;
   int n = sizeof(arr1) / sizeof(arr1[0]);
   int result1 = LinearSearch(arr1, n, x);
   if (result1 == -1)
   {
       cout << "Element is not present in array" <<endl;
   }
   else
   {
       cout << "Element is present at index " << result1 <<endl;
   }
   system("pause");
   return 0;
}

6)

#include<iostream>
#include<string>
using namespace std;

void sorting(int arr[],int size)
{
   int temp;
   for(int i=0;i<size-1;i++)
   {
      
       for(int j=i+1;j<size;j++)
       {
      
           if(arr[i]>arr[j])
           {
               temp=arr[i];
               arr[i]=arr[j];
               arr[j]=temp;
              
           }
       }
   }
  
}

int Max(int arr[],int size)
{
   int max=-999;
   for(int i=0;i<size;i++)
   {
       if(max<arr[i])
       {
           max=arr[i];
       }
   }
  
   return max;
}


int findFirstOccurrence(int arr[], int N, int x)
{
   // search space is arr[low..high]
   int low = 0, high = N - 1;

   // initialize the result by -1
   int result = -1;

   // iterate till search space contains at-least one element
   while (low <= high)
   {
       // find the mid value in the search space and
       // compares it with target value
       int mid = (low + high)/2;

       // if target is found, update the result and
       // go on searching towards left (lower indices)
       if (x == arr[mid])
       {
           result = mid;
           high = mid - 1;
       }

       // if target is less than the mid element, discard right half
       else if (x < arr[mid])
           high = mid - 1;

       // if target is more than the mid element, discard left half
       else
           low = mid + 1;
   }

   // return the leftmost index or -1 if the element is not found
   return result;
}

// Function to find last occurrence of a given number
// in sorted array of integers
int findLastOccurrence(int arr[], int N, int x)
{
   // search space is A[low..high]
   int low = 0, high = N - 1;

   // initialize the result by -1
   int result = -1;

   // iterate till search space contains at-least one element
   while (low <= high)
   {
       // find the mid value in the search space and
       // compares it with target value
       int mid = (low + high)/2;

       // if target is found, update the result and
       // go on searching towards right (higher indices)
       if (x == arr[mid])
       {
           result = mid;
           low = mid + 1;
       }

       // if target is less than the mid element, discard right half
       else if (x < arr[mid])
           high = mid - 1;

       // if target is more than the mid element, discard left half
       else
           low = mid + 1;
   }

   // return the leftmost index or -1 if the element is not found
   return result;
}

int main()
{
   int first;
   int Largest;
   int a[11]={3,2,5,1,4,6,1,5,1,6,6};
   sorting(a,11);
   for(int i=0;i<11;i++)
   {
       cout<<"i="<<i<<endl;
       cout<<a[i]<<endl;
   }
  
   Largest=Max(a,11);
   cout<<"First occurance:"<<findFirstOccurrence(a,11,Largest)<<endl;
   cout<<"Last occurance:"<<findLastOccurrence(a,11,Largest)<<endl;
  
  
  
  
  
}

7)

#include <iostream>
using namespace std;
void factors(int n)
{
int x = 2;
while (x * x <= n)
{
if (n % x == 0)
{   
cout << x<<endl;
n = (n / x);
}
else
{
x++;
}
}
if (n > 1)
{
cout << n << "\n";
}
}
int main()
{
int x;
cout << "Enter a positive integer greater than 1 : "<<endl;
cin >> x;
factors(x);
cout << "The result: " << x;
return 0;
}

8)

#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (b == 0)
{
        return a;
}
return gcd(b, a % b);
}
int main()
{
int a , b;
cout<<"Enter the values of a: "<<endl;
cin>>a;
cout<<"Enter the value of b:"<<endl;
cin>>b;
cout<<"GCD of "<< a <<" and "<< b <<" is:"<< gcd(a, b)<<endl;
}

9)

#include <iostream>
#include<string>
using namespace std;
  
int main()
{
bool check=true;
cout<<"Enter the number of rows of square matrix"<<endl;
int row;
cin>>row;
cout<<"Enter the number of coloumns"<<endl;
int col;
cin>>col;
int arr[row][col];

cout<<"Enter Elements"<<endl;
for(int i=0;i<row;i++)
{
cout<<"Row#"<<i;
for(int j=0;j<col;j++)
{
cout<<" Col#"<<j<<endl;
cin>>arr[i][j];

}
}

for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(arr[i][j]!=arr[j][i])
{
check=false;

}
}
}

if(check==true)
{
cout<<"This is symmetric"<<endl;
}

else
{
cout<<"This is not symmetric"<<endl;

}

}

10)

#include <iostream>
using namespace std;
int calculatePower(int, int);

int calculatePower(int base, int powerRaised)
{
if (powerRaised != 0)
{
  
return (base*calculatePower(base, powerRaised-1));
}
else
{
  
return 1;
}
}

int main()
{
int base, powerRaised, result;

cout << "Enter base number: ";
cin >> base;

cout << "Enter power number(positive integer): ";
cin >> powerRaised;

result = calculatePower(base, powerRaised);
cout << base << "^" << powerRaised << " = " << result;

return 0;
}

Comment down for any queries
Please give a thumbs up if you are satisfied with the answer

Add a comment
Know the answer?
Add Answer to:
Computer science Implement these below questions in C++: 1. Given two-bit strings of length n, find...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please, implement these subroutine. Write assembly language subroutines to perform the calculations listed hereunder: Table 1:...

    Please, implement these subroutine. Write assembly language subroutines to perform the calculations listed hereunder: Table 1: List of required subroutines. z= ABS (1) umber z=MAX(x, y) Get absolute value of a x: 16-bit signed z: 16-bit unsigned signed number. integer integer for the Assume signed number are absolute value of x represented in 2's i.e. X complement format. Given the elements x and x, y: 16-bit z: 16-bit unsigned y. Return the element with unsigned integers. integer storing the greater...

  • Consider the following two problems: Bin Packing: Given n items with positive integer sizes s1, s2,...

    Consider the following two problems: Bin Packing: Given n items with positive integer sizes s1, s2, . . . , sn, a capacity C for bins and a positive integer k, is it possible to pack the n items using at most k bins? Partition: Given a set S of n integers, is it possible to partition S into two subsets S1 and S2 so that the sum of the integers in S1 is equal to the sum of the...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • Prove using mathematical induction that for every positive integer n, = 1/i(i+1) = n/n+1. 2) Suppose...

    Prove using mathematical induction that for every positive integer n, = 1/i(i+1) = n/n+1. 2) Suppose r is a real number other than 1. Prove using mathematical induction that for every nonnegative integer n, = 1-r^n+1/1-r. 3) Prove using mathematical induction that for every nonnegative integer n, 1 + i+i! = (n+1)!. 4) Prove using mathematical induction that for every integer n>4, n!>2^n. 5) Prove using mathematical induction that for every positive integer n, 7 + 5 + 3 +.......

  • k Determine whether the rule describe a function with the given domain and target. You must...

    k Determine whether the rule describe a function with the given domain and target. You must provide a specific counterexample if you determine it is not a function. (Note that the symbol squareroot refers to the principal or positive square squreroot .) f:R rightarrow R where f(x) = sqaurerootx f:Z rightarrow where f(n) = squaretrootn^2 + 1 For c, d and e below, consider the function: f: {0,1}^n rightarrowZ (i.e., f maps elements from the set of all bit strings...

  • 1. (Integers: primes, divisibility, parity.) (a) Let n be a positive integer. Prove that two numbers...

    1. (Integers: primes, divisibility, parity.) (a) Let n be a positive integer. Prove that two numbers na +3n+6 and n2 + 2n +7 cannot be prime at the same time. (b) Find 15261527863698656776712345678%5 without using a calculator. (c) Let a be an integer number. Suppose a%2 = 1. Find all possible values of (4a +1)%6. 2. (Integers: %, =) (a) Suppose a, b, n are integer numbers and n > 0. Prove that (a+b)%n = (a%n +B%n)%n. (b) Let a,...

  • Can someone please help me with these questions? There is one example given. The solution should...

    Can someone please help me with these questions? There is one example given. The solution should be like that. Necessary lines of codes which can run in dr.Racket. thank you!! Here is an example of using the llisting package to display code. Check the preamble to see where this is imported and set up. After that you will see a sample interaction from the DrRacket console to show how this function could be tested. You should provide similar listings and...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

  • 1. A universal set, with n(U)70, is partitioned into three subsets: A, B, and C. If...

    1. A universal set, with n(U)70, is partitioned into three subsets: A, B, and C. If n(B) 3-n(A), and n(C) 2-n(B), find the number of elements in the subset A. 2. A license plate consists of eight symbols on each plate, where the first three symbols are letters of the alphabet and the following five symbols are the digits selected from the set f0, 1, 2, 3, 4, 5, 6, 7, 8, 9)? How many license plates can be produced...

  • Java question for two classes: Given the upper limit n as a parameter, the result of...

    Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT