INTRODUCTION Flashcards
Define a data type and give some examples
- 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
What is an abstract data type
- 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
Explain the difference between data structures and algorithms
• 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.
Give examples of data structures
- Queue
- Stack
- Linked list • Graphs
What is a pseudocode?
• Pseudocode is an artificial and informal language that helps programmers
develop algorithms. • Pseudocode is very similar to everyday English.
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.
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
• 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
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
• Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message.
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
Write an algorithm that reads three numbers and prints the value of
the largest number.
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