CSE 220 - Final Study Guide
CSE 220 - Final Study Guide CSE 220
Popular in Programming for Computer Engr
Popular in Computer Science and Engineering
This 20 page Study Guide was uploaded by RianMartins on Wednesday December 2, 2015. The Study Guide belongs to CSE 220 at Arizona State University taught by Tadayon-Navabi in Fall 2015. Since its upload, it has received 185 views. For similar materials see Programming for Computer Engr in Computer Science and Engineering at Arizona State University.
Reviews for CSE 220 - Final Study Guide
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 12/02/15
CSE 220 FIRST WEEK OF NOTES UNIX COMMANDS 0 You may be already a little familiar with the basic unix commands They are the same we use in the command prompt o If you are not familiar at all with them don t worry Let s work on that 0 Changing Directories To change directories we use the command cd We can use the command to enter directories by using the relative or absolute path we are going to get to that latter Keep in mind some useful trucks dot means your current directory and dot dot means the parent directory of your current directory Ex f am at homefredexam1 directory and type cd it will remain in the same directory that I am already in But if I type cd it will enter in the directory fred exam1 s parent 0 Relative vs Absolute Path Relative Path A relative path is defined as path related to the present working directory pwd 0 Ex Suppose I am located in varlog and I want to change directory to varlogkernel I can use relative path concept to change directory to kernel just type quotcd kernel Absolute Path An absolute path is defined as the specifying the location of a file or directory from the root directory In other words we can say absolute path is a complete path from start of actual filesystem from directory 0 Ex Using the same example above using absolute path I would type quotcd varlogkernel 0 List content of directory To list the content of your directory we use the Is command If we type just Is it will list only the name of the files and directories in your current directory Although there are some other options that we may use along the Is o ls I will list all the content and some more information about each file or directory like permissions date last modified file size and others o Is a will list all hidden files if any 0 You can also combine the options Typing ls al you list all the files and directories including the hidden elements and also be able to see their information 0 Change files permissions The permissions of the files are shown separated in three groups owner group owner and everybody Each group may have independent permissions 0 Ex When we see rwxrwr we should look at it like rwx rw r o The first will indicate when is a directory or not r wx are the permissions for the owner of the file that means that he can Read Write and eXecute the file r w are the permissions for the groupowner that means that they can Read and Write but cannot eXecute it And r are the permissions for everybody else That means that if you are not the owner or makes part of the groupowner you can only Read the file I To change the file permissions we use the chmod command I It is possible to use this command in two different ways chmod 775filename Changes the permission of file to be rwx for the owner and rx for the group and the world 7 rwx111 in binary 5r x101 chmod xfilename That will make the file executable for all the three groups 0 Globing Wild Cards I star Some cases you need to use globing patterns to remove more than one file for example The means anything Ex f have file01h file02h file11h file111h file3txt and templatec in my current working directory If I type rm f I will remove all the files starting with lowercase F Using rm fh I will remove all the files starting with lowercase F it could have anything after it in its name and have extension h I question mark The is used to replace only one character Ex Suppose I have the same files described above f type quotrm file1h I will remove the filesfile01h and file11h f use quotrm file1h I will remove the files file01hfile11h and file111h 0 Copying Moving Renaming files I Copying To copy files use quotcpquot command cp testtxt This command will copy the file testtxt present in the current working directory and paste in its parent directory cp testtxtusersfred This command will copy the file testtxt present in the current working directory and paste in usersfred directory cp usersbarneydocumentsfootxt This command will copy the file footxt present in under the folder documents of Barney and paste in your current working directory I Moving To move files use quotmvquot command mv testtxt This command will move the file testtxt present in the current working directory to its parent directory mv testtxtusersfred This command will move the file testtxt present in the current working directory to usersfred directory mv usersbarneydocumentsfootxt This command will mve the file footxt present in under the folder documents of Barney to your current working directory I Renaming files To rename a file use the quotmvquot command as well o mv testtxt testCompetetxt This command will rename the existing file test txt to testCompete txt CSE 220 NOTES NOTES FROM CLASS ABOUT CONST KEYWORD 10302015 The keyword constis used to indicate that a member function does not modify an object of class const can be used with objects 0 const Time noon 120 0 Only const member function can call const objects This is true even for get member functions that does not modify the object A const member function can be overloaded with a nonconst version The compiler based on the object on which the function is invoked 0 Const object can call a constant member function 0 Nonconst object can call a nonconstant member function Objects of the same time can be assigned to one another using default memberwise assignment Time t1 8 10 5 Time t2 t2 t1 A member function should not return a private data member breaks the encapsulation of the class const used to indicate that a member function does not modify an object of class Compositio a form of reuse in which a class can have objects of other classes Friendship which enables a class designer to specify nonmember functions that can access a class nonpublic member Destructor is provided automatically 0 Performs termination housekeeping on an object of class before that object is destroyed 0 Default constructor CSE 220 NOTES POLYMORPHISM Know that any function which is intended to be called polymorphically must be declared as virtual in the base class Having a virtual function makes the class abstract An abstract class cannot be instantiated In practice an abstract class declares data members and functions that are inherited by objects of all derived classes A class is made abstract in C by declaring at least one pure virtual function similar to Java Interface A base class destructor should be virtual When you delete an object of a derived class via a pointer to the base class it will delete the object EX class Base public Base Baseint x Virtual Base class Derived public Base public Derived Derivedint x Derived CSE 220 EXAM 1 STUDY GUIDE GNULINUX LAB MANUAL 0 Current Working Directory I An easy way to check what is your current working directory is to use the command quotpwdquot it will show you the absolute path of the directory you are I The dot is used to refer to your current working directory and the notation dot dot is used to refer to the parent directory of your current working directory 0 Ex Suppose I type quotpwdquot and see that I am at afsasueduusers whenever I use dot it refer to the directory quotusersquot and the dot dot refers to quotasuedu directory 0 Relative vs Absolut Path I Relative Path A relative path is defined as path related to the present working directory pwd 0 Ex Suppose I am located in varlog and I want to change directory to varlogkernel I can use relative path concept to change directory to kernel just type quotcd kernel I Absolut Path An absolute path is defined as the specifying the location of a file or directory from the root directory In other words we can say absolute path is a complete path from start of actual filesystem from directory 0 Ex Using the same example above using absolute path I would type quotcd varlogkernel o Create Directory Use the quotmkdir command where is the name of your new directory 0 Create files Use the text editors to create files Just type the name of the text editor and the name of your file remember to specify the format I Pico 0 Ex quotpico testtxt will open the editor pico and after you type the text you want exit quotX and save your file named testtxt I Vi 0 Ex quotvi testtxt does the same of the example above 0 Remove files To remove file just type quotrm where is the name and format of the file that you want to remove 0 Remove Directories I Empty Directory Use the command quotrmdirquot followed by the name of the directory I Full Directory If the directory contain any element you will not able to remove by using quotrmdirquot command In that case you will need to use quotrm rf and the file name It will remove the directory and all its content 0 Globing Wild Cards I star Some cases you need to use globing patterns to remove more than one file for example The means anything 0 Ex f have file01h file02h file11h file111h file3txt and templatec in my current working directory If I type rm f I will remove all the files starting with lowercase F Using rm fh I will remove all the files starting with lowercase F it could have anything after it in its name and have extension h I question mark The is used to replace only one character 0 Ex Suppose I have the same files described above f type quotrm file1h I will remove the filesfile01h and file11h f use quotrm file1h I will remove the files file01hfile11h and file111h 0 Copying Moving Renaming files I Copying To copy files use quotcpquot command 0 cp testtxt This command will copy the file testtxt present in the current working directory and paste in its parent directory 0 cp testtxtusersfred This command will copy the file testtxt present in the current working directory and paste in usersfred directory 0 cp usersbarneydocumentsfootxt This command will copy the file footxt present in under the folder documents of Barney and paste in your current working directory I Moving To move files use quotmvquot command 0 mv testtxt This command will move the file testtxt present in the current working directory to its parent directory 0 mv testtxtusersfred This command will move the file testtxt present in the current working directory to usersfred directory 0 mv usersbarneydocumentsfootxt This command will mve the file footxt present in under the folder documents of Barney to your current working directory I Renaming files To rename a file use the quotmvquot command as well 0 mv testtxt testCompetetxt This command will rename the existing file test txt to testCompete txt Keep in Mind LinuxUnix are case sensitive so the command quotls L is different from quotls l GNULINUX LAB MANUAL Chapter 8 o Redirections to inputoutput I To execute a program and use another file as input use the lt less than sign 0 Ex f00 lt input I To execute a program using another file as input and redirect its output to another file use the gt greater than sign 0 Ex f00 lt input txt gt output txt I You can even redirect the output of a program to be an input of another program using the pipe sign 0 Ex f00 lt inputtxt foo GNULINUX LAB MANUAL Chapter 12 o Compiling programs I To compile a C program type gcc cfilenamec it will produce only the filenameo Compilation only I You can also compile the C program using gccfilenamec it will produce a file aout To run the program type aout I Another form to compile a C program is using gccfilenamec o progl It will compile the file and create an executable progl Type prog1 to run the program C PROGRAMMING BASICS o HelloWord Program Hello World program includeltstdiohgt main printfquotHello Worldquot 0 Preprocessor I Definition The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation It is called a macro processor because it allows you to define macros which are brief abbreviations for longer constructs I The C preprocessor provides some separate facilities one of them is the Inclusion of header files These are files of declarations that can be substituted into your program 0 Header Files A header file is a file with extension h which contains C function declarations and macro definitions and to be shared between several source files There are two types of header files the files that the programmer writes and the files that come with your compiler You request the use of a header file in your program by including it with the C preprocessing directive include like you have seen inclusion of stdioh header file which comes along with your compiler o printf method I The printf function is not part of the C language because there is no input or output defined in C language itself The printf function is just a useful function from the standard library of functions that are accessible by C programs The behavior of printf is defined in the ANSI standard fthe compiler that you are using conforms to this standard then all the features and properties should be available for you int i or d char c float f string 5 0 Assume that we have intintl intint2 charchn float flt string strl string str2 0 Ex printfquotPrint int d intl 0 Ex printfquotPrint char c chr 0 Ex printfquotPrint float f fit 0 Ex printfquotPrint string s str1 o scanf method The scanf function allows you to accept input from standard in which for us is generally the keyboard The scanf function can do a lot of different things but it is generally unreliable unless used in the simplest ways It is unreliable because it does not handle human errors very well But for simple programs it is good enough and easytouse Ex Ex The same table above is applicable for scanf scanf d ampint2 sca nf s ampstr2 Keep in Mind You need to include ltstdiohgt in the top of your c file to be able to use printf and scanf functions C PROGRAMMING PREPROCESSOR 0 Define a Preprocessor macro using define The C preprocessor modifies a source code file before handing it over to the compiler You39re most likely used to using the preprocessor to include files directly into other files or define constants but the preprocessor can also be used to create quotinlinedquot code using macros expanded at compile time and to prevent code from being compiled twice There are essentially three uses of the preprocessordirectives constants and macros Directives are commands that tell the preprocessor to skip part of a file include another file or define a constant or macro Directives always begin with a sharp sign and for readability should be placed flush to the left of the page All other uses of the preprocessor involve processing define39d constants or macros Typically constants and macros are written in ALL CAPS to indicate they are special as we will see 0 Example define PIPLUSONE 314 1 t define a constant PPLUSONE with value 314 1 o Conditional Compilation There are a whole set of options that can be used to determine whether the preprocessor will remove lines of code before handing the file to the compiler They include if elif else ifdef and ifndef An if or ifelifelse block or a ifdef or ifndef block must be terminated with a closing endif The if directive takes a numerical argument that evaluates to true if it39s non zero If its argument is false then code until the closing else elif of endif will be excluded 0 Example ifndef FILENAMEI I define FILENAMEI I code endif ifndef FILENAMEI I 0 Data Types int vs unsigned int 0 Unsigned can hold a larger positive value and no negative value 0 Unsigned uses the leading bit as a part ofthe value while the signed version uses the leftmostbit to identify ifthe number is positive or negative 0 signed integers can hold both positive and negative numbers char vs unsigned char 0 There39s no dedicated quotcharacter typequot in C language char is an integer type same in that regard as int short and other integer types char just happens to be the smallest integer type So just like any other integer type it can be signed or unsigned For variable of type char only 7 bits are available and its range is 127 to 127 2 7 1 For variable of type unsigned char all 8 bits are available and the range is O to 255 2quot8 1 o If you use char as character unsigned key word is completely ignored by the compiler just as comments are removed from your program 0 Scope amp Lifetime of variables see Practice Scope handout for examples available in BB I Lifetime Lifetime of any variable is the time for which the particular variable outlives in memory during running of the program I Scope The scope of any variable is actually a subset of lifetime A variable may be in the memory but may not be accessible though So the area of our program where we can actually access our entity variable in this case is the scope of that variable The scope of any variable can be broadly categorized into three categories 0 Global scope When variable is defined outside all functions It is then available to all the functions ofthe program and all the blocks program contains 0 Local scope When variable is defined inside a function or a block then it is locally accessible within the block and hence it is a local variable 0 Function scope When variable is passed as formal arguments it is said to have function scope C PROGRAMMING C FUNCTIONS amp SCOPE 0 Definitions and Syntaxes I Study chapter 3 of the book C How to Program 7th edition 0 Recursion I A recursive method is a method who calls itself I It should contain a base case and a recursive case 0 Base Case Is the part that check whenever it should get out the loop and return the expected value 0 Recursive case If the result is not right yet this part is responsible to call itself again I Please keep in mind that you must have those two cases and the Base case should be the first condition in your method I Example include ltstdiohgt int factorialint i ifi lt 1 base case always first return 1 return i factoriali 1 recursive case C PROGRAMMING C POINTERS O O Pointer operators I amp refers to memory address I refers to corresponding values Parsing arguments to functions by reference I In order to parse a pointer as argument you have to inform the type of the pointer in the function prototype and function header 0 Ex void cubeByReference int nPtr Relationship between arrays and pointers quotArrays and pointers are intimately related in C and often may be used interchangeably An array name can be thought of as a constant pointer Pointers can be used to do any operation involving array subscripting Assume that integer array b5 and integer pointer variable thr have been defined Because the array name without a subscript is a pointer to the first element of the array we can set thr equal to the address of the first element in array b with the statement thr b This statement is equivalent to taking the address of the array s first element as follows thr ampb 0 Array element b3 can alternatively be referenced with the pointer expression thr 3 The 3 in the expression is the offset to the pointer When the pointer points to the array s first element the offset indicates which array element should be referenced and the offset value is identical to the array subscript This notation is referred to as pointeroffset notation The parentheses are necessary because the precedence of is higher than the precedence of Without the parentheses the above expression would add 3 to the value of the expression thr ie 3 would be added to bO assuming thr points to the beginning of the array Just as the array element can be referenced with a pointer expression the address ampb 3 can be written with the pointer expression thr 3 The array itself can be treated as a pointer and used in pointer arithmetic For example the expression b 3 also refers to the array element b3 In general all subscripted array expressions can be written with a pointer and an offset In this case pointeroffset notation was used with the name of the array as a pointer The preceding statement does not modify the array name in any way b still points to the first element in the array Pointers can be subscripted like arrays If thr has the value b the expression thr 1 refers to the array element b1 This is referred to as pointersubscript notation C How to Program 7th edition Chapter 7 Section 9 CSE 220 STUDY GUIDE EXAM 2 Chapter 5 C functions gt Storage duration or lifetime of a variable During the execution ofthe program each object exists as a location in memory for a certain period called its lifetime There is no way to access an object before or after its lifetime For example the value of a pointer becomes invalid when the object that it references reaches the end of its lifetime In C the lifetime of an object is determined by its storage duration Objects in C have one of three kinds of storage duration static automatic or allocated C does not specify how objects must actually be stored in any particular system architecture but typically objects with static storage duration are located in a data segment of the program in memory while objects with automatic storage duration are located on the stack Allocated storage is memory that the program obtains at runtime by calling the malloc calloc and realloc functions gt Scope of a variable A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed There are three places where variables can be declared in C programming language 0 Inside a function or a block which is called local variables 0 Outside of all functions which is called global variables 0 In the definition of function parameters which are called formal parameters Local Variables Variables that are declared inside a function or block are called local variables They can be used only by statements that are inside that function or block of code Local variables are not known to functions outside their own The following example shows how local variables are used Here all the variables a b and c are local to main function Global Variables Global variables are defined outside a function usually on top of the program Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program A global variable can be accessed by any function That is a global variable is available for use throughout your entire program after its declaration The following program show how global variables are used in a program Formal Parameters Formal parameters are treated as local variables within a function and they take precedence over global variables gt Linkage of a variable defines whether the variable39s scope is restricted to the source code file in which it is defined or whether the variable is quotknownquot to other source code files External Linkage gt means global nonstatic variables and functions The variable can be used in other source code file Internal Linkage gt means static variables and functions with file scope The variable is static and can be used in the whole file but not in other files None Linkage gt means Local variables The variable has no linkage so it only can be used in its own scope gt Difference between block scope and file scope as it applies to global variablesconstants local variablesconstants function definitions and function declarations also called prototypes Variables declared outside the main function has a file scope If the declaration of a file scope identifier contains the storage class specifier static the identifier has internal linkage If the declaration of an identifier has file scope and no storageclass specifier its linkage is external Chapter 6 C arrays gt Initializer list to initializes the elements of a 1D array int num5 2 8 7 6 6 int num 2 8 7 6 6 does the same as the above line gt Difference between passing an entire 1D array to a function versus passing just a single element of a 1D array int myArray5 2 8 7 6 6 void foolint array foolmyArray pass an entire 1D array to a function void fooZint element fooZmyArray3 pass the fourth element of array Know That void fooint a and void fooint a are both syntacticallylegal ways to specify that the input parameter a is a 1D array of integers gt Use an initializer list to initialize the elements of a 2D array int array2d33 6 1 2 16 11 12 26 21 22 Chapter 7 C pointers Obs A pointer is the address in memory of something eg a variable or function and that the value stored in a pointer variable is a pointer gt How to define a pointer variable and how to make it point to a variable or function int x declare a int variable int pointer declare a pointer to an int variable pointer ampx makes the pointer point to the variable x Know That NULL is a preprocessor macro defined in stdioh and a few other C Standard Library header files and that NULL is equivalent to void O ie memory address 0 Know That It is legal to define a pointer variable as a void pointer ie void p p could be a local variable a global variable a structure member or a formal parameter but a void pointer can never be dereferenced eg p 10 or pi quotfooquot is a syntax error gt Use the address operator amp to obtain the address of a variable printf d ampx prints the address of variable x gt Use the indirection or dereference operator to access the memory location to which a pointer variable points printf d pointer prints the value of the variable x gt Use the sizeof operator sizeofmyArray returns the size of the array gt Define and use arrays of pointers int x4 declare a int array int pointer declare a pointer to an int variable pointer ampx makes the pointer point to the first position of the array printf d pointer2 prints the thirt element of the array x Chapter 8 C strings Know That A C string is a 1D array of characters with a null character 39039 stored in memory following the last character of the string Chapter 9 Formatted InputOutput gt Use printf to display characters c integers d and u floats f doubles If and strings s gt Use scanf to read characters integers floats doubles and strings from the keyboard Chapter 10 C structures gt Define a structure variable Know what a selfreferential structure is and how to define one A selfreferential structure is used to create data structures like linked lists stacks etc The main characteristic of a selfreferential structure is that at least one of its members is also a structure struct structname int data structname nextPtr gt Initialize a structure variable structname strName gt Access the data members of a structure variable using both the operator and the gt operator Know when to use and when to use gt To access a data member of a structure using a structure variable we should use To access a data member of a structure using a pointer we should use gt structname strPtr strPtr ampstrName printf d strNamedata prints the data member of the struct printf d strPtr gt data prints the data member of the struct gt How structures are passed byvalue as parameters to functions struct student int id char name2 float percentage void funcstruct student stRecord gt Use typedef to define an quotaliasquot for an existing data type typedef struct student Student typedef Student StudentPtr Chapter 11 C file Processing gt File is simply a sequential stream of bytes gt Open a text file for reading quotrquot or quotrtquot or writing quotwquot or quotwtquot using fopen FILE fp fp fopen inputtxt P read the file inputtxt fp fopen outputtxt w write in the file outputtxt Mode Purpose r or quotrtquot Open a text file for reading The file must already exist Open a text file for writing If the file already exists all the data will lost quotwquot or quotwtquot If It doesnt eXIst It WI be created gt Read data from or write data to a text file using fscanf and fprintf Once a file has been successfully opened you can read from it using 39Fscan39F or write to it using 39Fpr int39F These functions work just like scanF and pr int39F except they require an extra first parameter a FILE for the file to be readwritten char username9 int score while fscanfifp quots dquot username ampscore EOF fprintfofp quots dnquot username score10 Chapter 12 C Data Structures gt Dynamic memory allocation To use the memory allocation methods we have to first include ltstdlib hgt Dynamic memory allocation allows a program to obtain more memory space while running or to release space when no space is required gt Form Linked List using pointers struct linkList char data each linkList structure contains a character struct linkList nextPtr pointer to the next node typedef struct linkList LinkedList synonym for struct linkedList typedef LinkedList LinkedListPtr Synonym for LinkedList gt How to create a binary tree using selfreferential structure struct binTree char data each binaryTree structure contains a character struct binTree leftPtr pointer to the left child node struct binTree rightPtr pointer to the right child node typedef struct binTree BinaryTree synonym for struct binTree typedef BinaryTree BinaryTreePtr synonym for BinaryTree gt dynamically allocate memory with malloc and deallocate with free malloc Allocates requested size of bytes and returns a pointer first byte of allocated space StudentPtr nethr mallocsizeofStudent allocate the memory free dellocate the previously allocated space freenethr deallocate memory Chapter 13 C preprocessor gt include preprocessor directive Used to include necessaries libraries to the program include ltctypehgt gt define directive to create symbolic constants Used to create constants define const 10 constant named const with value of 10 gt Using define directive to define a macro In the C Programming Language the define directive allows the definition of macros within your source code These macro definitions allow constant values to be declared for use throughout your code Macro definitions are not variables and cannot be changed by your program code like variables You generally use this syntax when creating constants that represent numbers strings or expressions Obs Expressions should always be between round brackets define num 16 represents number define str constant string represents string define exp 162 represents expressions gt Use the ifdef preprocessor directive Chapter 14 miscellaneous topics ifndef mathh include ltmathhgt define mathh endif ifdef wIN32 define INTSIZE 32 else define INTSIZE 16 endif gt Compiling multiplesourcefile Programs Create a makefile to compile multiplesourcefile programs is always a good practice n1akefHe projectName source1c source2c sourceHeaderh gcc 0 project source1c source2c After create the makefile as the above example type make in the command line to compile the project gt extern and static do when applied to global variables When you declare a variable as static you are restricting it to the current source file If you declare it as extern you are saying that the variable exists but are declared somewhere else and if you don39t have it declared elsewhere without the extern keyword you will get a link error symbol not found
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'