Review of Exam #2CS-2301, B-Term 20091 Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming Languages and Paradigms The C Programming Language.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
MPI and C-Language Seminars Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques.
Homework #4CS-2301 B-term Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
Kernighan/Ritchie: Kelley/Pohl:
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Numerical Computation Review and Continuation CS-2301, B-Term Numerical Computation in C Review and Continuation CS-2301, System Programming for.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Strings, Arrays, and Pointers CS-2301 D-term Strings, Arrays, and Pointers CS-2301 System Programming C-term 2009 (Slides include materials from.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
Strings, Arrays, and Pointers CS-2301, B-Term Strings, Arrays, and Pointers CS-2301, System Programming for Non-Majors (Slides include materials.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 C++ Syntax and Semantics, and the Program Development Process.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Introduction to Data Structures Systems Programming Concepts.
Arrays in CCIS 1057 Fall Arrays in C (with a brief Introduction to Pointers) CIS 1057 Computer Programming in C Fall 2013 (Slides include materials.
C LANGUAGE Characteristics of C · Small size
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Windows Programming Lecture 03. Pointers and Arrays.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Function: Declaration
A bit of C programming Lecture 3 Uli Raich.
Chapter 16-2 Linked Structures
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Structures, Unions, and Typedefs
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Differences between Java and C
Introduction to Classes and Objects
Presentation transcript:

Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Review of Exam #2CS-2301, B-Term Results Maximum – 85 Mean – 51.2 Median – 47.5 Minimum – 29

Review of Exam #2CS-2301, B-Term Question 1 1. (10 points) Define or explain the following C language terms in the space provided:– subscript: The expression in square brackets following an array identifier (or array expression) that denotes which element of the array to select. See Kernighan and Ritchie, pp 22, 97, 99, 100 typedef: A declaration/definition that gives a new name to an existing data type. (Does not delete the old name.) malloc : A library function in C that allocates memory from the heap and returns a pointer to that memory. linked list: A set of data elements, generally all of the same type, that are allocated independently of each other that are linked together by pointers in a linear sequence. An element points to the next element — it does not “call” it! sizeof : A unary operator in C that returns the size (in bytes) of the result of an expression or of a data type. Applies to any type, not just a character array initialized by a string!

Review of Exam #2CS-2301, B-Term Question 2 1.(5 points) Evaluate the following expressions, where S as declared as follows:– char S[] = "A simple string.\n"; (a) strlen(S) 17. (b) sizeof(S) 18. (c) *S The character 'A'. (d) *(S+17) The null character; i.e., '\0'. (e) strcmp(S+16, "\n") 0 — answer sheet was incorrect!

Review of Exam #2CS-2301, B-Term Question 3 1. (5 points) Explain the purpose of the functions fopen() and fclose(). What is the type of the result returned by fopen() ? fopen() “opens” a file. That is, it prepares the file for access by the program. This means that the operating system finds the file and sets up its internal data structures so that the program can read, write, or append, according to the mode. fopen() returns a pointer to type FILE (which is defined in ). fclose() “closes” the file. That is, it writes out any data to the disk or file storage medium, cleans up the data structures that the system maintained for the file, and generally completes the operations of preserving the data on the disk or file system. What are stdin, stdout, stderr ?

Review of Exam #2CS-2301, B-Term Question 4 1.(10 points) Explain the loop in the following function — in particular, how it steps through all of the characters of the string and only the characters of the string. int isAllLowerCase(char *c) { //PRE:- c is a string of zero or more characters //POST:- returns 1 if all letters are lower case // returns 0 if any letter is upper case for (; *c; c++) { if (*c >= 'A' && *c <= 'Z') return 0; } return 1; }// int isAllLowerCase(...)

Review of Exam #2CS-2301, B-Term Question 5 1.(10 points) Let the function isAllLowerCase be declared as in Question 4. For each of the following expressions or statements, tell whether it compiles correctly and, if it does compile correctly, what is the result of attempting to execute it. (a) isAllLowerCase("Yes, we can. ") Compiles correctly and returns zero (b) isAllLowerCase('x') Does not compile because 'x' is not a pointer. (c) isAllLowerCase(0) Compiles correctly because 0 denotes the NULL pointer; causes a segmentation fault when executing. (b) isAllLowerCase(5) Does not compile because 5 is not a pointer. (e) char s[] = {'\0', 'A', 'b', 'C', 'd', 'E'}; printf("%d",isAllLowerCase(s)); Compiles correctly, Prints the number 1, because the function encounters the character '\0' before encountering any uppercase letters.

Review of Exam #2CS-2301, B-Term Question 6 1. (5 points) Explain why the following two function prototypes are identical. int main(int argc, char *argv[]); int main(int argc, char **argv); Pointers and arrays are the same thing in C. Specifically, argv[] and *argv both denote pointers, in this cases they are pointers to pointers to characters.

Review of Exam #2CS-2301, B-Term Question 7 (array version) 1. (20 points) Write an implementation of the C library function strcmp(char *s, char *t). See the cover sheet this exam for an explanation of this function. [Hint: there are many correct implementations of this function. Several were given in Kernighan and Ritchie.] /* array version */ int strcmp(char *s, char *t) { int i; for (i = 0; s[i] == t[i]; i++) if (s[i] == '\0') return 0; return s[i] – t[i]; } Issues:– one loop, not two Testing for equality “Art” versus “Arthur” Lexical order means dictionary order Testing for length, not comparing characters “Arthur” versus “Bob”

Review of Exam #2CS-2301, B-Term Question 7 (pointer version) 1. (20 points) Write an implementation of the C library function strcmp(char *s, char *t). See the cover sheet this exam for an explanation of this function. [Hint: there are many correct implementations of this function. Several were given in Kernighan and Ritchie.] /* pointer version */ int strcmp(char *s, char *t) { for (; *s == *t; s++, t++) if (*s == '\0') return 0; return *s – *t; } Issues (continued):– Some misread “implement” return < 0

Review of Exam #2CS-2301, B-Term Question 8 In a struct, memory is allocated for each, individual member. All members can be accessed. In a union, memory is allocated for only one member, namely the largest. Only one member can be stored in a union at any one time, and the program has to have a means of knowing which member is populated in each individual union.

Review of Exam #2CS-2301, B-Term Question 9a (a)Declare a struct to represent a list element for one motor, and define a typedef called motor to give a type name to that struct. Don’t forget to include a link to the next element on the list. typedef struct _motor { float voltage; float amps; float rpm; int phases struct _motor *next; } motor; Q: Why do we need this? A: To declare this.

Review of Exam #2CS-2301, B-Term Question 9b phases volts amps next rpm phases volts amps next rpm phases volts amps next rpm motor *first, *last;

Review of Exam #2CS-2301, B-Term Question 10a (a)Write a function called addMotor() that adds a new element of type motor to the end of the list. Be sure to deal with the case where the list is empty — i.e., where both first and last are NULL. The prototype for your function should be void addMotor(motor *newMotor) { if (newMotor) /*don’t do anything if newMotor is NULL*/ if (tail ) //i.e., list is not empty tail = tail -> next = newMotor; else head = tail = newMotor; }//addMotor Issues:– Many didn’t use “->” notation! Messed up “.” notation Messed up relinking the tail! My sloppiness – did not assign NULL to newMotor->next Notice this notation!

Review of Exam #2CS-2301, B-Term Question 10b (b)Write a function called getFirstMotor() that removes the first element from the list and returns a pointer to it. Be sure to deal with the cases where the list is empty and where the list has only one element. The prototype for your function should be motor *getFirstMotor(void) { motor *first = head; if (head) { //i.e., list is not empty head = head-> next; if (head == NULL) tail = NULL; /*i.e., list has become empty */ } return first; }//getFirstMotor Notice this notation! My sloppiness:– Should be if (head != tail) head = heal -> next; else head = tail = NULL;

Review of Exam #2CS-2301, B-Term Questions? On the Exam Other Review questions?

Review of Exam #2CS-2301, B-Term Project #6 Assignment