Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

An Introduction to Programming and Algorithm Development | CS 340, Exams of Computer Science

Material Type: Exam; Professor: McNelis; Class: Intro Scientific Comp; Subject: Computer Science; University: Western Carolina University; Term: Spring 2006;

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-xk0-1
koofers-user-xk0-1 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 340 Introduction to Scientific Computing
An Introduction to Programming and Algorithm Development
Thursday, March 16, 2006
1 Variables
In mathematics, we say that a quantity is variable (adjective) if it’s value changes. We also refer to the
symbol used to represent that changing value as a variable (noun). In computer science, we use the term
variable to represent a name, or more precisely a location, where a piece of information is stored. For the
most part, variables can be classified as one of three main types:
numbers (integers and floating point numbers)
characters
boolean (logical)
These data types might have modifiers such as short, long, signed, or unsigned that deal with the number
of bits used and representation the variable in memory.
For instance, the table below gives the main data types or primatives in a variety of programming
languages. For such languages, the variable must be declared to be of a specific type and/or size before it
can be used. In dynamic typing softwares such as MATLAB and Python, the type of variable is determined
by the context of it’s initial use.
A Comparison of Fundamental Data Types
Data Type C C++ Fortran Java
16-bit Short Integer short short INTEGER*2 short
32-bit Integer int int INTEGER or INTEGER*4 int
64-bit Long Integer long long n/a INTEGER*8 long
32-bit IEEE Floating Point Number float float REAL or REAL*4 float
64-bit IEEE Floating Point Number double double DOUBLE PRECISION or REAL*8 double
8-bit Character char char CHARACTER char
Boolean bool bool LOGICAL boolean
1.1 Boolean Variables
Definition 1 (Boolean Variable)
Aboolean variable is a variable that has the value of either true or false. Typically, when working with
boolean variables and boolean expressions (expressions that are either true or false), the number 1 is used
to represent “true” and the number “0” is used to represent false.
Recall the truth tables for AND and OR from basic logic
X Y X AND Y
T T
T F
F T
F F
X Y X OR Y
T T
T F
F T
F F
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download An Introduction to Programming and Algorithm Development | CS 340 and more Exams Computer Science in PDF only on Docsity!

CS 340 – Introduction to Scientific Computing An Introduction to Programming and Algorithm Development Thursday, March 16, 2006

1 Variables

In mathematics, we say that a quantity is variable (adjective) if it’s value changes. We also refer to the symbol used to represent that changing value as a variable (noun). In computer science, we use the term variable to represent a name, or more precisely a location, where a piece of information is stored. For the most part, variables can be classified as one of three main types:

  • numbers (integers and floating point numbers)
  • characters
  • boolean (logical)

These data types might have modifiers such as short, long, signed, or unsigned that deal with the number of bits used and representation the variable in memory.

For instance, the table below gives the main data types or primatives in a variety of programming languages. For such languages, the variable must be declared to be of a specific type and/or size before it can be used. In dynamic typing softwares such as MATLAB and Python, the type of variable is determined by the context of it’s initial use.

A Comparison of Fundamental Data Types

Data Type C C++ Fortran Java 16-bit Short Integer short short INTEGER2 short 32-bit Integer int int INTEGER or INTEGER4 int 64-bit Long Integer long long n/a INTEGER8 long 32-bit IEEE Floating Point Number float float REAL or REAL4 float 64-bit IEEE Floating Point Number double double DOUBLE PRECISION or REAL*8 double 8-bit Character char char CHARACTER char Boolean bool bool LOGICAL boolean

1.1 Boolean Variables

Definition 1 (Boolean Variable) A boolean variable is a variable that has the value of either true or false. Typically, when working with boolean variables and boolean expressions (expressions that are either true or false), the number 1 is used to represent “true” and the number “0” is used to represent false.

Recall the truth tables for AND and OR from basic logic

X Y X AND Y T T T F F T F F

X Y X OR Y

T T

T F

F T

F F

Figure 1: Illustrating the Scope of a Variable in MATLAB

1.2 Variable Scope

By the scope of a variable, we mean the depth of the levels to which a variable’s value can be accessed and/or changed. By default, all variables are local, meaning that they are only accessible from the level or layer of the program in which they were created. For instance, in MATLAB, the command window is considered the top most layer. Variables defined at the command line are only “known” or available at that level of MATLAB. To share them with some other level, such as inside a function, they must be passed to the function as input. We can also have functions that call functions.

Consider the variable data in the function calc mean in Figure 1. It is defined only in the calc mean function. Once the calc mean function has completed it’s calculations and returned the value of xbar, references to its local variables are destroyed. The information has been passed back to it’s calling function, calc stats and named mu.

In the case that we do want a variable to have more than a strictly local scope, we can define it to be global, meaning that all functions that with the global variable name within them can recognize and alter the value of that variable. For the programming we will consider, we will not make use of global variables often.

3 Conditional Statements and Looping

A conditional statement is an structure that allows a set of commands to be executed if and only if the condition specified for their execution is true. Each of these conditions that determines if the set of commands is executed is a logical expression, meaning it will evaluate to either TRUE or FALSE. The classic example of a condition statement is an “if · · ·, then · · ·”. This is seen in mathematical theorems as well as computer programming.

3.1 Logical Operators

Before discussing the conditional statements and their structure, it is important to note the logical expres- sions and typical logical operators used in these expressions. Again, a logical expression is one that can be evaluated to either a value of true (associated with a bit value of 1) or false (associated with a bit value of 0).

The primary relational operators are as follows:

Relation Less than Greater than Less than or Greater than or Equal to Not Equal to Equal to Equal to Logic Symbol < > ≤ ≥ = 6 = MATLAB Syntax < > ≤ ≥ == ∼=

The logical operators are:

Logical Operator AND OR NOT MATLAB Syntax & | ∼

Note, just as with mathematical operations, inequalities and logical operators have a precedence described by the order of operations:

Order of Operations (Precedence decreases moving to the right · · ·) Highest Lowest Parenthesis Exponents NOT Multiplication Addition and Relational AND OR and Division Subtraction Operators <, >, ≤, ≥, =, 6 =

Example 2 (Logical Examples) Evaluate the following:

  1. ∼ (4 > −2)|(abs(−3) == 3)
  2. (6 < 10)&(7 > 8)|(5 ∗ 3 ∼= 60/4)

3.2 Conditional Statements

A conditional statement consists of two primary pieces: a test of a boolean expression and a set of commands that are executed if the boolean expression is true. In flow charts, the tests of logical expressions are treated as decisions, where decisions are either “yes” (true or 1) or “no” (false or 0), and a set of commands is executed only if the result of the boolean expression leads to that set (typically if the expression evaluates to be true).

3.2.1 IF ... THEN ... Statements

As stated earlier, the “if ..., then ...” statement is the classical example of a conditional statement. A logical expression is tested in the “if” portion of the statement, and if that expression is true, the prescribed set of actions is carried out, otherwise these actions are skipped. NOTE: In and of itself, an if then statement is only executed once!

MATLAB Syntax for Flow Chart for IF ... THEN Statement IF ... THEN Statement

.... MATLAB Program ....

if logical expression .... .... MATLAB Commands .... end

.... MATLAB Program ....

Example 3 (IF ... THEN Example) Suppose you’re writing a program to determine whether a special discount comes into effect for shipping an order of books from an online book store. A holiday special states that if the person buys over $50 worth of books, shipping is free. A MATLAB function that determines whether the this special is in effect could look like:

function in_effect = ShippingSpecial(Sales) % This function determines if a special on shipping applies to a % particular dollar value of sales. The ‘‘Sales’’ parameter gives % the amount of spent on book sales.

in_effect = 0; % Set ‘‘in_effect’’ to false as default result.

if Sales >= 50 in_effect = 1; end;

3.3 Looping

As we saw in the “Pick a number” example, repetition of a set of actions can be common place in an algorithm. This repetition is handled through a looping process, where a set of commands to be executed are guarded by a boolean expression. Like an IF statement, the commands are only executed if the boolean expression is true. If the conditional statement is false, these commands are ignored and the remainder of the program following the loop is executed. Unlike the IF statement, as long as the boolean expression is determined to be true, the interior commands are executed and the flow of the program returns to the top of the loop to reevaluate the conditional statement.

3.3.1 WHILE ... Loops

The WHILE loop repeats execution as long as the boolean expression following the “while” evaluates to be true. The chart below illustrates the procedural flow associated with a WHILE loop. Note, in order to cease execution of the WHILE loop, i.e. the value of the boolean expression changes from true to false, the variable involved in the boolean expression must have a chance of being altered in value in the commands within the loop.

Flow Chart for WHILE Loop MATLAB Syntax for WHILE Loop

.... MATLAB Program ....

while logical expression .... .... MATLAB Commands .... end

.... MATLAB Program ....

Example 5 (WHILE Loop Example) A fixed point of a function f (x) is a point (x, y) such that f (x) = x (i.e. y = x). In order to find a fixed point, one can pick an initial point assumed to be close in value to the fixed point, call this initial value x 0 , and generate a sequence of points x 1 , x 2 , x 3 , · · · , xk where

xk = f (xk− 1 ) for k = 1, 2 , 3 , · · ·

If x 0 was a good initial choice, this sequence of x-values will converge to a fixed point of f (x). We test for convergence by checking that the difference (in absolute value) between successive iterates, xi− 1 and xi, is small; perhaps less than 10 −^5. The following MATLAB code illustrates this process:

function fp = FixedPoint(fcn,x0) % Given a function, f(x), specified in the function file, fcn.m and passed % to the routine by the user, and an initial point x0, this program will % iterate the points x1, x2, x3, ... until a fixed point is found. NOTE: if % these points don’t converge, the WHILE loop is NEVER exited and we enter % an infinite loop, so pick wisely. In the case of an infinite loop, press % CTRL + C or CTRL + BREAK to stop the loop.

% Define a boolean variable that indicates when the fixed point is found % Set it to ‘‘false’’ initially. FoundIt = 0;

while ~FoundIt next_x = feval(fcn,x0); if abs(next_x - x0) < 10^(-5) FoundIt = 1; fp = next_x; else x0 = next_x; end; end;

3.3.2 FOR ... Loops

When we need to execute a particular set of commands for a fixed number of times, we particularly rely on a FOR loop. The boolean condition in the FOR loop does not appear to be a question that evaluates to a true or false answer necessarily. Instead, the entering statement for a FOR loop looks like the following:

An index variable, denoted by i in this case, determines how often the loop is traversed. Each time through the body of the FOR loop, the index variable has a new value. These values are automatically incremented by the step size specified in the loop statement at the end of the commands in the FOR loop. If a step size is not specified, the default step size is taken to be “1”. Once the index is incremented beyond the final value, the execution of the FOR loop stops and the program continues on with the remainder of the commands in the program.

FOR Loop WHILE Loop

.... MATLAB Program ....

for k = a:step:b .... .... MATLAB Commands .... end

.... MATLAB Program ....

.... MATLAB Program .... k = a;

while k <= b .... .... MATLAB Commands .... k = k + step; end

.... MATLAB Program ....

3.3.3 Break Statements

Sometimes it is desirable to exit a WHILE or FOR loop before the interior commands are completed or before the boolean expression is false. For instance, if you have a set of commands you want to execute a fixed number of times, provided that some quantity doesn’t grow to small. In this case you would want to break out of the loop. MATLAB allows the user to do this with a break statement. While inside a FOR or WHILE loop, the break command terminates the execution of the loop (the entire loop, not just that pass through the loop). Breaking the loop returns the flow of the program to the position in the code immediately after the end statement for the loop that was terminated.

Example 7 (Using a break Statement) Suppose you needed to write a command that approximated π to n decimal places. For computational time, you put a limit on the number of times you will go through the FOR loop to calculate this sum. If you have reached the desired accuracy before this maximum number of iterations, break out of the loop:

function approx = ApproxPi(n) % Use the first n terms of the infinite series for Pi to find % an approximation for Pi.

old_approx = 0; MaxIter = 1000;

for k=0:MaxIter approx = old_approx + (-1)^k 1/(2k + 1); if abs(approx - old_approx) < 10^{-(n+1)} break; else old_approx = approx; end; end;

3.3.4 Nested Loops

As the previous examples have shown, it is often desirable to include, or nest, conditional statements within WHILE or FOR loops. It is also common to nest loops. The code below performs Gaussian Elimination on a system Ax = b to find the solution vector x:

Example 8 (Naive Gaussian Elimination)

function [x,adds,mults,compares] = NaiveGaussLE(A,b); % Note, this is the less efficient (LE) method of naive Gaussian % elimination. Less efficient in terms of space and time because % we physically swap rows and zero out elements.

[m,n]=size(A); [p,q]=size(b);

if m~=n error(’The A matrix must be square, yours is %d by %d.’,m,n); elseif (q~=1|n~=p) error(’The second argument must be a vector of appropriate size.’); end;

% Set up the vector x that will hold our solution eventually x = zeros(n,1);

adds = 0; mults = 0; compares = 0;

for i=1:n-1 % For each row (pivot element) % If the pivot element is zero, go down the column until you find a % nonzero element, then swap that row with the pivot row. if A(i,i) == 0; nonzero_pivot = 0; %False, we have not found our pivot yet for pivot = i+1:n if A(pivot,i) ~= 0 dummy = A(i,:); dummy_b = b(i); A(i,:) = A(pivot,:); b(i) = b(pivot); A(pivot,:) = dummy; b(pivot) = dummy_b; nonzero_pivot = 1; %True, we have a nonzero pivot now break; end; end; if nonzero_pivot == 0 % all elements on and below diag are 0! error(’This system does not have a unique solution. Thus this method will not help.’); end; end;

printed in a specific numerical format (integer, floating point number, string, etc.). A call to fprintf to print to the command window has the following syntax:

fprintf(formatted string,variables in string)

For example, to print out the dimensions of a matrix A, we could use code like:

[m,n]=size(A); fprintf(’The matrix is %d by %d \n’, m, n);

Note that a string in MATLAB (and in C) is indicated by the use of single quotes, informing MATLAB that the text inside the single quotes is to be interpreted as a string of characters rather than executable commands.

4.1.1 Formatting Strings

In order to indicate that a portion of the output has a special numerical representation, a conversion spec- ification is used within the character string as a place holder for this numerical information. A conversion specification controls the notation, alignment, significant digits, field width, and other aspects of output format. The formatted string can contain escape characters to represent non-printing characters such as newline characters and tabs. Note that information about the conversion specification and presented here has been taken directly from the MATLAB on-line help for fprintf.

Conversion specifications begin with the % character and contain these optional and required elements:

  • Flags (optional),
  • Width and precision fields (optional)
  • A subtype specifier (optional)
  • Conversion character (required)

You specify these elements in the following order:

Flags You can control the alignment of the output using any of these optional flags.

Character Description Example Minus sign (-) Left-justifies the converted argument in its field. %-5.2d Plus sign (+) Always prints a sign character (+ or -). %+5.2d Zero (0) Pad with zeros rather than spaces. %05.2d

Field Width and Precision Specifications You can control the width and precision of the output by including these options in the format string.

Character Description Example Field width A digit string specifying the minimum number of digits to be printed.

%6f

Precision A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point.

%6.2f

Conversion Characters Conversion characters specify the notation of the output.

Specifier Description %c Single character %d Decimal notation (signed) %e Exponential notation (using a lowercase e as in 3.1415e+00) %E Exponential notation (using an uppercase E as in 3.1415E+00) %f Fixed-point notation %g The more compact of %e or %f. Insignificant zeros do not print. %G Same as %g, but using an uppercase E %i Decimal notation (signed) %o Octal notation (unsigned) %s String of characters %u Decimal notation (unsigned) %x Hexadecimal notation (using lowercase letters a-f) %X Hexadecimal notation (using uppercase letters A-F)

Escape Characters This table lists the escape character sequences you use to specify non-printing characters in a format specification.

Character Description \b Backspace \f Form feed \n New line \r Carriage return \t Horizontal tab \ Backslash \’’ or ’’ (two single quotes) Single quotation mark %% Percent character

4.2 Error Statements

MATLAB has a special command for printing error statements to be printed to the screen: the error command. Essentially, the error command is an fprintf command that also stops execution of the program at that point in the code. For example, error statements can be used to notify the user that they have entered the wrong type or size of data:

[m,n]=size(A);

5 Variables and Arrays

In addition to representing mathematical objects like matrices and vectors, we can use the same type of structure to store data. In most languages, rather than use the term matrix (which has a distinct mathematical connotation, we refer to these structures as array. We can have single dimensional arrays, two dimensional arrays, or multi-dimensional arrays (considered to be arrays of arrays).

We refer to a particular item in storage by referring to the name of the array we’ve stored it in, its location (it’s index) in the array. Typically we see 1-D arrays which are indexed like vectors, like the one shown below, and 2-D arrays which are indexed like matrices.

The name of the 1-D array below is ED, and to refer to the third element in ED we must reference ED(3). Note, the syntax depends on the language you’re using. NOTE: MATLAB starts its indexes with the value of 1 and goes up, though some languages start indexing with 0.

When programs go through an iterative process, creating a sequence of values along the way, it is sometimes helpful to keep track of all of the values rather than overwriting old values. We can use arrays for this purpose, assigning each new value of the sequence to its own position in the array. MATLAB is particularly nice for this as it uses dynamic variables, meaning that variables are created or adjusted according to how they are used in the program. To create a dynamic array in MATLAB, one in which you keep appending new values to the end of the array, you simply refer to the next position in the array. If it does not already exist, MATLAB will extend the array to handle this situation. For instance, if you have a vector called xvals that has four entries [1, 1. 5 , 1. 625 , 1 .6875] and you want to append the value of 1.8 to the end of this array, simply use the command xvals(5) = 1.8, and the xvals array will be automatically updated to look like:

xvals = [1, 1.5, 1.625, 1.6875, 1.8]

NOTE: This will not be the case with all languages. We will typically have to create an empty array large enough to hold the data we intend to generate, or go through a process that allows us to build arrays as we go. We will discuss this more when we talk about programming in C and Fortran.

Example 9 (Fixed Point Iteration) A fixed point of the function f (x) is the point (x∗, y∗) = (x∗, f (x∗)) where f (x∗) = x∗, i.e. your input matches your output (or y = x). Graphically, we can find the fixed point of a function by finding the locations where the graph of f (x) crosses the curve y = x.

Given an initial x-value, x 0 , the fixed point iteration generates a sequence of points x 1 , x 2 , x 3 , · · · that, if x 0 is chose wisely, approach a fixed point of the function f (x). These x-values are generated using the iterative formula:

xk = f (xk− 1 ) for k = 1, 2 , 3 , · · ·

We know that the sequence of x-values is converging on a fixed point when we see little change between the values. Quantitatively we can measure this as

|xk − xk− 1 | < tol

where tol is a very small number.

Putting this together we can create a program that, given a function, f (x), and an initial value x 0 , will create the sequence of numbers x 1 , x 2 , x 3 , · · ·, stopping when xk− 1 and xk are sufficiently close to each other:

function [fp, xvals] = FixedPoint(fcn,x0)

xvals(1) = x0;

% Define MaxIt to indicate the maximum number of iterations to carry out without % finding a fixed point to within 5 decimal places.

tol = 10^(-5); MaxIt = 100;

for i=1:MaxIt xvals(i+1) = feval(fcn,xvals(i)); if abs(xvals(i+1) - xvals(i)) < tol fp = xvals(i+1); break; end; end;

if i > MaxIt error(’Could not find a fixed point correct to %g within %d iterations.\n’, tol, MaxIt) end;