For each of the functions described in questions 1 to 7 you are to: 1. Modify the function to count the number of operat
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
For each of the functions described in questions 1 to 7 you are to: 1. Modify the function to count the number of operat
questions 1 to 7 you are to: 1. Modify the function to count the number of operations performed when the function executes in the worst case. See the section on counting operations below for more detail. 2. Determine a detailed cost function for the function. This function should be written in the form wn* + yn + z where w, x, y and z are real numbers and n is a variable referring to the size of the function's input. If necessary, you should adapt this format to include other terms such as log₂(n). 3. Identify all of the barometer operations for the function. 4. Write the O notation running time. There is a detailed example after the last question. Counting Operations Augment each function with an integer reference parameter to maintain a count of the number of operations. This reference parameter should always be the last parameter in the function's parameter list. When counting operations follow these rules - make sure you read them carefully (and follow them). 1. An executable statement (a line of code that ends in a semi-colon) or a condition (that controls an if statement or while loop) or a loop increment statement (like i++ in the third field of many for loops) counts as one (1) operation regardless of its complexity. This includes input or output instructions. Statements that include one or more function calls will count as more than one operation, as detailed by rule 2. • Examples: x = 4; cout << "Hello world!\n"; int w = x+y+z; (x 0) { i--; int j = 0; while (j <= i) { cout << j << " "; j++; } cout << endl; Question 3 This function returns an array in dynamic memory that represents the matrix that is the result of multiplying the matrix (array) parameter by itself. // PRE: m represents a square matrix of size rows * rows // PARAM: rows represents the number of both rows and columns // POST: Returns a pointer to an array (matrix) of the same size as m // NOTE: values are indexed rece, rec1,...,r@cn-1,rice,... int matrixselfMultiply(int* m, int rows) { // Create result array int columns = rows; int* result = new int[rows columns]; int r = 0; while (r< rows) { int c = 0; r++; while (c 0) { } } // 1search pattern (n/2, 1); // Print i spaces cout << string(i,' '); // A loop to print n asterisks, each one followed by a space int ast = 0; while (ast 0) { if (exp & 1) { } // pow } } exp >>= 1; base base base; Notes 1. The operation count is related to the binary representation of the exponent. If you are unable to find a precise, closed form, cost function then give a good upper bound. 2. The & (bitwise AND) and >>= (right shift assignment) are bitwise operators. If you are unfamiliar with these please look them up (Google). Example ret = base; return ret; This function computes the sum of squares of its array parameter. int sumsquares (int arr[], int n) { } int i = 0; int sum = 0; } while (i
For each of the functions described in