INTRODUCTION Flashcards

1
Q

Define a data type and give some examples

A
  • A data type is characterized by: • a domain of values
  • a set of operations (e.g add, subtract, square root), which can be applied uniformly to all these values
  • Examples: int, float, double, char
  • Data types depend on the programming language
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an abstract data type

A
  • An Abstract Data Type (ADT) is:
  • a set of values
  • a set of operations, which can be applied uniformly to all these values

• An ADT is a formal description, not code; independent of any programming
language

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

Explain the difference between data structures and algorithms

A

• Data structures implement ADTs. • They contain operations (implemented) to manipulate data elements. • ADT is in the logical level while a data structure is in the implementation level. • So a list can be described in terms of an abstract data type (we can insert into it, get the nth element, delete an element, etc.), • A linked list, for example, is an implementation of a list abstract data type -> it implements the specified behavior by structuring the data as either an empty list or a pairing of a data element and another linked list, inserting an element or deleting an element. • We could implement the same list abstract data type in many other ways, for example with an array, or binary tree.

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

Give examples of data structures

A
  • Queue
  • Stack
  • Linked list • Graphs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a pseudocode?

A

• Pseudocode is an artificial and informal language that helps programmers
develop algorithms. • Pseudocode is very similar to everyday English.

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

Write an algorithm to determine a student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as the average of four marks.

A
Pseudocode: 
• Input a set of 4 marks
• Calculate their average by summing and dividing by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”
• Detailed Algorithm:
Step 1: Input M1,M2,M3,M4
Step 2: GRADE <- (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

• Write a pseudocode, an algorithm and draw a flowchart that will calculate the roots of a quadratic equation • Hint: d = sqrt (b^2 -4ac ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

A
Pseudocode: • Input the coefficients (a, b, c) of the quadratic equation
• Calculate d
• Calculate x1
• Calculate x2
• Print x1 and x2
  • Algorithm:
  • Step 1: Input a, b, c
  • Step 2: d <-sqrt ( ) • Step 3: x1 <-(–b + d) / (2 x a)
  • Step 4: x2 <-(–b – d) / (2 x a)
  • Step 5: Print x1, x2

**See notes for flowchart

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

• Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message.

A
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then 
MAX <- VALUE1
else 
MAX <- VALUE2
endif
Step 3: Print “The largest value is”, MAX

**See notes for flowchart

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

Write an algorithm that reads three numbers and prints the value of
the largest number.

A
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then 
MAX <-N1 
else 
MAX <-N3 
endif 
else if (N2>N3)
 then MAX <-N2  
else 
MAX <-N3  
endif 
endif
 Step 3: Print “The largest number is”, MAX
How well did you know this?
1
Not at all
2
3
4
5
Perfectly