COP 3223 Final Review

Similar documents
COP 3223 Final Review

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

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

Lectures 5-6: Introduction to C

C programming basics T3-1 -

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

CS 222: Pointers and Manual Memory Management

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

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

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

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


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

Fall 2018 Discussion 2: September 3, 2018

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

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

today cs3157-fall2002-sklar-lect05 1

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

C for C++ Programmers

CSCI 171 Chapter Outlines

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

SYSC 2006 C Winter 2012

C mini reference. 5 Binary numbers 12

Lectures 5-6: Introduction to C

CS 61c: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Course organization. Course introduction ( Week 1)

H192 Midterm 1 Review. Tom Zajdel

Procedural Programming & Fundamentals of Programming

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

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

Introduction to Computer Science Midterm 3 Fall, Points

File I/O. Arash Rafiey. November 7, 2017

Arrays and Pointers in C. Alan L. Cox

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

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

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

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

Multidimension array, array of strings

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

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

Final Intro to C Review

Flow Control. CSC215 Lecture

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

CS201 Some Important Definitions

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

ARRAYS(II Unit Part II)

Kurt Schmidt. October 30, 2018

Pointers, Dynamic Data, and Reference Types

CS201- Introduction to Programming Current Quizzes

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

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

EMBEDDED SYSTEMS PROGRAMMING Language Basics

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

Introduction to Programming Using Java (98-388)

First of all, it is a variable, just like other variables you studied

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

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)

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)

C introduction: part 1

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

Pointers in C/C++ 1 Memory Addresses 2

Type Conversion. and. Statements

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

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

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

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

Introduction to Scientific Computing and Problem Solving

ESC101N: Fundamentals of Computing End-sem st semester

Standard C Library Functions

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

CS107 Spring 2019, Lecture 4 C Strings

File IO and command line input CSE 2451

Euclid s algorithm, 133

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS1100 Introduction to Programming

CSC 2400: Computer Systems. Arrays and Strings in C

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

Write a C program using arrays and structure

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Compiling and Running a C Program in Unix

Object Oriented Pragramming (22316)

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Important Questions for Viva CPU

High Performance Programming Programming in C part 1

SAE1A Programming in C. Unit : I - V

211: Computer Architecture Summer 2016

CSE 374 Programming Concepts & Tools

C Programming Multiple. Choice

[0569] p 0318 garbage

Language Reference Manual

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

Transcription:

COP 3223 Final Review Jennifer Brown December 2, 2018 1 Introduction 1.1 Variables I. How can we store data in a program? Initializing variables with data types A. Which of these are valid names for variables? i. 9length Not valid ii. hello Not valid iii. IamASuperCoolName Valid iv. int Not valid v. a new int Valid vi. str1 Valid II. Which way does assignment go? Right to left A. Which of these are valid if I have an int n; in main? i. 3 = n; Not valid ii. n = 5; Valid 1.2 Arithmetic I. What order of operations does the C compiler follow? A. Is it the same or different than PEMDAS? The compiler follows PEM- DAS along with other precedence rules B. If we have an int n; in main, what goes into n? i. n = 3 + 4 / 2 * 7; 17 ii. n = (3 + 4) / 2 * 7; 21 iii. n = 4 * (1-8); -28 C. Is this valid? Neither of these are valid, there need to be a * operator if you want to multiply i. n = 4(1 + 2); ii. n = (3-2)(4 + 5); 1

1.3 Precedence I. Which operation executes first? A. i++; Post executes first B. ++i; II. What does the?: tertiary operator do? Simplified if statement A. What will these return? i. int a = 3, b = 1; (a > b)? a : b; Will return a ii. int a = 3, b = 5; (a > b)? a : b; Will return b B. Are there any other tertiary operators? No III. Which as higher precedence? A. Does the + execute before the =? Yes i. int b = 3 + 4; B. Additionally the + and = operators are... i. Unary? ii. Binary? Binary IV. Lets get fancy! What does the % operator do? A. How can we use mod to determine an even or odd number? Using % 2 B. How can we use mod to restrict range? Mod will restrict the numbers to 0 through n-1 C. What does mod do when we have rand()? Restricts what random numbers we get 1.4 Data Types I. What is a data type? Determines what type of data A. What type of data do these create? i. int Integer ii. float Floating decimal iii. double Double float iv. int * Integer pointer v. void No data, used in functions 2

1.5 Conversion Specifiers I. What is a conversion specifier (format code etc)? Changes the way data is read or written A. What do the following codes display in a printf? 2 Functions 2.1 Main i. %d Decimal integer ii. %f Floating point iii. %c Character iv. %lf Double/long float v. %p Pointer address I. Let s break down the main function! A. int main(void) { return 0; } i. What data type does main return? Integer ii. What parameters does main take in? None iii. What does main return? 0 iv. Can I have no main function? No v. Can I have 2 main functions? No II. What is this clever little void we keep seeing? return type or no parameters Used when there is no III. What is the difference between pass by value and pass by reference? Pass by value passes the values variables contain, pass by reference passes the addresses fo the variables 2.2 Writing Functions and Function Prototypes I. How do we build our own functions? We define them in our code A. What do we need? i. Data type What will be returned ii. Name The function name iii. Parameters What values are taken in by the function iv. Return Unless void, must 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); 3

i. What parameters do they take in? Swamp function takes in two pointers to integers, max takes in two integers ii. What do they return? Void returns nothing, int returns an integer III. Let s finish up the max function! A. The function max will find the biggest number and return that value Link 3 Conditionals 3.1 Decisions I. What keyword do we use when making decisions? If A. When do we execute a decisions? Will these execute? Only executes if statement is true i. int a = 3, b = 1; if (a > b) Will execute ii. int a = 3, b = 1; if (a < b) Will not execute iii. if (1) Will execute iv. if (0) Will not execute v. if (a = 1) Will execute always, since assignment was successful a. Oh no! Did we do this right? Will want to use the == instead 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? The compiler will see anything that is a 0 or equivalent as false and all other numbers as true II. What if we want to compare more than one thing at a time? Must use logical operators A. What do these operators do? i. && Logical AND ii. Logical OR 4

B. If we have a and the first comparison is true, do we check the second one? Does it matter? We do not i. Likewise, if we have a &&, and the first comparison is false, do we compare the second? Does it matter? We do not ii. What is this called? Short circuiting 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! Link 4 Loops When we want to make a decision over and over again, we use loops. What are different types of loops we have? While, do while, for 4.1 While Loop I. How does the while loop work? while (condition) A. What happens in these loops? 4.2 For Loop i. int i = 3; while (i!= -1) printf( %d, i ; Loops until -1 is reached ii. while (i!= 1); { printf( Hurray!); i = 1; } a. Oh no! This looks like it would work, but it doesn t! What happened? The ; after the loop causes the loop to infinitely run I. How does the for loop work? for(pre; condition; post) A. What happens in these loops? i. for (i = 0; i < 5; i++) printf( %d, i); Prints 0-4 ii. for(;;) 5

4.3 Do While Loop a. This looks a little funky, is this still valid? Yes, but will loop infinitely b. What is the minimum thing that for loops require?two semicolons I. How does the do while loop work? Executes the do first and then checks the condition 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? Will not execute, only prints the print statement b. Does this execute at least once? Yes c. How is it different than a while loop? Always executes once 5 Break/Continue 5.1 Break I. What do we do when we want to leave a loop early? Break A. Let s write loop that adds to a total as long as the numbers are even Link 5.2 Continue I. What do we do when we want to skip a certain case and continue looping? continue statement A. Let s write a loop that keeps looping as long as the user doesn t put in negative numbers, but skips odds. Link 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++ } Must add i++ after the if statement 6

6 Switch 6.1 Decisions 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? Switch keyword, and the cases i. Let s write a switch that takes in a letter grade and tells the user what range that grade will be in. Link 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? This will print both a and b, needs a break statement b. What is so important about break in switch statements? Stops the cases from falling through C. Is the default case in a switch statement important? To what is it similar to in if/else if/else? The default case is the catch all at the end, used for unexpected values, and is simliar to else 7 Pointers 7.1 Data Type I. What type of data does a pointer contain? An address A. How do you initialize a pointer? Data type and then * 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? We are attempting to dereference an uninitialized pointer, and placing the 5 into a random place in memory 7

b. What is dereferencing, and how can we do it without self destructing our code? Dereferencing means going to the address at which a pointer points to and then changing the value, this code can be fixed by changing int *p to int *p = &a ii. int a; int *p = NULL; *p = 5; a. Oh no! We also seg faulted here, what happened? Simliarly here, we tried to place a 5 into nowhere, and the compiler cannot find a place to put it b. Can we dereference a null pointer? We will seg fault 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? Link 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? We want to return the max of the two 7.3 Pointer pointers I. What type of data does a pointer to a pointer contain? An address to an address that point to a data type 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 = &a; int **q = &p; **q = 5; C. What s wrong with the following code? i. int a; int *p; int **q; **q = 5; 8

a. This code is spooky scary, why is that? We have two uninitialized pointers, and neither points to a non-random address, see question above for fix 8 File I/O I. What is File I/O? reading and writing from a file outside the program A. What code would we use to open a.txt file? We need both FILE *pointer name and fopen 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(); a. Syntax? fopen(name, mode) ii. fscanf(); a. Syntax? fscanf(file pointer, converstion specifier, destination iii. fgets(); a. Syntax? fgets(destination, length, file pointer iv. fprintf(); a. Syntax? fprintf(file pointer, converstion specifier, source v. fputs(); a. Syntax? fputs(source, file pointer) vi. fclose(); a. Syntax? fclose(file pointer) 8.2 Reading a file I. What code do we write to read from a.txt file? fopen, fscanf/fgets, fclose A. What functions do we need? i. fopen? Yes ii. fscanf? Possible iii. fgets? Possible iv. fprintf? This function writes to a file v. fputs? This function writes to a file vi. fclose? Yes B. Let s write a function that reads names from a file and then prints them. Link 9

9 ASCII 9.1 Do characters have values? I. If I have the letter/char a, what value would it have? 97 A. Look at the following code: i. char a = a ; printf( %d, a); a. What do you think it will print? This will print the ASCII value of a, 97 B. What about this? 9.2 ASCII Table i. int a = 97; printf( %c, a); This will print the what ASCII letter the integer has a. Crazy magic! This code is valid, why is that? This is valid because all characters have set values 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. Link A. What value is 1? 49 B. What value is A? 65 C. What value is a? 97 10 Arrays and Strings 10.1 Arrays I. What is an array? An array is a complex data type that contains cells of a primitive data type consecutively A. Is the data in an array consecutive? Yes i. How do i initialize an array? datatype arrayname[numberofcells]; ii. Where does the index of an array always start? 0 II. What will the following code do? A. int arr[5] = {1, 2, 3}, i; Initializes an array with 5 cells in it 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? This will print 12300 10

10.2 Passing arrays I. If I want to pass an array to a function, what do I send in the function call? The array name A. What parameter will hold what we are sending? We will need a pointer of the array s datatype 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? The address of the 0 index of the array, or the beginning i. IE: int array[5] = {4, 7, 6, 9, 1}, n = 5; max(array, n); a. What does the max prototype look like? int max(int *array, n) II. When passing arrays, why do we need a pointer? Arrays are a type of pointer A. Let s write that max function! i. Takes in an array and a length ii. Returns the max value in that array Link 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 Link I. How are strings different from arrays? Strings are an array of characters that also contain a null terminator /0 II. How are are they the same? They are still both arrays A. What data type are the cells in a string? char B. What extra value do we have at the end of a string? The null terminator i. What happens if our array is too small to hold the null terminator? The compiler will not know when the string ends C. When we pass a string to a function, is it similar to an array? Yes, it will utilize a pointer i. What is different? Must be datatype char D. What will the following code print? 11

i. char str[6] = hello ; int i; for (i = 0; i < 6; i++) printf( %c str[i]); This will print each character of the string E. Let s write a function that finds the smallest alphabetical letter. IE: a is smaller than b Link F. Let s write a function that finds the biggest alphabetical letter. IE: z is bigger than y Link 10.4 String Functions I. What library do we need to use string functions? string.h II. What are some of the string functions that we can use? A. What do the following functions do? i. strlen(); Finds the length of the string a. Syntax? strlen(string) ii. strcmp(); Compares two strings, returns 0 if the same a. Syntax? strcomp(str1, str2) iii. strcpy(); Copies one string to another a. Syntax? strcpy(destination, source) iv. strcat(); Adds a string to the end of another strcat(destination, source) a. Syntax? v. strstr(); Finds if a string is inside another string, returns the string found a. Syntax? strstr(haystack, needle) B. When using string functions, what do we have to be careful of? Can we segmentation fault? We have to be careful we are not placing a string bigger than its destination i. What could go wrong with the following code? a. char str[6] = { hello }; strcat(str, helloooooo ); str is not big enough to add this to ii. What went wrong and why? Went out of bounds of memory 11 2D Arrays I. What does a 2D Array look like? A coordinate system A. How can we loop through a 2D array and set values to 0? We need to loop through the rows and the columns 12

i. Can we do this with one loop or do we need two? Need two ii. Are 2D arrays similar to a coordinate system? Yes B. When initializing a 2D array, it looks like: int arr[ ][ ]; i. Which spot sets the rows? The first ii. Which spot sets the columns? The second 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? rows b. Does j correspond to: rows or columns? columns 12 Dynamic Memory Allocation (DMA) I. Why do we want to use dynamic memory allocation instead of static? In case we do not know how big an array needs to be yet A. Do we always know how big we want an array to be? Not always! B. Do we always know how big we want a string to be? Not always! II. In order to create an array dynamically, what do we need first? We need a pointer and then a malloc call A. What functions do we use? malloc and calloc i. What does malloc do? Allocates a new space in memory ii. What does calloc do? redallocates a new space in memory and sets the values to 0 or NULL 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? Allocates a new space in memory each time with only one pointer. Once the pointer moves to the new one, the address of the old is lost! I. Why are structures used? Why are they super mega awesome? Structures allow us to create our own datatypes, this is really cool and awesome! They are used if we want information to be packaged together 13

A. Structures are known as abstract data types, why do you think they are called this? Because they are defined in code, and can be anything they dream to be II. Is memory allocated in the following code? Not yet, only defined A. struct StudentInfo { char *name int ID; } ; i. There is an error here, what is it? Needs a semi-colon at the end III. How do I make a new struct? The struct must be defined, and then we call the struct in main using the entire name, unless typdef was used A. What is wrong with the following code? i. int main(void) { StudentInfo newstudent; return 0; } This needs to have the word struct in front of it to be valid a. How can we change the struct so that this code is A-OK? We can typdef our struct: typdef struct StudentInfo { char *name int ID; } StudentInfo; B. How do we change the values in a struct when it s been initialized? We have to use variablename.datatype i. Let s change the ID to 1111111, how do we write that in code? newstudent.id = 1111111; ii. Let s make the name Aleks, how do we write that in code? newstudent.name = malloc((sizeof(char) * n) + 1); strcpy(newstudent.name, Aleks ); IV. Can we create a pointer to a struct? A. How would that look? How do we code that? Struct name *newvariable B. How would we change the data with a pointer? Since this is now a pointer to a struct, we must dereference first using * and then use the. i. Do we still need to dereference? Yes ii. Is there a sneaky shortcut we can use other than *.? We can use - instead C. What benefit is there in creating a pointer to a struct? We can now store those pointers into an array, and have them collected together in a database 14