IB CS M22

Ace your homework & exams now with Quizwiz!

4d. State the output from the given tree using inorder tree traversal.

(5+2)*(25−16)/3

2. Outline the need for a translation process from high level language to machine code.

(The program written in HLL must be translated into machine code) so that the computer can execute the program;as the computer only understands machine language / as code written in HLL can only be understood by humans and cannot be interpreted by the computers (which work in binary);

Exam 2

----

7. List the output from the given algorithm for the following input. 2, 6, 8, 9, 12, 15, 18, 20 loop for Count from 0 to 7 input NUMBER if NUMBER div 2 = NUMBER / 2 then if NUMBER div 3 = NUMBER / 3 then output NUMBER end if end ifend loop

6; 12; 18;

4b. Explain why a stack is used in the process of evaluating the expression in the algorithm.

A stack is a last in first out (LIFO) / first in last out (FILO) data structure;...which means data is popped off the stack in the reverse order to which it was pushed;In the expression, it is important to evaluate some of the values in a certain order(to obtain the correct result);

3a. Outline why a sub-program is considered an example of abstraction.

A sub-program is a named section of code that performs a specific task (in a program) / can be called by name / referred by the identifier when needed;without knowing the details (of code and data structures) as these are wrapped / hidden within the sub-program;

2c. Describe the process a binary search would follow to find a record in the surname array.

Calculate the index of the middle point in the array SURNAME: (first + last)/2; Compare surname found with the one stored at middle point; If greater than the value at the middle point, search the upper half of the array (right side) by calling the binary search method again with a new first index (first = middle + 1); If smaller than the value at the middle point, search the lower half of the array (left side) by calling the binary search method with a new last (last = middle −1);

5c. Describe the process a binary search would follow to find a record in the surname array.

Calculate the index of the middle point in the array SURNAME: (first + last)/2;Compare surname found with the one stored at middle point;If greater than the value at the middle point, search the upper half of the array (right side) by calling the binary search method again with a new first index (first = middle + 1);If smaller than the value at the middle point, search the lower half of the array (left side) by calling the binary search method with a new last (last = middle −1);

6b. State the purpose of the algorithm.

Converts denary number to (reverse) binary.

1e. Explain how you would change your algorithm in part (d) to allow a user to choose a grade and output the names and marks of the students who have achieved this grade.

G=input() COUNTER = 0 loop while COUNTER < 30 if GRADE[COUNTER] = G then output(NAME[COUNTER], MARK[COUNTER]) end if COUNTER = COUNTER + 1end loop

2b. State the constant used in Algorithm 1.

LIMIT

5a. A company has 600 employees whose names are currently stored using a collection called NAMES. The names are stored as surname, first name. For example: Smith, Jane, Uysal, Rafael, Ahmed, Ishmael, Jonsonn, Sara, ... a. Construct a pseudocode algorithm that will store the surnames in one array and first names in another.

NAMES.resetNext() loop COUNTER from 0 to 599 SURNAME[COUNTER] = NAMES.getNext() FIRSTNAME[COUNTER] = NAMES.getNext() end loop

A company has 600 employees whose names are currently stored using a collection called NAMES. The names are stored as surname, first name. For example: Smith, Jane, Uysal, Rafael, Ahmed, Ishmael, Jonsonn, Sara, ... 2a. Construct a pseudocode algorithm that will store the surnames in one array and first names in another.

NAMES.resetNext() loop COUNTER from 0 to 599 SURNAME[COUNTER] = NAMES.getNext() FIRSTNAME[COUNTER] = NAMES.getNext() end loop

8b. Outline how the error in the algorithm identified in part (i) can be corrected.

Replace LOWEST = 0;With LOWEST=99999999/ any very high number;

3b. Construct a similar sub-program to the one in part (i) to calculate and return the area of the shape.

SUB_AREA(PERIM, APOTHEM) AREA = PERIM * APOTHEM / 2 r eturn AREA end SUB_AREA

3a. The flowchart below represents an algorithm that allows a user to enter the number of sides, length of side and apothem (see diagram below) for a regular polygon. It then calculates and outputs the area and perimeter of this shape. Construct pseudocode for the body of the following sub-program to calculate and return the perimeter of the shape.

SUB_PERIMETER(NUM_SIDES, LENGTH_SIDE) //Pseudocode to be added return NUM_SIDES * LENGTH_SIDE End SUB_PERIMETER

2e. Evaluate the use of a sequential search and a binary search including the advantages and disadvantages of each.

Sequential Search: The algorithm searches through every element in the array starting at the beginning and working through one after the other; This method is potentially slow if the data set is large and the required element is towards the end of the list;The data does not need to be in any particular order; Binary Search: The data must be sorted before it is stored;After comparing the data set with the target data, half of the data can be discounted using a simple condition;...enabling the target to be more quickly located;

4c. Outline the steps involved in traversing the given tree using postorder tree traversal.

Start from the root node;If the root is null, return immediately;Traverse left subtree;Traverse right subtree;Visit root;

8a. The flowchart contains a logic error that will affect the algorithm's functionality. 8a. Identify the logic error in the algorithm.

The algorithm will always output zero as the value of LOWEST, for any set of the input values (because all input values are greater than or equal to 0);

2d. Outline why MID_VAL could not be a constant.

The value (of MID_VAL) changes during the operation of the algorithm;

6. Distinguish between a variable and a constant.

The value of a variable can change while value of a constant does not change;During program execution/ during run-time / while stored in the memory;

2d. Outline one benefit of using sub-programmes to implement your algorithms from parts (a) and (b).

These sub-programs (sorting/searching) could be used in (many) other programs;Which saves programmer's time/ effort;

5d. Outline one benefit of using sub-programmes to implement your algorithms from parts (a) and (b).

These sub-programs (sorting/searching) could be used in (many) other programs;Which saves programmer's time/ effort;

1c. Outline how the name, mark and grade in the three arrays correspond to the same student.

Three arrays are parallel/ they have the same number of elements/ the same length;the same array index can be used to represent name, grade and mark of the same student/ the array index makes sure that data from the three arrays lines up;

6d. Suggest two design considerations that could be made to an algorithm that would make it more efficient.

Use of arrays/data structures;So data can be stored/re-used/re-entered; The algorithm could use loops;To remove the necessity to process extra lines of code;

3d. Without using pseudocode, explain how your algorithms could be altered to also find the area and circumference of a circle.

ask user to input if the shape is circle and use if statement to check this input value;if not circle then code for the polygon (as written in part a);otherwise (it is circle!) input radius;calculate area;and circumference using formulas given in the question;

A teacher would like a simple program to store the names, marks and grades of students in a set of three parallel one-dimensional arrays called NAME[], MARK[] and GRADE[] . 1a. Identify two components in a conditional statement.

if; then; else;

1d. Construct an algorithm using pseudocode to output the names and grades of all students who achieve a grade of Merit or Distinction.

loop COUNTER from 0 to 29 if MARK[COUNTER] >= 60 then output NAME[COUNTER], GRADE[COUNTER] end if end loop

1b. Construct an algorithm using pseudocode to take the marks that have been stored in MARK[], convert them into the appropriate grade and store the calculated grades in GRADE[].

loop COUNTER from 0 to 29 if MARK[COUNTER] >= 80 then GRADE[COUNTER] = "Distinction" else if MARK[COUNTER]>˝= 60 then GRADE[COUNTER] = "Merit" else if MARK[COUNTER} >= 40 then GRADE[COUNTER] = "Pass" else GRADE[COUNTER] = "Fail" end if end if end if end loop

5b. Construct a pseudocode algorithm that will sort the surnames into alphabetical order using the bubble sort method. The order of the first names must also be changed so that they keep the same index as their corresponding surname.

loop I from 0 to 599 loop C from 0 to 598-I if SURNAME[C] > SURNAME[C + 1] then TEMP1 = SURNAME[C] TEMP2 = FIRSTNAME[C] SURNAME[C] = SURNAME[C + 1] FIRSTNAME[C] = FIRSTNAME[C + 1] SURNAME[C + 1] = TEMP1 FIRSTNAME[C + 1] = TEMP2 end if end loop end loop


Related study sets

Chapter 6: Strengthening the New Nation

View Set

Unit 9 Test - New Imperialism (includes all Shane quiz questions)

View Set

AC LO 1-1, 1-2, 1-3, 1-4 Homework

View Set

Chapter 1-3 LearnSmart & Quizzes

View Set

Teaching and Learning/Patient Education PrepU

View Set

Hands-On Ethical Hacking and Networking Defense

View Set

6.1 - 6.10 Identity, Access, and Account Management

View Set