Lec 2 Flashcards

1
Q

Poor design disadvantages

A

1.Duplicatedd code make program unnecessarily large
2.Writng long sequences of statements is time consuming
3.If part of the duplicated code needs to be changed, this affects many other parts of the code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Good code

A

1.Would be to write a segment of code once and to place that segment of code inside of a repetition structure loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

repetition structure loop

A

Have the computer repeat a segment of code as many times as needed commonly known as a loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Their are 2 main types of loops

A

Condition-controlled
Count -controlled

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Condition-controlled loop

A

Condition-controlled loop will execute statements of code based on whether a conditions is met

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

3 types of condition controlled loop

A

3 types
-While
-DO-while
-Do Until

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

While loops and Do-While

A

While loops and Do-While loops will repeat as long as a condition is TRUE, meaning that the program exit the loop
once the condition is FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Do-Until loops

A

. On the other hand, Do-Until loops will repeat until a
condition is TRUE, meaning that the program stay in the loop as long as the condition is FALSE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a pretest loop

A

While Loop: “While a condition is TRUE, execute these statement”

A While loop consists of: (1) a condition that is evaluated as TRUE or FALSE, and (2) One or more statements that
will be repeated while the condition is TRUE. A While loop is also called a pretests loop
because the condition is evaluated before each iteration of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example 1:Write a program that prints all the odd integers from 1 to 11

A

Declare Integer counter and max value
Set counter=1
Set Maxvalue=11

While counter <maxvlaues
Display counter
Set counter=counter+2
End while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Do while

A

o-Until Loop (Gaddis 5.2): “Do this task UNTIL this condition is TRUE”
OR another way to say this is: do while a condition is false
A Do-Until loop consists of: (1) One or more statements that will be repeated while the condition is FALSE and (2) a
condition that is evaluated as TRUE or FALSE. A Do-Until loop is also called a post tests

Do
set
Display call
Until
.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a program that will ask the user to enter a value and use a loop to calculate a running total. Each time the loop
iterates, it should prompt the user if they want to continue, only showing the final total when they say ‘no’

A

Declare
Set total =0
Do
Display “Enter
Set total=total+num
Display”do you wat to continue”
Input Choice
While choice is “y”
Display “the total is”,total

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Condtion-Cointrolled loop summary

A

2 parts
-some Boolean expression evaluates as true or false
-statements of code executed within a loop
-Can be post test of pretest
Types
While(Pretests)- in loop when true exit when false
Do While(post)-In loop
Do-Until(Post)-in loop is false and exits when true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Counter controlled loop summary

(LOOK AT)

A

Counter controlled loop will execute a ststatement of code a speific numbers of iterations For Loop, a major type is (blank)

Certain of

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Counter controlled loop major parts(For Loop)

A

1.Initailization Counter variable is initialized to a starting value
2.Test the counter variable and compute it to a maximum value(End value)
3.Increment step:After each iteration, the counter is increments.The default increament is +1 but we use step as the increment vlaue

It is a pretets loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Exmaple of step and For loop-Wraps back around

A

For counter variable
Delcare
Set Counter =Startvalue
Start value to max value Step 1
Stament
Statment
End For

17
Q

pretets vs post tests

A

Pretest-loop at diamond
post test-loop after diamond

18
Q

Calculating running total 5 number

A

Delcare
Set Counter=1
Set total=o
For counter=1 to 5 Step 1(Flow chart counter<=5)
Display “Enter num
Input num
Set Counter=Counter+1
Set total=Total+num
End For
Displat total

19
Q

The difference between while and For Loops
Displat number of 5 of interval 0 to 100

A

Review From PROG-LEC-07
* While Loops (Condition-Controlled)
* Will iterate or run indefinitely until some condition is met
* Use a While loop if it is not clear how many times a loop will need to run
* Key “clues”
* “repeat until …”
* “only stopping when…”
* “ask the user to continue/stop…”

For Loops (Counter-Controlled)
* Will iterate or run a specified and predictable number of times
* Use For loops if you have a clear start, increment, and end point
* Key “clues”
* “For each value”
* Clear start and endpoint: “From 0 to 20 in increments of 5

While Loop
Set-number=0
While numver<100
Set Number=number+5
Display numbers
End While

For Loop
Declare INterger counter
Set COunet =0
For COunter=0 to 100 Step 5
Display COunter
End For

20
Q

Array

A

A array is a type of a variable that allows you to store groups of numbers or groups of strings togther.Arrays can hold mutiples variables at once, which is useful instroing list of infromation

-Arrays must be the same data type(can’t mix number and string/text in one array
-to create an arrays you must give it a name ad string

21
Q

Size of array

A

The sixe of an arrys is to let you know how many items are in a list .An important note when working with arrays in pseudocode, you must have a square brackets

22
Q

Elements

A

Items within an arrys are stored in their own location, this is called elements.

23
Q

Index

A

The positions of an elements is called its index
-Highlevel programming index arrays start at 0 but mat lab starts at 1

if you delcare number[5], you have elemnts 0,1,2,3,4

24
Q

Why does the For loop use”For Index=0 to 2 instead of For index= 0 to three

A

the start occurs at 0

25
Q

Why does the loop use (Index+1) instead of empolyee number
(LOOK AT)

A

because you

26
Q

Sequential Search of an arrsy

A
  1. Use For-loop to go through variables of each elements in an arrays to find what you are looking for
    2.You need a sperate variable to indicate when you have found what you are looking for within your array
    3.Use compound Boolean expression and decision structures to compare between information stored in each elements of an array and the information you want to find.
27
Q

One dimension vs two dimensation arrays

A

One dimensional array-holds one set of data.For example they can hold one set of strings(names, words, letters)or one set of numbers.

Two dimensional array can hold multiple sets of data. Declaring and creating a two dimensional array is very similar but now programmer need to use 2 sizes.The two values indicate the number of rwos and the number f columns in a an array

Declare row and column
Display enter row
Input row
Display enter column
Input Column
Declare integer exam [row][column]
Set exam[3][1]