COP 3223 Final Review

Similar documents
COP 3223 Final Review

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

C programming basics T3-1 -

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

Lectures 5-6: Introduction to C

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

CSCI 171 Chapter Outlines

SYSC 2006 C Winter 2012

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

C mini reference. 5 Binary numbers 12

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

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

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Multidimension array, array of strings

today cs3157-fall2002-sklar-lect05 1

C for C++ Programmers

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS 222: Pointers and Manual Memory Management

For example, let s say we define an array of char of size six:

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Important Questions for Viva CPU


Euclid s algorithm, 133

H192 Midterm 1 Review. Tom Zajdel

ARRAYS(II Unit Part II)

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

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

Language comparison. C has pointers. Java has references. C++ has pointers and references

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Standard C Library Functions

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

Pointers, Dynamic Data, and Reference Types

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Lectures 5-6: Introduction to C

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Advanced C Programming Topics

CS1100 Introduction to Programming

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Final Intro to C Review

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

CS201 Some Important Definitions

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

BASIC ELEMENTS OF A COMPUTER PROGRAM

Fall 2018 Discussion 2: September 3, 2018

Arrays and Pointers in C. Alan L. Cox

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Computers Programming Course 10. Iulian Năstac

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

High Performance Programming Programming in C part 1

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

CS 61c: Great Ideas in Computer Architecture

There are functions to handle strings, so we will see the notion of functions itself in little a detail later. (Refer Slide Time: 00:12)

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CSC 2400: Computer Systems. Arrays and Strings in C

Arrays and Pointers (part 1)

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

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

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

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

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

EL2310 Scientific Programming

Arrays and Pointers (part 1)

Introduction to Scientific Computing and Problem Solving

CSC 2400: Computer Systems. Arrays and Strings in C

Continued from previous lecture

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CS 115 Exam 3, Spring 2010

C-LANGUAGE CURRICULAM

8. Characters, Strings and Files

Arrays, Strings, & Pointers

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CSE 374 Programming Concepts & Tools

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Intermediate Programming, Spring 2017*

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

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

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

Introduction to C Language (M3-R )

Subject: Fundamental of Computer Programming 2068

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Memory, Arrays & Pointers

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Expressions and Casting

211: Computer Architecture Summer 2016

Transcription:

COP 3223 Final Review Jennifer Brown December 2, 2018 1 Introduction 1.1 Variables I. How can we store data in a program? A. Which of these are valid names for variables? i. 9length ii. hello iii. IamASuperCoolName iv. int v. a new int vi. str1 II. Which way does assignment go? A. Which of these are valid if I have an int n; in main? i. 3 = n; ii. n = 5; 1.2 Arithmetic I. What order of operations does the C compiler follow? A. Is it the same or different than PEMDAS? B. If we have an int n; in main, what goes into n? i. n = 3 + 4 / 2 * 7; ii. n = (3 + 4) / 2 * 7 iii. n = 4 * (1-8); C. Is this valid? i. n = 4(1 + 2); ii. n = (3-2)(4 + 5); 1

1.3 Precedence I. Which operation executes first? A. i++; B. ++i; II. What does the?: tertiary operator do? A. What will these return? i. int a = 3, b = 1; (a > b)? a : b; ii. int a = 3, b = 5; (a > b)? a : b; B. Are there any other tertiary operators? III. Which as higher precedence? A. Does the + execute before the =? i. int b = 3 + 4; B. Additionally the + and = operators are... i. Unary? ii. Binary? IV. Lets get fancy! What does the % operator do? A. How can we use mod to determine an even or odd number? B. How can we use mod to restrict range? C. What does mod do when we have rand()? 1.4 Data Types I. What is a data type? A. What type of data do these create? i. int ii. float iii. double iv. int * v. void 2

1.5 Conversion Specifiers I. What is a conversion specifier (format code etc)? A. What do the following codes display in a printf? i. %d ii. %f iii. %c iv. %lf v. %p 2 Functions 2.1 Main I. Let s break down the main function! A. int main(void) { return 0; } i. What data type does main return? ii. What parameters does main take in? iii. What does main return? iv. Can I have no main function? v. Can I have 2 main functions? II. What is this clever little void we keep seeing? III. What is the difference between pass by value and pass by reference? 2.2 Writing Functions and Function Prototypes I. How do we build our own functions? A. What do we need? i. Data type ii. Name iii. Parameters iv. Return II. Let s look at the following function prototypes A. void swap(int *a, int *b); B. int max int(int x, int y); i. What parameters do they take in? ii. What do they return? III. Let s finish up the max function! A. The function max will find the biggest number and return that value 3

3 Conditionals 3.1 Decisions I. What keyword do we use when making decisions? A. When do we execute a decisions? Will these execute? i. int a = 3, b = 1; if (a > b) ii. int a = 3, b = 1; if (a < b) iii. if (1) iv. if (0) v. if (a = 1) a. Oh no! Did we do this right? B. What comparisons can we make? i. >=, <= ii. ==,!= iii. <, > 3.2 Boolean Logic I. We saw some tricky numbers in our decisions! Let s review Boolean logic, what does the compiler see as true and as false when we get sneaky numbers? II. What if we want to compare more than one thing at a time? A. What do these operators do? i. && ii. B. If we have a and the first comparison is true, do we check the second one? Does it matter? i. Likewise, if we have a &&, and the first comparison is false, do we compare the second? Does it matter? ii. What is this called? 3.3 If/Else if/else I. What keyword do we use if we want to execute when the condition is false? A. Let s write a nested if/if else/else that determines the age of the user, and then tells them if they can vote, drink alcohol, both, or neither. We fancy! 4

4 Loops When we want to make a decision over and over again, we use loops. What are different types of loops we have? 4.1 While Loop I. How does the while loop work? A. What happens in these loops? 4.2 For Loop i. int i = 3; while (i!= -1) printf( %d, i ; ii. while (i!= 1); { printf( Hurray!); i = 1; } a. Oh no! This looks like it would work, but it doesn t! What happened? I. How does the for loop work? A. What happens in these loops? i. for (i = 0; i < 5; i++) printf( %d, i); ii. for(;;) a. This looks a little funky, is this still valid? b. What is the minimum thing that for loops require? 4.3 Do While Loop I. How does the do while loop work? A. What happens in these loops? i. do printf( Yasssss ); while (0); a. Oh lookie, we have some tricksy numbers again in our loops. What do we do? b. Does this execute at least once? c. How is it different than a while loop? 5

5 Break/Continue 5.1 Break I. What do we do when we want to leave a loop early? A. Let s write loop that adds to a total as long as the numbers are even 5.2 Continue I. What do we do when we want to skip a certain case and continue looping? A. Let s write a loop that keeps looping as long as the user doesn t put in negative numbers, but skips odds. 6 Switch 6.1 Decisions i. Depending on your logic, continue can very easily cause nasty infinite loops. Let s fix the following code: a. int i = 0; while (i < 7) if (i == 2) continue; i++ } I. Now that we are master of if/else if/else, let s explore another way to do this with switch statements A. How do we write a switch? i. Let s write a switch that takes in a letter grade and tells the user what range that grade will be in. B. in the following code, what will happen? i. char a = a ; switch(a) { case a : printf( a ); case b : printf( b ); } a. Something horrible happened! What happened and how do we fix it? b. What is so important about break in switch statements? C. Is the default case in a switch statement important? To what is it similar to in if/else if/else? 6

7 Pointers 7.1 Data Type I. What type of data does a pointer contain? A. How do you initialize a pointer? B. What does the following code do? i. int a; int *p; *p = 5; a. DANGER! Something VERY BAD has happened! What is it and why is it so VERY BAD? b. What is dereferencing, and how can we do it without self destructing our code? ii. int a; int *p = NULL; *p = 5; a. Oh no! We also seg faulted here, what happened? b. Can we dereference a null pointer? 7.2 Functions with Pointers I. We saw earlier that swap prototype, but we didn t write it! Now that we have reviewed pointers, we are ready to tackle it. A. Recall: void swap(int *a, int *b); i. Why do we need the pointers, and how can we finish up this function? B. Let s also write a findmax function using pointers i. Prototype: int findmax(int *x, int *y); ii. This can also be achieved via pass by value, but we want to be fancy and use pointers instead. iii. Why do we need to change the return value from void to int? 7.3 Pointer pointers I. What type of data does a pointer to a pointer contain? A. How do you initialize a pointer to a pointer? B. How do I use the q pointer to manipulate a in the following code? i. int a; int *p =???; int **q =???;??? 7

C. What s wrong with the following code? 8 File I/O i. int a; int *p; int **q; **q = 5; a. This code is spooky scary, why is that? I. What is File I/O? A. What code would we use to open a.txt file? 8.1 File I/O Functions I. What functions can we use once we have opened a file? A. What do the following functions do? i. fopen(); ii. fscanf(); iii. fgets(); iv. fprintf(); v. fputs(); vi. fclose(); 8.2 Reading a file I. What code do we write to read from a.txt file? A. What functions do we need? i. fopen? ii. fscanf? iii. fgets? iv. fprintf? v. fputs? vi. fclose? B. Let s write a function that reads names from a file and then prints them. 8

9 ASCII 9.1 Do characters have values? I. If I have the letter/char a, what value would it have? A. Look at the following code: i. char a = a ; printf( %d, a); a. What do you think it will print? B. What about this? 9.2 ASCII Table i. int a = 97; printf( %c, a); a. Crazy magic! This code is valid, why is that? I. Let s write a function that prints the ASCII values of 33-126. First it will print the integer value and then the character. A. What value is 1? B. What value is A? C. What value is a? 10 Arrays and Strings 10.1 Arrays I. What is an array? A. Is the data in an array consecutive? i. How do i initialize an array? ii. Where does the index of an array always start? II. What will the following code do? A. int arr[5] = {1, 2, 3}, i; for(i = 0; i < 5; i++) printf( %d, arr[i]); i. If I have the code arr[5]; what values are in the cells of the array? 9

10.2 Passing arrays I. If I want to pass an array to a function, what do I send in the function call? A. What parameter will hold what we are sending? B. If I have an array called array and I send it to a max function (returns the max value in the array), what am I technically sending? i. IE: int array[5] = {4, 7, 6, 9, 1}, n = 5; max(array, n); a. What does the max prototype look like? II. When passing arrays, why do we need a pointer? A. Let s write that max function! i. Takes in an array and a length ii. Returns the max value in that array B. Let s write a min function 10.3 Strings i. Takes in an array and a length ii. Returns the max value in that array I. How are strings different from arrays? II. How are are they the same? A. What data type are the cells in a string? B. What extra value do we have at the end of a string? i. What happens if our array is too small to hold the null terminator? C. When we pass a string to a function, is it similar to an array? i. What is different? D. What will the following code print? i. char str[6] = hello ; int i; for (i = 0; i < 6; i++) printf( %c str[i]); E. Let s write a function that finds the smallest alphabetical letter. IE: a is smaller than b F. Let s write a function that finds the biggest alphabetical letter. IE: z is bigger than y 10

10.4 String Functions I. What library do we need to use string functions? II. What are some of the string functions that we can use? A. What do the following functions do? i. strlen(); ii. strcmp(); iii. strcpy(); iv. strcat(); v. strstr(); B. When using string functions, what do we have to be careful of? Can we segmentation fault? i. What could go wrong with the following code? a. char str[6] = { hello }; strcat(str, helloooooo ); ii. What went wrong and why? 11 2D Arrays I. What does a 2D Array look like? A. How can we loop through a 2D array and set values to 0? i. Can we do this with one loop or do we need two? ii. Are 2D arrays similar to a coordinate system? B. When initializing a 2D array, it looks like: int arr[ ][ ]; i. Which spot sets the rows? ii. Which spot sets the columns? C. When using loops to manipulate 2D arrays: i. If we have i for loop 1 and j for loop 2 a. Does i correspond to: rows or columns? b. Does j correspond to: rows or columns? 11

12 Dynamic Memory Allocation (DMA) I. Why do we want to use dynamic memory allocation instead of static? A. Do we always know how big we want an array to be? B. Do we always know how big we want a string to be? II. In order to create an array dynamically, what do we need first? A. What functions do we use? i. What does malloc do? ii. What does calloc do? III. What is wrong with the following code? A. int *p, i, n = 5; for (i = 0; i 5; i++) p = malloc(sizeof(int) * n); 13 Structures i. DANGER! Something very bad is happening here, what is it, and why? I. Why are structures used? Why are they super mega awesome? A. Structures are known as abstract data types, why do you think they are called this? II. Is memory allocated in the following code? A. struct StudentInfo { char *name int ID; } i. There is an error here, what is it? III. How do I make a new struct? A. What is wrong with the following code? i. int main(void) { StudentInfo newstudent; return 0; } a. How can we change the struct so that this code is A-OK? B. How do we change the values in a struct when it s been initialized? i. Let s change the ID to 1111111, how do we write that in code? 12

ii. Let s make the name Aleks, how do we write that in code? IV. Can we create a pointer to a struct? A. How would that look? How do we code that? B. How would we change the data with a pointer? i. Do we still need to dereference? ii. Is there a sneaky shortcut we can use other than *.? C. What benefit is there in creating a pointer to a struct? 13