Question

In: Computer Science

Counting evens Write the pseudo-code for a brute force approach to counting the number of even...

Counting evens

  1. Write the pseudo-code for a brute force approach to counting the number of even integers in an array of n integers.

  1. Write the pseudo-code divide-and-conquer algorithm for counting the number of even integers in an array of n integers.

  1. Give the recurrence relation for the number of additions in your divide-and-conquer algorithm.  

Solutions

Expert Solution

Brute force approach pseudo code:

// function to count no. of even integers
// pseudo code
// input: arr : array of int, n: size of array
// 0 based indexing
// output: no. of even integers
// brute force approact

int countEven(int arr[],int n){
        int count = 0; 
        // loop through array
        for(int i=0;i<n;i++){
                if(arr[i]%2 == 0){ // even integer
                        count = count + 1;
                }
        }
        // return count value
        return count;
}

Divide and conquer approach:

// function to count no. of even integers
// pseudo code
// input: arr : array of int, s: starting index of array, e: last index of array
// initial call: countEven(arr,0,n-1); where n is size of array
// 0 based indexing
// output: no. of even integers
// divide and conquer approach

int countEven(int arr[],int s,int e){
        // base case
        if(s-e == 0){ // only one element then check it directly
                if(arr[s]%2 == 0 ){
                        return 1;
                } else{
                        return 0;
                }
        }
        // mid value of array
        int mid = (s+e)/2;
        // call to left half
        int count = countEven(arr,s,mid); 
        // call to right half
        count = count + countEven(arr,mid+1,e); // also add to count
        // return count value
        return count;
}

===============

Recurrence relation:

Let n be the size of array and T(n) represent no. of addition for array of size n.

T(1) = 0 // we directly return the value by checking, (base case)

T(n) = 2*T(n/2) + 1 // call to left and right half and we use one addition to count

==================

For any query coment


Related Solutions

Counting integers greater than 10 Write the pseudo-code for a brute force approach to counting the...
Counting integers greater than 10 Write the pseudo-code for a brute force approach to counting the number of integers greater than 10 in an array of n integers. Write the pseudo-code divide-and-conquer algorithm for counting the number of integers greater than 10 in an array of n integers. Give the recurrence relation for the number of comparisons in your divide-and-conquer algorithm in part b.  
String search   Describe the brute force solution to counting the number of times the letter “a”...
String search   Describe the brute force solution to counting the number of times the letter “a” occurs in a text. Outline a divide-and-conquer algorithm for counting the number of times the letter “a” occurs in a text. Analyze each approach and compare the efficiencies.
Write out the “brute-force” (exact) expressions for force and torque on a loop 1 in the...
Write out the “brute-force” (exact) expressions for force and torque on a loop 1 in the magnetic field B of loop 2. Write these expressions using magnetic moment of loop 1. Pay attention to the r, r’, … vectors. A magnet (magnetic dipole) tends to align itself parallel to external field. If the magnetic moment is 5 [CGS units] in -z direction and magnetic field is 25 [CGS units] in z direction. What is the value of the torque? Plot...
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a Python script that performs brute force to extract a password protected zip file named...
Write a Python script that performs brute force to extract a password protected zip file named sec2.zip. The password is believed to be associated with one of the dictionary word in the 'wordlist.txt file. a) Paste your code here b) What is the password?
Explain why centrifugal force is a false (pseudo) force.
Explain why centrifugal force is a false (pseudo) force.
Write psuedocode to brute-force a simple password engine. Then, offer advice on making passwords more secure...
Write psuedocode to brute-force a simple password engine. Then, offer advice on making passwords more secure to brute-force attacks.
A customer in a grocery store is purchasing three items. Write the pseudo code that will:...
A customer in a grocery store is purchasing three items. Write the pseudo code that will: • Ask the user to enter the name of the first item purchased. Then ask the user to enter the cost of the first item purchased. Make your program user friendly. If the user says the first item purchased is milk, then ask: “What is the cost of milk.” [This should work no matter what item is entered by the user. I might buy...
Given a BST and a sum, write pseudo code to determine if the tree has a...
Given a BST and a sum, write pseudo code to determine if the tree has a root- to-leaf path such that adding up all the values along the path equals the given sum. Given the below BST and sum = 49, the array is (8, 4, 10, 1, 0, 3, 9, 15, 16). Return true, as there exist a root-to-leaf path 8− > 10− > 15− > 16 which sum is 49.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT