Sample Problems for Quiz # 2

Similar documents
ECE264 Fall 2013 Exam 1, September 24, 2013

Sorting. Task Description. Selection Sort. Should we worry about speed?

Introduction to Computing Systems Fall Lab # 3

ECE264 Summer 2013 Exam 1, June 20, 2013

ECE264 Fall 2013 Exam 3, November 20, 2013

ECE264 Spring 2013 Exam 1, February 14, 2013

Sample Problems for Quiz # 3

ECE264 Spring 2013 Final Exam, April 30, 2013

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Lecture 6 Sorting and Searching

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Functions. Arash Rafiey. September 26, 2017

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

CS 241 Data Organization Binary

Beginning C Programming for Engineers

Make sure the version number is marked on your scantron sheet. This is Version 1

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

This is CS50. Harvard University Fall Quiz 0 Answer Key

Chapter 7 C Pointers

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COSC242 Lecture 7 Mergesort and Quicksort

Chapter 4: Computer Codes. In this chapter you will learn about:

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

C0MP1911 Final Exam 1337 Computing 1

Data Types. Operators, Assignment, Output and Return Statements

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

11/22/1999 7pm - 9pm. Name: Login Name: Preceptor Name: Precept Number:

Better sorting algorithms (Weiss chapter )

Solutions to Assessment

The type of all data used in a C (or C++) program must be specified

Data Structures And Algorithms

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Divide and Conquer Strategy. (Page#27)

QuickSort

Answer all questions. Write your answers only in the space provided. Full marks = 50

ITEC2620 Introduction to Data Structures

Dept. of CSE, IIT KGP

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

CS 146 Midterm Exam 2 Grade: Professors Howard, Fine, and Howard have proposed the following sorting algorithm:

About this exam review

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Bubble sort starts with very first two elements, comparing them to check which one is greater.

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Searching Algorithms/Time Analysis

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

Lecture 3. More About C

COMP1917 Computing 1 Written Exam Sample Questions

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Midterm Exam Review Slides

Data Structures and Algorithms for Engineers

Practice final for EECS 380, 2001: Prof Markov

DATA STRUCTURES AND ALGORITHMS

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : End-Semester Examination

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

Duration: 110 minutes

CS171 Midterm Exam. October 29, Name:

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Computer System and programming in C

Lecture 03 Bits, Bytes and Data Types

CS13002 Programming and Data Structures, Spring 2005

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Tools for algorithms and programs. From practice to theory and back again. On to sorting: Selection Sort. To code or not to code, that is the

Quiz 0 Review Session. October 13th, 2014

Computer Organization & Systems Exam I Example Questions

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Design and Analysis of Algorithms

Review Topics. Midterm Exam Review Slides

Programming in C. Pointers and Arrays

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

AMCAT Automata Coding Sample Questions And Answers

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

David Rappaport School of Computing Queen s University CANADA. Copyright, 1996 Dale Carnegie & Associates, Inc.

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Denotational semantics

CSCI 2132 Final Exam Solutions

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Subject: Fundamental of Computer Programming 2068

Expression and Operator

Data Structures and Algorithms 2018

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Sorting. Bringing Order to the World

Simple Data Types in C. Alan L. Cox

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

Transcription:

EE 1301 UMN Introduction to Computing Systems Fall 2013 Sample Problems for Quiz # 2 (with solutions) Here are sample problems to help you prepare for Quiz 2 on Oct. 31. 1. Bit-Level Arithmetic (a) Consider the following C program. # include <stdio.h> int main ( int argc, char ** argv ) unsigned int v; v = atoi ( argv [1]); v - -; v = v >> 1; v = v >> 2; v = v >> 4; v = v >> 8; v = v >> 16; v ++; printf ("%d\n", v); What will it print out if it is called as follows?./ rndpow2 32./ rndpow2 42./ rndpow2 1023 Hint: the program computes 1 << (log 2 (v 1) + 1).

EE 1301, Fall 13 2 (b) What will the following program print out? # include <stdio.h> int main () int i; int m = 0xA; unsigned int n = 0; for (i = 0; i < 8; i ++) n = (m << i *4); for (i = 0; i < 32; i ++) printf ("%d", (n >> i) & 1); printf ("\n"); Answer: 01010101010101010101010101010101

EE 1301, Fall 13 3 (c) Consider the following program. # include <stdio.h> enum boolean false, true ; int main ( int argc, char ** argv ) int i; int n = atoi ( argv [1]); enum boolean sign = atoi ( argv [ 2]) > 0? true : false ; unsigned int r; if ( sign ) r = 0; r = (1 << n); else r = ~0; r ^= (1 << n); for (i = 0; i < 32; i ++) printf ("%d", (r >> i) & 1); printf ("\n"); Suppose that it is called with the following arguments. What will it print out?./ set - given - bit 7 0 Answer: 11111110111111111111111111111111./ set - given - bit 27 1 Answer: 00000000000000000000000000010000

EE 1301, Fall 13 4 2. Finite-Precision Arithmetic Consider the following program. # include <stdio.h> int main () unsigned char a = 100; unsigned char b = 200; unsigned char c = a + b; unsigned char d = a - b; char e = a + b; char f = a - b; char g = -(a + b); int h = a + b; printf ("%d %d %d %d %d %d %d %d %o %x\n", a, b, c, d, e, f, g, h, h, h); What will it print out? Answer: 100 200 44 156 44-100 -44 300 454 12 c

EE 1301, Fall 13 5 3. Optimal Cost Paths Through Graphs Find the minimum cost path from A to be B, where the cost is the sum of the numbers along that path. 1 ----- 25 ----- 3 ------ 6 ------ 12 ----- 12 ----- 5 ------ 25 ----- 1 / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / 2 \ / 11 \ / 8 \ / 7 \ / 4 \ / 1 \ / 1 \ / 8 \ A / / / / / / / / B \ / \ 9 / \ 5 / \ 1 / \ 25 / \ 8 / \ 1 / \ 15 / \ 9 / \ / \ / \ / \ / \ / \ / \ / \ / \ / 7 ----- 10 ----- 17 ----- 9 ------ 1 ------ 11 ----- 7 ------ 1 ------ 3

EE 1301, Fall 13 6 4. Number Systems (a) Suppose that the following bits represent positive numbers. their decimal equivalents. i. 10101011 2. Answer: 171 10. ii. 01111110 2. Answer: 126 10. iii. 10000000 2. Answer: 128 10. iv. 11111111 2. Answer: 255 10. Convert them to (b) Suppose that the following bits represent positive and negative numbers in two s complement notation. Perform the operation indicated. If the result cannot be represented with 8 bits, say so. i. 10000000 2 + 00000001 2. Answer: 10000001 2 = -127 10. ii. 01111111 2 + 01111110 2 Answer: 11111101 2. Positive number plus positive number yields negative number. Conclude that result cannot be represented with 8 bits. iii. 10000000 2-01000000 2. Answer: 10000000 2 + 11000000 2 = 101000000 2. Extra bit falls off. But then we have a negative number plus a negative number yields a positive number. Conclude that result cannot be represented with 8 bits. iv. 11111100 2-11111110 2. Answer: 11111100 2 + 00000010 2 = 11111110 2 = -00000010 2 = -2 10. (c) Suppose that the following are all positive numbers. Perform the indicated conversion. i. 101101101000111101110 2 to hexademical. Answer: 0001 0110 1101 0001 1110 1110 2 = 16D1EE 16. ii. 623401 8 to hexademical. Answer: 110 010 011 100 000 001 2 = 0011 0010 0111 0000 0001 2 = 32701 16. iii. AF AB 16 to octal. Answer: 1010 1111 1010 1011 2 = 001 010 111 110 101 011 2 = 127653 8

EE 1301, Fall 13 7 5. Ninjas and Knapsacks This problem will test your understanding of dynamic programming, an algorithmic archetype that we discussed in class Suppose that a ninja has broken into the Imperial Palace in Tokyo to steal valuable trinkets. He will catch the direct return flight with Delta to Minneapolis, but he s very annoyed at Delta s new fees for checked-in luggage. Out of principle, he refuses to pay such fees; everything that he steals from the Imperial Palace must fit in his backpack as carry-on. The tensile strength of his UMN backpack is such that he can safely pack 10 lbs. The ninja assesses the value and weight of the trinkets to be as follows: item value weight 1 3 1 2 4 3 3 9 6 4 2 3 5 11 4 6 2 3 7 4 2 8 3 1 9 1 1 10 7 3 What is the value of the best choice of trinkets? Solution See attached spreadsheet.

EE 1301, Fall 13 8 6. Sorting This problem tests your understanding of sorting (and, of course, recursion). Here is the algorithm that we discussed in class, mergesort(). What does the code print out? # include <stdio.h> mergesort ( int a[], int low, int high ) int mid ; if(low < high ) mid =( low + high )/2; mergesort (a, low, mid ); mergesort (a, mid + 1, high ); merge (a,low, high, mid ); return (0); merge ( int a[], int low, int high, int mid ) printf ("%d, %d, %d\n", low, high, mid ); int i, j, k, c [10]; i = low ; j = mid + 1; k = low ; while ((i <= mid ) &&( j <= high )) if(a[i] < a[j]) c[k] = a[i]; k ++; i ++; else c[k] = a[j]; k ++; j ++; while (i <= mid ) c[k] = a[i]; k ++;

EE 1301, Fall 13 9 i ++; while (j <= high ) c[k] = a[j]; k ++; j ++; for (i = low ; i < k; i ++) a[i] = c[i]; int main ( int argc, char ** argv ) int i; int v [10] = 6, 21, 3, 89, 2, 77, 1, 78, 79, 99; mergesort (v, 0, 9); for ( i = 0; i < 10; i ++) printf ("%d ", v[i ]); printf ("\n"); Answer 0, 1, 0 0, 2, 1 3, 4, 3 0, 4, 2 5, 6, 5 5, 7, 6 8, 9, 8 5, 9, 7 0, 9, 4 1 2 3 6 21 77 78 79 89 99

EE 1301, Fall 13 10 Here is one the most common sorting algorithm, quicksort(). What does the code printout? # include <stdio.h> /* swap : interchange v[i] and v[j] */ void swap ( int v[], int i, int j) int temp ; temp = v[i]; v[i] = v[j]; v[j] = temp ; /* qsort : sort v[ left ]... v[ right ] into increasing order */ void qsort ( int v[], int left, int right ) int i, last ; printf (" left %d, right %d\n", left, right ); /* do nothing if array contains fewer than two elements */ if ( left >= right ) return ; /* move " pivot " element to v [0] */ swap (v, left, ( left + right )/2); last = left ; /* partition */ for ( i = left + 1; i <= right ; i ++) if (v[i] < v[ left ]) swap (v, ++ last, i); /* restore " pivot " element */ swap (v, left, last ); qsort (v, left, last -1); qsort (v, last +1, right ); int main ( int argc, char ** argv )

EE 1301, Fall 13 11 int i; int v [10] = 6, 21, 3, 89, 2, 77, 1, 78, 79, 99; qsort (v, 0, 9); for ( i = 0; i < 9; i ++) printf ("%d ", v[i ]); printf ("\n"); Answer left 0, right 9 left 0, right 0 left 2, right 9 left 2, right 4 left 2, right 2 left 4, right 4 left 6, right 9 left 6, right 5 left 7, right 9 left 7, right 6 left 8, right 9 left 8, right 7 left 9, right 9 1 2 3 6 21 77 78 79 89 99

EE 1301, Fall 13 12 7. Graphs for Coding This problem asks you to formulate codes for characters of the alphabet, using graphs specifically binary decision diagrams. In 1951, David A. Huffman and his MIT information theory classmates were given the choice of a term paper or a final exam. The professor, Robert M. Fano, assigned a term paper on the problem of finding the most efficient binary code. Huffman, unable to prove any codes were the most efficient, was about to give up and start studying for the final when he hit upon the idea of using a frequency-sorted binary tree and quickly proved this method the most efficient. In doing so, the student outdid his professor, who had worked with information theory inventor Claude Shannon to develop a similar code. Huffman avoided the major flaw of previous coding by building the tree from the bottom up instead of from the top down. Suppose that the following characters appear with the given frequency. Determine an optimal binary code for these characters. Char Frequency space 7 a 4 e 4 f 3 h 2 i 2 m 2 n 2 s 2 t 2 l 1 o 1 p 1 r 1 u 1 x 1

EE 1301, Fall 13 13 Solution Char Frequency Code space 7 111 a 4 010 e 4 000 f 3 1101 h 2 1010 i 2 1000 m 2 0111 n 2 0010 s 2 1011 t 2 0110 l 1 11001 o 1 00110 p 1 10011 r 1 11000 u 1 00111 x 1 10010