CSCI-100 Introduction to Computing Algorithms Part IV
Categories of operations Sequential operations Carry out a single well-defined task; when that task is finished, the algorithm moves on to the next operation Add 1 cup of butter to the mixture in the bowl Subtract the amount of the check from the current account balance Conditional operations Ask a question and then select the next operation to be executed on the basis of the answer to that question If the mixture is too dry, then add one-half cup of water to the bowl Iterative operations Tell us to go back and repeat the execution of a previous block of instructions Repeat previous operations until mixture has thickened
Sequential Operations
Sequential Operations Examples (DONE IN CLASS) Write pseudocode for an algorithm that gets three data values x, y, and z as input and outputs the average of those three values Write pseudocode for an algorithm that gets the radius r of a circle as input. Its output is both the circumference and the area of a circle of radius r
Conditional and Iterative Operations
Conditional and Iterative Operations
Conditional and Iterative Operations Examples (DONE IN CLASS) Write an if/then/else statement that sets the variable y to the value 1 if x ≥0. If x < 0 then the statement should set y to the value 2 Write an algorithm that gets as input three data values x, y, and z and outputs the average of these values if the value of x is positive. If the value of x is either zero or negative, your algorithm should not compute the average but should print the error message “Bad Data” instead
Example (DONE IN CLASS) Implement an algorithm to multiply two numbers, a and b, using repeated addition Algorithm outline Create a loop that executes exactly b times, with each execution of the loop adding the value of a to a running total (DONE IN CLASS)