Quiz 0 Review Session. October 13th, 2014

Similar documents
This is CS50. Harvard University Fall Quiz 0 Answer Key

Class Information ANNOUCEMENTS

EL2310 Scientific Programming

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CS61, Fall 2012 Section 2 Notes

Pointers, Dynamic Data, and Reference Types

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Lecture 8 Dynamic Memory Allocation

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Dynamic Data Structures (II)

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

In Java we have the keyword null, which is the value of an uninitialized reference type

MPATE-GE 2618: C Programming for Music Technology. Syllabus

PRINCIPLES OF OPERATING SYSTEMS

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Computer Science 50: Introduction to Computer Science I Harvard College Fall Quiz 0 Solutions. Answers other than the below may be possible.

Contents of Lecture 3

Low-Level C Programming. Memory map Pointers Arrays Structures

Computer Programming C++ (wg) CCOs

CSC C69: OPERATING SYSTEMS

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Recitation #11 Malloc Lab. November 7th, 2017

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

C Programming Basics II

ECE551 Midterm Version 1

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Short Notes of CS201

Data Structures and Algorithms for Engineers

Introduction to C Language (M3-R )

Arrays and Memory Management

CS201 - Introduction to Programming Glossary By

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

C-LANGUAGE CURRICULAM

M1-R4: Programing and Problem Solving using C (JULY 2018)

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

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Introduction to Programming Using Java (98-388)

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

6. Pointers, Structs, and Arrays. 1. Juli 2011

Lectures 5-6: Introduction to C

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

ECE551 Midterm Version 2

Full file at

CS61, Fall 2012 Midterm Review Section

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Data Structure Series

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

Data Structures and Algorithms for Engineers

CS201- Introduction to Programming Current Quizzes

CSCI 171 Chapter Outlines

Programs in memory. The layout of memory is roughly:

Contents. A Review of C language. Visual C Visual C++ 6.0

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

High Performance Programming Programming in C part 1

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Compiling and Running a C Program in Unix

Fundamental of Programming (C)

Computer Programming

211: Computer Architecture Summer 2016

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Contents. Preface. Introduction. Introduction to C Programming

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Procedural programming with C

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

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

Kurt Schmidt. October 30, 2018

Lecture 07 Debugging Programs with GDB

EL2310 Scientific Programming

FORM 2 (Please put your name and form # on the scantron!!!!)

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

CS 11 C track: lecture 6

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

ECE 551D Spring 2018 Midterm Exam

C Programming & Memory

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Lecture Notes on Memory Layout


C Introduction. Comparison w/ Java, Memory Model, and Pointers

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Computer Programming

DAY 3. CS3600, Northeastern University. Alan Mislove

Memory, Data, & Addressing II CSE 351 Spring

Motivation was to facilitate development of systems software, especially OS development.

Introduction to Computer Systems. Exam 2. April 10, Notes and calculators are permitted, but not computers.

Transcription:

Quiz 0 Review Session October 13th, 2014

Topics (non-exhaustive) Binary. ASCII. Algorithms. Pseudocode. Source code. Compiler. Object code. Scratch. Statements. Boolean expressions. Conditions. Loops. Variables. Functions. Arrays. Threads. Events. Linux. C. Compiling. Libraries. Types. Standard output. Casting. Imprecision. Switches. Scope. Strings. Arrays. Cryptography. Command-line arguments. Searching. Sorting. Bubble sort. Selection sort. Insertion sort. O. Ω.Θ. Recursion. Merge Sort. Stack. Debugging. File I/O. Hexadecimal. Strings. Pointers. Dynamic memory allocation. Heap. Buffer overflow. Linked lists.

Official Word http://cdn.cs50.net/2014/fall/quizzes/0/about0.html

Tips for Quiz 0 practice coding on paper (e.g., strlen, atoi) be familiar with your problem sets! do previous quizzes under time constraint creating your reference sheet is a great way to study

Data Types and Sizes char : 1 byte int : 4 bytes long long : 8 bytes float : 4 bytes double : 8 bytes <type>* : 4 bytes

Binary conversion: binary to decimal 1010102 = addition: 0 1 0 0 1 + 1 0 0 1 1 decimal to binary 5010 =

Binary conversion: binary to decimal 1010102 = 42 addition: 1 1 0 1 0 0 1 + 1 0 0 1 1 1 1 1 0 0 decimal to binary 5010 = 110010

Hexadecimal conversion: binary to hexadecimal 111111112 = hexadecimal to binary 0x5A =

Hexadecimal conversion: binary to hexadecimal 111111112 = 0xFF hexadecimal to binary 0x5A = 01011010

ASCII - Math Because characters are fundamentally just numbers, we can do math with chars! int A = 65; int B = 'A' + 1; char C = 'D' - 1; char D = 68; printf("%c %c %c %c", A, B, C, D); What will this print out?

ASCII - Math Because characters are fundamentally just numbers, we can do math with chars! int A = 65; int B = 'A' + 1; char C = 'D' - 1; char D = 68; printf("%c %c %c %c", A, B, C, D); What will this print out? A B C D

Scope Determines the region where a variable exists. Within this area, we can access or change the variable Global Entire program has access to it Exist for the duration of the program Local Confined to a region Examples: Within specific functions, if statements, for loops

What happens when we try to print at each point? int global_variable = 0; int main(void) global_variable = 5; if (global_variable == 5) int local_variable = 3; printf("%i\n", local_variable); printf("%i\n", local_variable); printf("%i\n", global_variable);

Prototypes When we define a function after we plan to use it, we must include a prototype! <return type> function_name(input list);

#include <stdio.h> int cube(int input); int main(void) int x = 2; printf("x is %i\n", x); x = cube(x); printf("x is %i\n", x); int cube(int input) return input * input * input;

Floating Point Imprecision infinitely many real numbers (even between 0 and 1!) but finitely many bits to represent real numbers imprecision

Structs Allow us to create our own data type or container to hold data of different types typedef struct int id; string name; student;

Creating and Accessing Structs Declare using the struct name as the variable type Access using the. operator int main(void) student student_1; student_1.id = 1; student_1.name = "Daven";

Creating and Accessing Structs if we have a pointer to a struct we can use -> notation int main(void) student student_1; student* ptr = &student_1; ptr->name = "Rob"; (*ptr).name = "Daven";

Pointers

Memory 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB

Creating Pointers <type>* <variable name> Examples: int* x; char* y; float* z;

Referencing and Dereferencing Referencing: &<variable name> Dereferencing: *<pointer name>

Under the hood... int x = 5; int* ptr = &x; int copy = *ptr; Variable Address Value x 0x04 5 ptr 0x08 0x04 copy 0x0C 5

#include <stdio.h> void to_five(int a) 3: a = 5; 4: int main(void) 1: int x = 3; 2: to_five(x); 5: printf("%d\n", x); Buggy x 1: 2: 3: 4: 5: a

#include <stdio.h> void to_five(int a) 3: a = 5; 4: int main(void) 1: int x = 3; 2: to_five(x); 5: printf("%d\n", x); Buggy 1: 2: 3: 4: 5: x a

#include <stdio.h> void to_five(int a) 3: a = 5; 4: int main(void) 1: int x = 3; 2: to_five(x); 5: printf("%d\n", x); Buggy x a 1: 2: 3 3: 4: 5:

#include <stdio.h> void to_five(int a) 3: a = 5; 4: int main(void) 1: int x = 3; 2: to_five(x); 5: printf("%d\n", x); Buggy x a 1: 2: 3 3: 3 3 4: 5:

#include <stdio.h> void to_five(int a) 3: a = 5; 4: int main(void) 1: int x = 3; 2: to_five(x); 5: printf("%d\n", x); Buggy x a 1: 2: 3 3: 3 3 4: 3 5 5:

#include <stdio.h> void to_five(int a) 3: a = 5; 4: int main(void) 1: int x = 3; 2: to_five(x); 5: printf("%d\n", x); Buggy x a 1: 2: 3 3: 3 3 4: 3 5 5: 3

#include <stdio.h> void to_five(int* a) 3: *a = 5; 4: int main(void) 1: int x = 3; 2: to_five(&x); 5: printf("%d\n", x); Fixed Assume &x == 0x12 x 1-> 2-> 3-> 4-> 5-> a *a

#include <stdio.h> void to_five(int* a) 3: *a = 5; 4: int main(void) 1: int x = 3; 2: to_five(&x); 5: printf("%d\n", x); Fixed Assume &x == 0x12 1-> 2-> 3-> 4-> 5-> x a *a

#include <stdio.h> void to_five(int* a) 3: *a = 5; 4: int main(void) 1: int x = 3; 2: to_five(&x); 5: printf("%d\n", x); Fixed Assume &x == 0x12 x a *a 1-> 2-> 3 3-> 4-> 5->

#include <stdio.h> void to_five(int* a) 3: *a = 5; 4: int main(void) 1: int x = 3; 2: to_five(&x); 5: printf("%d\n", x); Fixed Assume &x == 0x12 x a *a 1-> 2-> 3 3-> 3 0x12 3 4-> 5->

#include <stdio.h> void to_five(int* a) 3: *a = 5; 4: int main(void) 1: int x = 3; 2: to_five(&x); 5: printf("%d\n", x); Fixed Assume &x == 0x12 x a *a 1-> 2-> 3 3-> 3 0x12 3 4-> 5 0x12 5 5->

#include <stdio.h> void to_five(int* a) 3: *a = 5; 4: int main(void) 1: int x = 3; 2: to_five(&x); 5: printf("%d\n", x); Fixed Assume &x == 0x12 x a *a 1-> 2-> 3 3-> 3 0x12 3 4-> 5 0x12 5 5-> 5

Pointer Arithmetic Adding/subtracting i adjusts the pointer by i * sizeof(<type of the pointer>) bytes x int x = 5; int* y = &x; y += 1; 5 y

Pointer Arithmetic Adding/subtracting i adjusts the pointer by i * sizeof(<type of the pointer>) bytes x int x = 5; 5 int* y = &x; 5 y += 1; y 0x04

Pointer Arithmetic Adding/subtracting i adjusts the pointer by i * sizeof(<type of the pointer>) bytes x y int x = 5; 5 int* y = &x; 5 0x04 y += 1; 5 0x08

Pointers and Arrays int array[3]; *array = 1; *(array + 1) = 2; *(array + 2) = 3; 0 1 2 1 2 3

Memory stack: block of memory set aside when a program starts running each function gets its own stack frame stack overflow: when the stack runs out of space, results in a program crash and potential security concerns heap: region of unused memory that can be dynamically allocated using malloc don t forget to free dynamically allocated memory to prevent memory leaks

MBuffer Overflowemory Memory adapted from http://en.wikipedia.org/wiki/stack_buffer_overflow

MBuffer Overflowemory Memory adapted from http://en.wikipedia.org/wiki/stack_buffer_overflow

Allocating Memory void* malloc(<size in bytes>); int* ptr = malloc(sizeof(int) * 10); free(ptr); **Don t forget to check for NULL!**

Common Error Messages segmentation fault when a program attempts to access memory that it is not allowed to access check for NULL! implicit declaration of function when a program is defined after the main function, and no prototype is present above undeclared identifier when a variable has not been declared

Recursion a programming concept whereby a function invokes itself don t forget to include a base case! pros: can lead to more concise, elegant code some algorithms lend themselves to recursion e.g. merge sort

Search and Sort Run Times linear search binary search bubble sort selection sort insertion sort merge sort O n log(n) n2 n2 n2 n log(n) Ω 1 1 n n2 n n log(n) Θ O Ω Θ n2 upper bound (in the worst case) lower bound (in the best case) identical upper and lower bound (in both cases) n log(n)

Linked Lists 2 head 3 9

Nodes n next typedef struct node int n; struct node* next; node;

Search 2 head 3 9

bool search(node* list, int n) // points at current node node* ptr = list; // traverse the list until the end while (ptr!= NULL) // check if we found value if (ptr->n == n) return true; // move on to next element ptr = ptr->next; return false;

Insertion 2 head 1 3 9

Insertion 2 head 1 3 9

void insert(int n) // create new node node* new = malloc(sizeof(node)); // check for NULL if (new == NULL) exit(1); // initialize new node new->n = n; new->next = NULL; // insert new node at head new->next = head; head = new;

GDB allows you to execute a program line by line and see what is going on inside it a few important commands: break run next step print continue

Who needs a computer? Code by hand: strlen() atoi() toupper() pow() GetPositiveInt() RandomInt()

Questions?

And finally... RELAX AND SLEEP! (you ll do great! =D )