CS 2050

Ace your homework & exams now with Quizwiz!

Abstract Data Types(ADR)

"Inventory"

This function runs in 0(1) constant time.

What is the runtime of this program? void printFirstItem(const int* arrayOfItems) { printf("%d\n", arrayOfItems[0]); }

Priority Queue

An ADT that supports the following: • Insert a new item • Check whether this is empty/full • Retrieve the item with the smallest/largest key

Bag

A collection of objects with few or no restrictions on its contents

A linked list

A data structure that is commonly used to implement the list ADT

Abstraction

A process that has the designer ask "what" instead of "how"

Collection ADTs

An ADT that is used to "hold" or "contained" some # of objects

Queue

An ADT that supports First-In-First-Out (FIFO)

Stack

An ADT that supports Last-In-First-Out (LIFO) access to data items

a) 90.000003

Assume that float takes 4 bytes, predict the output of the following program. #include <stdio.h> int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, .5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%f", *ptr2); printf("%d", ptr2 - ptr1); return 0; } a) 90.5000003 b) 90.50000012 c) 10.0000012 d) 0.5000003

- add item to a bag - delete item from bag - check if item is in bag - check for full bag

Bag operations include:

"Primitive" data types

Built in data type that supports ADTS for integers, reals, and characters.

d) sizeof arri[] = 12 sizeof ptri = 4 sizeof ptrc = 4

Consider a compiler where int take 4 bytes, char takes 1 byte and pointer takes 4 bytes. #include <stdio.h> int main() { int arri[] = {1, 2, 3}; int *ptri = arri;12 char arrc[] = {1, 2, 3}; int *ptri = arri; printf("sizeof arri[] = %d", sizeof(arri)); printf("sizeof ptri = %d", sizeof(ptri)); printf("sizeof arrc[] = %d", sizeof(arrc)); printf("sizeof ptrc = %d", sizeof(ptrc)); return 0; } a) sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4 b) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1 c) sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1 d) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

Top-Down Software

Development begins with an identification of a "high-level" solution to a problem that can be decomposed into smaller sub-solutions. At some level the sub-solutions will be so simple that they are trivial to implement.

Bottom-Up Software

Development often begins with an identification and implementation of a set of general ADTs and function modules from which a solution to a problem can be built easily.

Encapsulation

Hides details of the inner workings of ADT.

2) A queue

If an inbox ADT were defined (something that functions the way an inbox on someone's desk should operate) it would be virtually equivalent to which? 1) A stack 2) A queue 3) An array 4) A Linked List 5) A bag

typedef double Angle;

If it is later determined that angles require double precision then the typedef command can be changed to:

- add new entry - remove an item - remove all items - replace an item - look at any item - look for entry of a specific value - count numbers of items - check if list is full

Possible operations for a List ADT include:

The Push() Operation

Puts a data item onto the top of the stack

The Enqueue() Operation

Puts an item onto the queue

The Dequeue() Operation

Retrieves the earliest data item put into the queue

The Pop() Operation

Retrieves the last data item put on the stack

2) A sequence of enqueue and dequeue operations will take longer to execute than a sequence of push and pop operations for very large datasets

Suppose a queue and a stack are each maintained as a single pointer to a linked list. Which of the following statements is true? 1) The Enqueue and dequeue operations perform the same as push and pop for a stack, it's just the interpretation that's different 2) A sequence of enqueue and dequeue operations will take longer to execute than a sequence of push and pop operations for very large datasets 3) A sequence of enqueue and dequeue operations will take approximately the same amount of time to execute as a sequence of push and pop operations regardless of the size of the dataset.

1) The numbers 1-10 are printed in descending order

Suppose the integers 1-10 are iteratively enqueued and then iteratively dequeued and printed. What is the result? 1) The numbers 1-10 are printed in descending order 2) The order in which the numbers are printed depends on the implementation of the queue 3) The numbers 10-1 are printed in descending order 4) An error will occur because each enqueue operation must be followed by a dequeue operation 5) The tenth dequeue will produce an empty queue error

4) Selection Sort

Suppose you are developing code for a multiple-processor vector processing computer that supports a special set of very fast array operations. 2 of these operations are getMin(array) and getMax(array). Which return the index of the min or max element of an array in O(1) time. Which of the following sorting algorithms could most efficiently use operations of this kind? 1) Shuffle Sort 2) Bubble sort 3) Insertion Sort 4) Selection Sort 5) None

typedef float Angle; Angle theta phi;

The keyword typedef provides a way to create new data types based-on C's native built-in data types.

d) All of the above

The reason for using pointers in a C program is ? a) Pointers allow different functions to share and modify their local variables. b) To pass large structures so that complete copy of the structure can be avoided. c) Pointers enable complex "linked" data structures like linked lists and binary trees. d) All of the above

This function runs in 0(n^2) quadratic time.

What is the runtime of this program? void printAllPossibleOrderedPairs(const int* arrayOfItems, size_t size) { size_t i, j; for(i=0; i < size; j++){ for(j=0;j<size; j++){ printf("%d, %d\n", arrayOfItems[i], arrayOfItems[j]); }

Big O notation is used in Computer Science to describe the performance or complexity of an algorithm.

What is BIG-O?

- Call by Value creates a copy of the argument that is passed into a function - Call be Reference the address of the value is passed into a function as an argument

What is difference between call by reference and call by value?

a) 20

What is the output of the following C Program? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; } a) 20 b) 10 c) Compiler Error d) Runtime Error

a) 20

What is the output of the following program? #include <stdio.h> void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf("%d", y); return 0; } a) 20 b) 30 c) Compile Error d) Runtime Error

b) 30

What is the output of the following program? #include <stdio.h> void fun(int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&y); printf("%d", y); return 0; } a) 20 b) 30 c) Compile Error d) Runtime Error

a) Compile time error

What is the output of this C code(below) #include <stdio.h> struct student { int no; char name[20]; } void main() { struct student s; s.no = 8; printf("hello"); } a) Compile time error b) Nothing c) hello d) Varies

This function runs in 0(n) linear time

What is the runtime of this program? void printAllItems(const int* arrayOfItems, size_t size) { size_t i; for(i=0; i < size; i++){ printf("%d\n", arrayOfItems[i]); } }

This function runs in 0(2n) linear time. When working in Big-O we drop the constants so this is actually just 0(n)

What is the runtime of this program? void printAllItemsTwice(const int* theArray, size_t size) { size_t i; for(i=0; i<size; i++){ printf("%d\n", theArray[i]); } }

This function runs in O(n+n^2). In Big O we also drop the least significant term. This is O(n^2).

What is the runtime of this program? void printAllNumbersThenAllPairSums(const int* arrayOfNumbers, size_t size) { size_t i, j; printf("these are the numbers:\n"); for (i = 0; i < size; i++){ printf("%d\n", arrayOfNumbers[i]); } printf("and these are their sums:\n"); for(i = 0; i < size; i++){ for(j = 0; j < size; j++){ printf("%d\n", arrayOfNumbers[i] + arrayOfNumbers[j]); } } }

This function runs in O(1 + n/2 + 100). Which is just O(n)

What is the runtime of this program? void printFirstItemThenFirstHalfThenSayHi100Times(const int* theArray, size_t size) { size_t i, middleIndex, index; printf("%d\n", theArray[0]); middleIndex = size/2; index = 0; while(index < middleIndex){ printf("%d\n", theArray[index]); index++; } for(i = 0; i < 100; i++){ printf("hi\n"); } }

This function runs in 0(n) linear time.

What is the runtime of this program? void sayHiNTimes(size_t n) { size_t i; for(i = 0; i < size; i++){ printf("%d\n", theArray[i]); } }

A list is an ADT while a linked list is a data structure. A linked list data structure can be used to implement a variety of different ADTs

What's the difference between a list and a linked list

b) structures

Which of the following are themselves a collection of different data types? a) string b) structures c) char d) All of the mentioned

1) The worst case complexity of Insertion Sort is the same as that of Merge Sort.

Which of the following is false? 1) The worst case complexity of Insertion Sort is the same as that of Merge Sort. 2) The worst case complexity of Selection Sort is the same as that of Quick Sort 3) The best-case complexity of Insertion Sort is better than that of Quick Sort 4) The expected complexity of Merge Sort and Quick Sort is the same 5) Choosing the partitioning pivot randomly in Quick Sort results in an algorithm w/ O(N^2) complexity

2) Depending on whether the input dataset is sorted in ascending or descending order, insertion Sort will exhibit either its best case or worst case performance

Which of the following statements is true ? 1) Given that there are sorting algorithms with O(n log n) complexity, there is no reason why someone should ever prefer to use and O(n^2) sorting algorithm except possibly When something is known about the data distribution 2) Depending on whether the input dataset is sorted in ascending or descending order, insertion Sort will exhibit either its best case or worst case performance 3) Because of the Selection step, the speed of Selection Sort is more affected by the distribution of the data than is the case for insertion sort and bubble Sort. 4) Bubble Sort, insertion Sort and Selection Sort may each complete in O(n) time on certain datasets 5) None of the above

d) None of the mentioned

Which of the following structure declaration will throw an error? a)struct temp{}s; main(){} b) structs temp{}; struct temp s; main(){} c) struct temp s; struct temp{}; main(){} d) None of the mentioned

b) Function

which of the following cannot be a structure member? a) Another structure b) Function c) Array d) None of the mentioned

c) .

which operator connects the structure name to its member name? a) - b) <- c) . d) both (b) and (c)

The dot operator "."

• Exploiting a specific data representation • Won't allow the structure to be changed at a later time w/o forcing the users to update their programs by changing every line that uses the earlier version


Related study sets

Life Insurance Policy Provisions, Options and Riders

View Set

Med skills heart attack, stroke, seizures, fainting, diabetes

View Set

Med. Term Ch. 10 The Nervous System - Nerves

View Set