*Ian D. Allen*

*Rideau B215A*

idallen@freenet.carleton.ca

Focused on Your Career

Customized Training

Workplace Skills

Quality Instruction

B.A.

Honours Psychology

University of Waterloo

1980

MMath

Computer Science

University of Waterloo

1985

Ian D. Allen

idallen@freenet.carleton.caRideau B215A

Telephone (727-4723) x5949

Office hours

see my office door

Notes and Course Outlinesonline:

http://idallen.com/teaching/ http://www.algonquincollege.com/cst/8110/in library, if needed

Your Student Numbermarks are grouped by number

Your Course Number

CST8110 section 010 - Ian Allen

Course TimesLectures: two hours / week

010 Mon 14:00-14:50 RB163

Fri 11:00-11:50 RB163Labs: two hours / week

011 Wed 14:00-15:50 RC205

012 Thu 11:00-12:50 RC204

013 Thu 14:00-15:50 RC205

course outlines are available online

please review academic policies

withdrawing

repeating courses

probation

academic discipline

Lectures2 hours Class

2 hours Lab (mandatory)

Evaluation / Earning Credit35% in-class tests & quizzes

40% final examination

25% assignments

Assignment Late Penaltyup to one week ...

–20%after one week ...

–100%

A:80-100

An outstanding level of achievement has been consistently demonstrated.

B:70-79

Achievement has exceeded the required level.

C:60-69Course requirements are met.

D:50-59Achievement is at a marginal level. Consistent, ongoing effort is required for continuing success in the program.

Midterm #1in class

Monday, June 2, 1997

Midterm #2in class

Friday, July 4, 1997

Statutory Holidaysthree

Final Exam PeriodAugust 18 - 22 (inclusive)

Do the assignmentsunderstand the theory

apply the theory

practice

Attend Lectures and Labsremember your diskettes

back up your diskettes!

be on time

ask questions

Learn the toolsmemorization isn’t enough

There are no dumb questionsask

ask

ask

Stop me if I go too fastassist me in knowing your learning styles

Do your homeworkuse class time to ask questions

Be hereLab attendance is mandatory

Be on timeuse class time well

Listento me

to questions (and answers!)

Focusdon’t distract me or others

don’t distract yourself

Diskettes3

½inchhigh density (HD)

disks for assignments

spare disks for back-up

Yousign in

please be on time

attendance is mandatory

missed labs result in no course credit: see the course outline

Generate your accountsyou must do this for e-mail

see the Student Monitors

Labs: C204, C205, C207, +?

Choose a good passwordyou can remember it

other people can’t guess it

Use your account!ask for assistance

find the course outline

send your neighbour some e-mail

Addressable Memorycomposed of binary digits:

bitsbits grouped by 8 into

byteseach byte has an

addressbytes grouped into

words

Central Processing Unit (CPU)reads bytes and words from memory

operates on the bits (adds, shifts, etc.) based on the numeric

instruction codealso read from memorywrites bytes and words into memory

Input and Output (I/O)printers, terminals, modems, etc.

are simply numbers stored in the main memory of the computer

the numbers have special meaning when read by the Central Processing Unit (CPU)

the CPU reads the numbers as

instruction codesthat tell it to perform some actionsthe action taken by the CPU upon reading a numeric instruction code from memory depends on the type of the CPU, e.g. PC, Macintosh, Atari

Text: Section 1.5 - 1.6

Blue Book: Stepwise Refinement (p. 12-24)

Problem Solving:

the problemDefineDetermine the

OutputsDetermine the

InputsDerive the

Algorithm

Stepwise Refinement of an Algorithm:Steps are followed in order

Each stepwise refinement is indented

Each indentation explains how to do the higher-level step above it

No more than half a dozen steps at any level of indentation

Don’t focus on detail and refinement until all the higher-level steps are understood

Get Food

Obtain Moneygo outside and unlock bicycle

pedal over to bank machine and get cash

Go Shoppingpedal to local market with cash

purchase macaroni and cheese dinner

Come homeput cheese dinner ingredients in bicycle bag

cycle home (lock bike; go inside with food…)

Prepare Food

Prepare Wateradd salt to water

heat water on stove until boiling

Prepare Macaroniplace macaroni in boiling water

wait until cooked just right

drain macaroni

Add Magic Cheese Saucemix magic cheese powder into macaroni

mix butter and milk into macaroni

Serve FoodSet table for one

Turn on nice dinner music

Light candles

Place pot of food on dining room table

Put some food from pot onto plate

Eat FoodPlace fork into delicious food

Lift fork with food into face

Repeat until food is gone or face is full

Blue Book: p.10-11,

Appendix AUse pseudocode to express the details of your Algorithms

Pseudocode is English text combined with key programming words

Less strict than programming languages

More strict than English

Pseudocode should be well structured and pleasing to the eye.

Each statement must be concise.

Omit words such as "the" and "and".

Start each line with a capital letter.

Use lower case English with upper case programming

.keywordsKeywords are used for:

input and output:

GET,PUTdecisions:

IF,ELSElooping:

FOR,WHILEcomparison:

EQUAL,LESS

Input:

GETOutput:

PUTDecisions:

IF…ELSE…ENDIFLooping:

FOR…ENDFOR

WHILE…ENDWHILE

DO…WHILEProper indentation must be used for each type of statement.

Indent

spaces for all compound statements and all statements within loops.three

Text: Chapter 2

Natural Numbers

1, 2, 3, ...

Whole Numbers

0, 1, 2, 3, ...

Integers

… , -2, -1, 0, 1, 2, …

Rational Numbers

22/7, 1/3,etc.

Irrational Numbers

PI=3.14159265358979323…

4,295 base 10

4 x 10 to the power 3 = 4,000

2 x 10 to the power 2 = + 200

9 x 10 to the power 1 = + 90

5 x 10 to the power 0 = + 5

------

as a base 10 number = 4,295

3.1415 base 10

3 x 10 to the power 0 = 3.0

1 x 10 to the power -1 = +.1

4 x 10 to the power -2 = +.04

1 x 10 to the power -3 = +.001

5 x 10 to the power -4 = +.0005

-------

as a base 10 number = 3.1415

7162 base 8

7 x 8 to the power 3 = 3,584

1 x 8 to the power 2 = + 64

6 x 8 to the power 1 = + 48

2 x 8 to the power 0 = + 2

------

as a base 10 number = 3,698

2.1730 base 8

2 x 8 to the power 0 = 2.0

1 x 8 to the power -1 = +.125

7 x 8 to the power -2 = +.109375

3 x 8 to the power -3 = +.0058593

0 x 8 to the power -4 = +.0000000

----------

as a base 10 number = 2.2402343

Base 2 digits: 0,1

Base 8 digits: 0,1,2,…,6,7

Base 10 digits: 0,1,2,…,8,9

Base 16 digits:

0,1,2,…,8,9,A,B,…,E,F

Ais adigitwith value 10

Bis adigitwith value 11

Cis adigitwith value 12

Dis adigitwith value 13

Eis adigitwith value 14

Fis adigitwith value 15

F + 1 = 10 base 16

D + E = 1B base 16

C - 7 = 5 base 16

F - E = 1 base 16

719F base 16

7 x 16 to the power 3 = 28,672

1 x 16 to the power 2 = + 256

9 x 16 to the power 1 = + 144

F x 16 to the power 0 = + 15

-------

as a base 10 number = 29,087

F.1C3 base 16

F x 16 to the power 0 = 15.0

1 x 16 to the power -1 = + .0625

C x 16 to the power -2 = + .046875

3 x 16 to the power -3 = + .000732

----------

as a base 10 number = 15.110107

Conversion of a whole number in any base to decimal (base 10):Multiply each digit in the number by the base raised to the power corresponding to the position of the digit in the number

The rightmost digit is multiplied by the base raised to the power zero, next digit is multiplied by the base raised to the power 1, etc.

Sum the resulting products

Left-to-Right:Start dividing by the largest power of the base that is less than or equal to the value; continue with all the lesser powers of the base in order. Write down the quotient digits from top-to-bottom and left-to-right.

Right-to-Left:Keep dividing by the base until the quotient is zero. Write down the remainder digits in order from top-to-bottom and right-to-left.

Start dividing by the largest power of the base that is less than or equal to the value; continue with all the lesser powers of the base in order:

1933 base 10 to binary (base 2):

1933 div 2^{**10 }= 1 rem 909

909 div 2^{**9 }= 1 rem 397

397 div 2^{**8 }= 1 rem 141

141 div 2^{**7 }= 1 rem 13

13 div 2^{**6 }= 0 rem 13

13 div 2^{**5 }= 0 rem 13

13 div 2^{**4 }= 0 rem 13

13 div 2^{**3 }= 1 rem 5

5 div 2^{**2 }= 1 rem 1

1 div 2^{**1 }= 0 rem 1

1 div 1^{**0 }= 1 rem 0

= 11110001101 binary( top-to-bottom and left-to-right )

Start dividing by the largest power of the base that is less than or equal to the value; continue with all the lesser powers of the base in order:

1933 base 10 to hex:

1933 div 16^{**2}= 7 rem 141

141 div 16^{**1}= 8 rem 13

13 div 16^{**0}= 13 rem 0

= 78D hex (base 16)( top-to-bottom and left-to-right )

Note that 13 is written as the digit ‘

D’ in hexadecimal notation.

Keep dividing by the base until zero; write all the remainder digits down from right-to-left:

1933 base 10 to binary (base 2):

1933 div 2 = 966 rem 1

966 div 2 = 483 rem 0

483 div 2 = 241 rem 1

241 div 2 = 120 rem 1

120 div 2 = 60 rem 0

60 div 2 = 30 rem 0

30 div 2 = 15 rem 0

15 div 2 = 7 rem 1

7 div 2 = 3 rem 1

3 div 2 = 1 rem 1

1 div 2 = 0 rem 1

= 11110001101 binary( top-to-bottom and right-to-left )

Keep dividing by the base until zero; write all the remainder digits down in order from top-to-bottom and right-to-left:

1933 base 10 to hex:

1933 div 16 = 120 rem 13

120 div 16 = 7 rem 8

7 div 16 = 0 rem 7

= 78D hex (base 16)( top-to-bottom and right-to-left )

Note that 13 is written as the digit ‘

D’ in hexadecimal notation.

Bases 2, 8, and 16 are easily related

Binary: Consider a two-byte value:

10110100 11000110

Octal: group/ungroup by 8’s (3 bits)

1 011 010 011 000 110

1 3 2 3 0 6

=> 132306 base 8

Hex: group/ungroup by 16’s (4 bits)

1011 0100 1100 0110

B 4 C 6

=> B4C6 base 16

132306_{8}= B4C6_{16}= 46,278_{10}

Decimal to other basesFor positive or unsigned numbers:

Use the right-to-left or left-to-right dividing-by-the base method

Other bases to decimalFor positive or unsigned numbers:

Multiply each digit by its corresponding power of the base

Add up the products

Among binary, octal, and hexFor all bit patterns, whether signed or unsigned, just group and ungroup bits:

Octal: group/ungroup by 3 bits

Hex: group/ungroup by 4 bits

Note that

0111111base 2 is one less than1000000base 2Note that

0777base 8 is

one less than1000base 8Note that

0999base 10 is

one less than1000base 10Note that

0FFFbase 16 is

one less than1000base 16

American Standard Code for Information Interchange

An

interpretationof 7-bit numbers7 bits means 128 values (characters)

the value of ' ' (space) = 32

Upper-case: 'A' = 65 'Z' = 90

Lower-case: 'a' = 97 'z' = 122

ASCII characters are 7-bit numbers:

'A' + ' ' = 'a' (= 97)

'B' + ' ' = 'b' (= 98)

'a' + 2 = 'c' (= 99)

Consider one-byte (8-bit) integers:

2^{**8}= 256 possible numbers:

0000 0000 00_{16 }0

0000 0001 01_{16 }1

0000 0010 02_{16 }2

0000 0011 03_{16 }3

. . .

0111 1110 7E_{16 }126

0111 1111 7F_{16 }127

1000 0000 80_{16}128

1000 0001 81_{16}129

1000 0010 82_{16 }130

1000 0011 83_{16}131

. . .

1111 1101 FD_{16}253

1111 1110 FE_{16}254

1111 1111 FF_{16}255

Consider one-byte (8-bit) integers:

2^{**8}= 256 possible numbers:

0000 0000 00_{16 }0

0000 0001 01_{16 }1

0000 0010 02_{16 }2

0000 0011 03_{16 }3

. . .

0111 1110 7E_{16 }126

0111 1111 7F_{16 }127

1000 0000 80_{16 }-128

1000 0001 81_{16}-127

1000 0010 82_{16 }-126

1000 0011 83_{16}-125

. . .

1111 1101 FD_{16}-3

1111 1110 FE_{16}-2

1111 1111 FF_{16}-1

Consider two-byte (16-bit) integers:

2^{**16}= 65,536 possible numbers:

0000_{16}0

0001_{16}1

0002_{16}2

0003_{16}3

. . .

7FFE_{16}32,766

7FFF_{16}32,767

8000_{16}-32,768

8001_{16}-32,767

8002_{16}-32,766

. . .

FFFC_{16}-4

FFFD_{16}-3

FFFE_{16}-2

FFFF_{16}-1

Since half the bit values are used for negative numbers, signed integers have half the positive range of unsigned integers

16 bit unsigned:

0 … 65,53516 bit signed:

-32,768 … 0 … +32,767The most positive value and most negative value are adjacent bit patterns

adding one to the most positive value generates the bit pattern for the most negative value ("integer overflow")

the bit pattern for the most positive value is the complement of the bit pattern for the most negative value:

7FFFand8000,7FFFFFFFand80000000

-1is always "all bits turned on"8 bits:

FF16 bits:

FFFF32 bits:

FFFFFFFFadding

1to-1turns all bits off: zero

The same bit pattern may be interpreted as either signed or unsigned. We say N is a

signed integerif, when we interpret N, we interpret all bit patterns with thesign bitset asnegativenumbers.If N is a signed integer, then its bit pattern is to be read as signed, and the leftmost (top) bit is known as the

sign bit. If N is signed and the sign bit is set, the bit pattern will be interpreted as anegativenumber.Note: If the sign bit is zero, the bit pattern has the same numeric value whether interpreted as signed or unsigned.

If the number is a signed number, zero is positive (the sign bit is not set).

Negating signed binary, octal, hexinvert all the bits: 1 => 0, 0 => 1

(same as subtracting from -1)then add 1

i.e.

-N = ((-1) - N) + 1e.g. to negate

C0F6(negative):

= ((-1) - C0F6) + 1

= (FFFF - C0F6) + 1

= 3F09 + 1

= 3F0A(positive)

Converting negative decimal numbersconvert the positive equivalent

then negate it (using the above method)

e.g. to convert decimal

-16,138

= -(16,138)- make positive

= -(3F0A)- convert

= C0F6- negate

Numbers with the sign bit setOnly

signednumbers can be negative, and negative only if thesign bitis set

e.g.C0F6, 80A7, FFFF

To convert signed, negative numbers from binary, octal, and hex to decimalnegate the number (make positive)

then convert to decimal

then change the sign

e.g. to convert signed

C0F6(negative)

= -(3F0A)-negate

= -(16,138)-convert

= -16,138-change signe.g. to convert signed

10101010

= -(01010110)-negate

= -(86)-convert

= -86-change sign

From signed, negative bit patterns to negative decimal numbersnegate the negative bit pattern to make it a positive bit pattern

convert the positive bit pattern to a positive decimal number

change the sign on the positive decimal number to make it negative again

From negative decimal numbers to signed, negative bit patternschange the sign on the negative decimal number to make it positive

convert the positive decimal number to a positive bit pattern

negate the positive bit pattern to make it a negative bit pattern again

see "Scientific Notation" p.5 in the Algonquin CST8110 Blue BookFloating-point numbers are stored as separate mantissa (fraction) and exponent

.31415926 E+1Limits on the size of the mantissa and exponent limit the

precision(accuracy) andrangeof floating-point numbers

precision:# bits used in mantissa

1.234567 E1

1.234567890123456789 E1

range: #bits used in exponent

1.0 E38

1.0 E300

Arithmetic Overflow: Some real numbers are "too big" to represent in a given number of bits of storage:e.g.

1.0 E+500

Arithmetic Underflow: Some real numbers are "too small" to represent using a fixed number of bits:e.g.

1.0 E-500Numbers nearer to zero are closer together than numbers with larger exponents:

because the mantissa multiplies the exponent; successive numbers in the mantissa multiply the larger exponents, and the numbers space farther

Cancellation Error: Adding a relatively small number to a large one may not change the large one if the difference in magnitude is greater than the precision:e.g.

1.0E30 + 1.0E1 = 1.0E30the number with the smaller exponent has no effect if the magnitude of the two numbers differs by more than the number of digits of precision

the mantissa may not have enough precision to reflect the addition of such a relatively small number

Binary values between 0 and 1 are

sums of products of negative powers of two; they do not always accurately expresssums of products of negative powers of ten.Similar problem: 1/3 cannot be exactly expressed as a finite decimal number:

0.333333333333333333…1/10 (0.1 decimal) cannot ever be accurately represented in base 2, 8, or 16:

0.000110011001100… base 2

0.0631631631631… base 8

0.19999999999… base 16a sum of ten binary "tenths" will not be exactly 1 due to this representation error

Be aware of round-off error occurring due to the limited number of bits available for each of the mantissa and exponent.

Make sure you pick a storage type with enough bits for

precisionandrangeto accurately represent the numbers you use.Note that a four-byte integer has more bits of

precisionthan a four-byte floating-point number. A four-byte integer has lessrangethan a four-byte floating point #.Never compare floating-point numbers for exact equality;

test for "close enough" against a "reasonably" small value:absolute_value(f1 - f2) < epsilon

For general use, the C language on most computers defines a

floatfloating-point storage type, 4 bytes long, that handles approximately 6-7 decimal digits of mantissa and an exponent between-38and+38The C language

doublefloating-point storage type, 8 bytes long, handles approximately 17-18 decimal digits of mantissa and an exponent between-308and+308Remember: Bits are bits. The same bit pattern may be a signed or unsigned integer, a character, or a floating-point number, depending on how it is used.

main()

{

int i;

float x;

double y;

printf("\nFloats:\n");

for( i=0; i <= 9; i++ ){

x = 1.0 + (i / 10.0);

printf("Expect 1.%d - actual is %.42f\n",

i, x);

}

printf("\nDoubles:\n");

for( i=0; i <= 9; i++ ){

y = 1.0 + (i / 10.0);

printf("Expect 1.%d - actual is %.42f\n",

i, y);

}

}

Floats:

Expect 1.0 - actual is 1.000000000000000000000000000000000000000000

Expect 1.1 - actual is 1.100000023841857910156250000000000000000000

Expect 1.2 - actual is 1.200000047683715820312500000000000000000000

Expect 1.3 - actual is 1.299999952316284179687500000000000000000000

Expect 1.4 - actual is 1.399999976158142089843750000000000000000000

Expect 1.5 - actual is 1.500000000000000000000000000000000000000000

Expect 1.6 - actual is 1.600000023841857910156250000000000000000000

Expect 1.7 - actual is 1.700000047683715820312500000000000000000000

Expect 1.8 - actual is 1.799999952316284179687500000000000000000000

Expect 1.9 - actual is 1.899999976158142089843750000000000000000000

Doubles:

Expect 1.0 - actual is 1.000000000000000000000000000000000000000000

Expect 1.1 - actual is 1.100000000000000088817841970012523233890533

Expect 1.2 - actual is 1.199999999999999955591079014993738383054733

Expect 1.3 - actual is 1.300000000000000044408920985006261616945267

Expect 1.4 - actual is 1.399999999999999911182158029987476766109467

Expect 1.5 - actual is 1.500000000000000000000000000000000000000000

Expect 1.6 - actual is 1.600000000000000088817841970012523233890533

Expect 1.7 - actual is 1.699999999999999955591079014993738383054733

Expect 1.8 - actual is 1.800000000000000044408920985006261616945267

Expect 1.9 - actual is 1.899999999999999911182158029987476766109467

Any time execution encounters the simple **IF** statement, a choice is
made whether or not to execute the enclosed set of statements:

IFcontrol_expression

Statement 1

Statement 2

…

ENDIFIf

testscontrol_expressiontrue,execute the set of statements between theIFand theENDIF.If

testscontrol_expressionfalse,skip all the statements and continue execution with whatever comes after theENDIF.

IFisnota loop. When the simpleIFstatement is encountered, the set of enclosed statements is only executed once, or not at all.

**#define LOW_LIMIT 0**

**if ( num < LOW_LIMIT )**

**{**

** printf("Error:
%d is smaller than %d\n",**

** num,
LOW_LIMIT);**

** printf("Resetting
number to %d.\n",**

** LOW_LIMIT);**

** num = LOW_LIMIT;**

**}**

**#define MAXDAYS 10**

**if ( days > MAXDAYS )**

**{**

** printf("Warning:
%d days > %d\n",**

** days,
MAXDAYS);**

**}**

**#define T_LETTER ‘T’**

**if ( letter != T_LETTER )**

**{**

** printf("You
typed %c instead of %c\n",**

** letter,
T_LETTER);**

**}**

Consider the following pair of of double-if statements, with
the first IF being done if the *control_expression* is
TRUE, and the second one being done if the expression is FALSE
(NOT TRUE):

IFcontrol_expression

A first set of one

or more statements …

ENDIF

IF! control_expression

A second set of one

or more statements …

ENDIF

This pairing happens often enough that C allows the above two IF statements to be written as one IF/ELSE statement:

IFcontrol_expression

A first set of one

or more statements …

ELSE

A second set of one

or more statements …

ENDIF

The meaning is identical in both cases. The IF/ELSE version is more efficient, since it only has to test the condition once. If the condition is TRUE, the first set of statements is executed. If the condition is FALSE, the second set is executed instead.

Any time execution encounters a compound **IF** statement, one of two
sets of statements are chosen:

IFcontrol_expression

A first set of one

or more statements …

ELSE

A second set of one

or more statements …

ENDIFIf

testscontrol_expressiontrue,execute the

firstset of statements, once.If

testscontrol_expressionfalse,execute the

secondset of statements, once.When this style

IFstatement is encountered, oneorthe other of the sets of statements is done.Never both sets of statements; never no set

Always one set

orthe other

**evencount = oddcount =
0;**

**if ( (num % 2) == 0 )**

**{**

** printf("%d
is an even number.\n", num);**

** ++evencount;**

**}**

**else**

**{**

** printf("%d
is an odd number.\n", num);**

** ++oddcount;**

**}**

**#define FEB_NOLEAP_DAYS 28**

**#define FEB_LEAP_DAYS
(FEB_NOLEAP_DAYS+1)**

**if ( isLeapYear(year) )**

**{**

** febDays =
FEB_LEAP_DAYS;**

**}**

**else**

**{**

** febDays =
FEB_NOLEAP_DAYS;**

**}**

**printf("February has %d days
in %d\n",**

** febDays, year);**

A nested IF statement is an IF statement inside an IF statement. The nesting might occur in either part of the IF, and the nested IF may or may not itself be a compound IF statement. Nesting can go to any level.

IFcontrol_expression_1

IFcontrol_expression_2

A nested set of one

or more statements …

ENDIF

ELSE

IFcontrol_expression_3

A nested set of one

or more statements …

ELSE

A nested set of one

or more statements …

ENDIF

ENDIF

**evencount = oddcount =
0;**

**if ( (num % 2) == 0 )**

**{**

** printf("%d
is an even number.\n", num);**

** ++evencount;**

** if( (num % 4) ==
0 )**

** printf("It
is divisible by 4\n");**

** if( num ==
0 )**

** {**

** printf("It
is zero.\n");**

** printf("That
isn’t interesting.\n");**

** }**

**}**

**else**

**{**

** printf("%d
is an odd number.\n", num);**

** ++oddcount;**

** if( (num % 9) ==
0 )**

** printf("It
is divisible by 9\n");**

** else**

** printf("It
is not divisible by 9\n");**

**}**

A decision table IF statement selects among more than two
alternatives. This form of IF statement avoids deep
nesting. Usually, each ** control_expression** tests
the same variable against different constant values.

IFcontrol_expression_1

A first set of one

or more statements …

ELSE IFcontrol_expression_2

A second set of one

or more statements …

ELSE IFcontrol_expression_3

A third set of one

or more statements …

ELSE

A final set of one

or more statements …

ENDIF

Note the use of brace brackets {} to enclose multiple statements, where needed.

The order of the tests is very important. Testing for "less than" must proceed from smallest to largest. Testing for "greater than" must proceed from largest to smallest.

**void**

**noise_print(int noise_db)**

**{**

** if(
noise_db <= 50 )**

** printf("quiet\n");**

** else if (
noise_db <= 70 )**

** printf("intrusive\n");**

** else if (
noise_db <= 90 )**

** printf("annoying\n");**

** else if (
noise_db <= 110 )**

** printf("very
annoying\n");**

** else**

** {**

** printf("\nNoise level
%d ",**

** noise_db);**

** printf("is
uncomfortably loud!\n");**

** }
**

**}**

InitializationWhat variables need starting values, once, before we begin the loop?

Variables used in the

?control_expressionVariables used in the loop body?

Condition()control_expressionDetermine the

that makes the logicalterminating conditionfalse and thus tells when to stop looping.control_expression

Increment / decrementWhat variable or variables must change value during the looping, so that the terminating condition eventually happens?

Usually done at the end of the loop body.

BodyThe statements that are repeated in the loop.

The

WHILElooping statement iterates zero or more times:

WHILEcontrol_expression

Statement 1

Statement 2

…

ENDWHILEWhile the

testscontrol_expressiontrueat the start of an iteration of the loop, execute the set of statements between theWHILEand theENDWHILE.When the

testscontrol_expressionfalseat the beginning of an iteration of the loop,skip all the statements and continue execution after theENDWHILE.Some statement within the loop must cause some value in the

to change fromcontrol_expressiontruetofalse, otherwise thewill never testcontrol_expressionfalseand the looping will never stop.If the

is notcontrol_expressiontruewhen theWHILEstatement is first encountered, the set of statements is not executed even once.

The

DO/WHILElooping statement iterates one or more times:

DO

Statement 1

Statement 2

…

WHILEcontrol_expressionExecute the set of statements between the

DOand theWHILE, and then repeat while thetestscontrol_expressiontrueat theendof an iteration of the loop.When the

testscontrol_expressionfalseat the end of an iteration of the loop,continue execution after the line containing theWHILE; don’t do any more iterations.Some statement within the loop must cause some value in the

to change fromcontrol_expressiontruetofalse, otherwise thewill never testcontrol_expressionfalseand the looping will never stop.Because the

is tested at thecontrol_expressionendof the loop, the set of statements isalwaysexecuted at leastonce, even if theiscontrol_expressionfalse.

The

FORlooping statement iterates zero or more times:

FORvar from N1 up/down to N2

Statement 1

Statement 2

…

ENDFORExecute the statements between

FORandENDFORwith the(variable) first set to the valuevar. Redo the same statements withN1taking on the next value (up or down) towardvar. Continue untilN2is equal tovar.N2If

is already pastN1when theN2FORstatement is first encountered, the set of statements is not executed even once. Execution continues after theENDFOR.

Variable: a named location in memory

Iterate: to loop; to do something more than once

Increment: to increase the value in a variable, usually by 1

Decrement: to decrease the value in a variable, usually by 1

Termination condition: the condition that terminates a loop (the complement of the loop expression)

: Numbers will be input from keyboard one at a time. Find the sum of the positive numbers and the sum of the negative numbers. Display both results.ProblemUse a loop controlled by a character from the keyboard.

After each number, the user will be asked if he/she wishes to enter another number. If the response is 'Y', get the next number and sum it. Terminate input for any other response character.

Semi-English solution:

Initialize positive sum, negative sum

DO

Prompt user for a number

Get and echo number

IF number >= 0

Add to positive sum

ELSE

Add to negative sum

ENDIF

Prompt user for a Y response

Get and echo response

WHILE response is Y

Display both sums

PosSum <-- 0

NegSum <-- 0

DO

PUT "Enter a number: "

GET Number

PUT "You entered: " Number

IF Number >= 0

PosSum <-- PosSum + Number

ELSE

NegSum <-- NegSum + Number

ENDIF

PUT "Type a ‘Y’ to continue"

GET Response

PUT "Your response was: " Response

WHILE Response EQUALS ‘Y’

PUT "The positive sum is: " PosSum

PUT "The negative sum is: " NegSum

: The marks for 70 students are to be entered. Find and display the average mark.ProblemUse a loop controlled by a counter set to 70 when the program begins.

Semi-English solution:

Initialize counter, sum, maxmark

WHILE counter <= maxmark

Prompt for the mark

GET mark

Echo the mark

Add mark to sum

Increment counter

ENDWHILE

Calculate average using sum, maxmark

PUT average

Sum <-- 0

Counter <-- 1

MaxMark <-- 70

WHILE Counter <= MaxMark

PUT "Enter Mark #" Counter ":"

GET Mark

PUT "You entered:" Mark

Sum <-- Sum + Mark

Counter <-- Counter + 1

ENDWHILE

Average <-- Sum / MaxMark

PUT "The average of " MaxMark

PUT " marks is " Average

Write the pseudocode for the following problems:1. Accept numbers from keyboard. Use 9999 to terminate.

Find and display largest number (excluding 9999).2. To count the number of times it takes player #2 to guess a number between 1 and 100 that is input by player #1.

When the guess is too high or too low, display appropriate messages. When a correct guess is made, display an appropriate message and the number of guesses taken.3. Accept positive numbers from keyboard.

Terminate with a negative number.

Determine the smallest and largest positive number that was entered. Display both.4. Accept student test marks from the keyboard.

Terminate with a -1. Display each mark.

Count the number of marks that correspond to each letter grade: A, B, C, D, and F. Display the five counts.