340 likes | 532 Views
Computational Tools Structured Programming Spreadsheets. Structured Programming Flowcharts Flow control Functions. Flowchart Symbols. Start/Stop. Operation. Decision. Input/Output. Example Flowchart. start. read x. y = f(x). yes. y > 4?. y = 4. no. print y. end.
E N D
Computational Tools • Structured Programming • Spreadsheets Computational Tools
Structured Programming • Flowcharts • Flow control • Functions Computational Tools
Flowchart Symbols Start/Stop • Operation • Decision • Input/Output Computational Tools
Example Flowchart start read x y = f(x) yes y > 4? y = 4 no print y end Computational Tools
Flowchart with Iteration start sum = 0 n = 0 n = n + 1 sum = sum + n n=5? no yes print sum end Computational Tools
When the program represented by the following flowchart is run, what is the resulting sum? 10 15 20 21 start sum = 0 n = 0 n = n + 1 sum = sum + n n=5? no yes print sum sum = 15 B end Computational Tools
Structured Programming • Flowcharts • Flow control • Functions Computational Tools
In this program, if a value of 0.5 is read for x, what value is assigned to y? read x if x < 0 then y = 0 else if x > 1 then y = 1 else y = x*x end 0.25 0.5 0.75 1 A Computational Tools
In this program, what is the final value of x? x = 1 y = 2*x x = x + y*y if x > 4 then x = x/5 1 5 6 8 x = 1 y = 2 x = 5 x = 1 A Computational Tools
In this program, if a value of 0.5 is read for x, what is the final value of x? read x if x > -1 and x < 1 then x = 0 else x = x*x 0 0.25 0.5 1 A Computational Tools
In this program, if a value of 3 is read for n, what value is assigned to x? read n switch n case 1 x = 2 case 2 x = 4 case 3 x = 6 otherwise x = 8 end 3 4 6 8 x = 6 C Computational Tools
In this program, what is the final value of y? x = 1 y = 1 for n = 1 to 3 x = x + 2 y = y*x end 7 15 105 945 y = 105 C Computational Tools
In this program, what is the final value of y? x = 0 y = 0 while x 6 x = x + 2 y = y + x end 8 10 12 20 y = 20 D Computational Tools
What formula is implemented by this program? read x, m s = 1 t = 1 for k = 1 to m t = t*x/k s = s + t end • s = 1 + x + x/2 + x/3 + … + x/m • s = 1 + x/1! + x/2! + x/3! + … + x/m! • s = 1 + x/1 + x2/2 + x3/3 + … + xm/m • s = 1 + x/1! + x2/2! + x3/3! + … + xm/m! s = 1 + … k = 1; t = x; s = 1 + x + … k = 2; t = x2/2; s = 1 + x + x2/2 + … k = 3; t = x3/(2·3); s = 1 + x + x2/2 + x3/3! + … D Computational Tools
Structured Programming • Flowcharts • Flow control • Functions Computational Tools
Function • Main Program: Function: • a = 0; x = 1; y = 2 function y = special(x) • d = special(a) y = x + 1 • Print d return y • Notice that: • The function input is named a in the main program and x in the function. • The function output is named d in the main program and y in the function. • The variables x and y in the function do not affect the variables x and y in the main program. • The only effect of the function is to set d equal to a + 1. Computational Tools
The following program is run. It uses the function simplefunction, which is also shown below. What is the value of h at the end of the program? g = 3; h = 5 p = simplefunction(g) function y = simplefunction(x) h = 7 y = x + h return y 12 10 5 7 • The function simplefunction returns 10 to the main program. The fact that the function uses h as the name of a local variable does not affect the h in the main program. The variable h in the main program remains 5. C Computational Tools
The following program is run. It uses the function squareof, which is also shown below. What is the value of s at the end of the program? read x s = 1 y = squareof(x) s = s + y function s = squareof(x) s = x*x return s x2 + 1 x2 2x2 This program will result in an error message. • The function squareof returns x2 to the main program. The fact that the function uses the name s for the square does not affect the s in the main program. The assignment statement s = s + y produces x2+ 1. A Computational Tools
Spreadsheets • Formulas • Copying and Pasting Formulas Computational Tools
Formulas in Cells Computational Tools
Colon Computational Tools
Spreadsheets • Formulas • Copying and Pasting Formulas Computational Tools
Absolute and Relative Addresses Computational Tools
Copy and Paste a Cell: $A$1 Computational Tools
Copy and Paste a Cell: A1 Computational Tools
Copy and Paste a Cell: $A1 Computational Tools
Copy and Paste a Cell: A$1 Computational Tools
Copy and Paste a Column Computational Tools
Copy and Paste a Row Computational Tools
In a spreadsheet, the number in cell A3 is 4. Cell A4 contains the formula A3 + $A$3. This formula is copied into cells A5 and A6. What is the value of cell A6? A. 4 B. 8 C. 16 D. 32 C Computational Tools
In a spreadsheet, the formula $A$3 + $B3 + D2 is entered into cell C2. The contents of cell C2 are copied and pasted into cell D5. The formula in cell D5 is: A. $A$3 + C$2 + C4 B. $B$6 + $C4 + C4 C. $A$3 + $B6 + E5 D. $A$3 + $B2 + B2 cell C2: $A$3 + $B3 + D2 • cell D5: $A$3 + $B6 + E5 C Computational Tools
A spreadsheet contains the series of numbers 5,10,15,… in cells C2:C7. Cell D3 contains the formula (2*C2)+7. If the formula is copied and pasted into cells D4:D8, what is the numeric value in cell D8? 67 55 93 77 The formula (2*C2)+7 in cell D3 employs the number in the cell one column to the left and one row up. • In cell D8, this formula employs the number in the cell one column to the left and one row up (cell C7). This number is 30. (2*30)+7 = 67 A Computational Tools
In this spreadsheet, the contents of column B are copied and pasted into columns C and D. What numeric value will cell D4 have? 32 64 96 128 C Computational Tools
In this spreadsheet, the cell D1 contains the formula (A1+B1+C1)/3. This formula is copied into the range of cells D2:D3. Cell D4 contains the formula SUM(D1:D3). What is the numeric value in cell D4? 15 12 14 18 A Computational Tools