Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C PROGRAMMING Q1. SUM OF DIGITS sumDigits(126) 9 sumDigits(49) 13 sumDigits(12) 3 sumDigits(1) 1 sumDigits(0) 0 sumDigits(10110) 3 sumDigits(235) 10 Code: Q2. Count 7

IN C PROGRAMMING

Q1. SUM OF DIGITS

sumDigits(126) 9

sumDigits(49) 13

sumDigits(12) 3

sumDigits(1) 1

sumDigits(0) 0

sumDigits(10110) 3

sumDigits(235) 10

Code:

Q2. Count 7

count7(717) 2

count7(7) 1

count7(123) 0

count7(771237) 3

count7(47571) 2

count7(777576197) 5

count7(99799) 1

Code:

Q3. Count 9

count9(9) 1

count9(919) 2

count9(9919) 4

count9(1919199) 5

count9(123) 0

count9(99799) 6

count9(7979) 2

count9(99999) 9

Code:

Q4. Power N

powerN(2, 5) 32

powerN(3, 1) 3

powerN(3, 2) 9

powerN(3, 3) 27

powerN(10, 2) 100

powerN(10, 3) 1000

Code:

Q5. array330

array330([1, 2, 20], 3, 0) true

array330([3, 30], 2, 0) true

array330([3], 1, 0) false

array330([1, 2, 3, 4, 5, 6], 6, 0) false

array330([1, 2, 3, 4, 4, 50, 500, 6], 7, 0) true

array330([], 0, 0) false

Code:

Q6. Binary Search

The binary search algorithm is a method of searching a sorted array for a single element by cutting the array in half with each recursive pass. The trick is to pick a midpoint near the center of the array, compare the data at that point with the data being searched and then responding to one of three possible conditions: the data is found at the midpoint, the data at the midpoint is greater than the data being searched for, or the data at the midpoint is less than the data being searched for.

Recursion is used in this algorithm because with each pass a new array is created by cutting the old one in half. The binary search procedure is then called recursively, this time on the new (and smaller) array. Typically the array's size is adjusted by manipulating a beginning and ending index. The algorithm exhibits a logarithmic order of growth because it essentially divides the problem domain in half with each pass.

In this example, you need an extra function at user's convenience since a user may want to simply call the function with three parameters such as binarySearch(data, dataSize, toFind). Once you get the user's initial call, then you call binary_search(data, toFind, 0 dataSize-1) recursively.

To test this example, just use an integer sorted array size of TEST_N = 10 which is filled with numbers between 0 ~ 99. In your test, at least include the following cases such as

a negative number, and a number greater than 100,

an existing one data in upper half and lower half,

non-existing one in upper half and lower half. For example,

data { 5, 20, 21, 30 40, 65, 70, 77, 88, 90}

minimum test cases for 'toFind' = -10, 105, 88, 21, 80, 25

Code:

/*

Binary Search Algorithm. INPUT:

data is a array of integers SORTED in ASCENDING order,

toFind is the integer to search for, start is the minimum array index, end is the maximum array index

OUTPUT:

position of the integer toFind within array data, -1 if not found

*/

int binary_search(int *data, int toFind, int start, int end)

{

//Get the midpoint.

int mid = start + (end - start)/2; //Integer division

//Stop condition. if (start > end) return -1;

else if (data[mid] == toFind) //Found?

return mid;

else if (data[mid] > toFind) //Data is greater than toFind, search lower half

return binary_search(data, toFind, , );

else //Data is less than toFind, search upper half

return binary_search(data, toFind, , );

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

What is the effect of word war second?

Answered: 1 week ago