CS 1713 Introduction to Programming II

Similar documents
CS 1713 Introduction to Programming II

CS Operating system

EECE.2160: ECE Application Programming Fall 2017

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

C & Data Structures syllabus

CS 223: Data Structures and Programming Techniques. Exam 2

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Administrative Stuff. More on Strings, File I/O. Last Time. Java API

More on Strings, File I/O. September 8, 2016

CS 0449 Sample Midterm

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

PRINCIPLES OF OPERATING SYSTEMS

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

EE 312 Fall 2017 Midterm 1 October 12, 2017

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

CSE 333 Midterm Exam 5/10/13

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

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

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

CSE 333 Midterm Exam Sample Solution 5/10/13

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

COMP2521. Generic ADTs in C

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

Array Initialization

8. Characters, Strings and Files

CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Wednesday, 11/18/2009

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

ECE264 Spring 2013 Exam 1, February 14, 2013

CSE 303 Midterm Exam

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

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

Do not start the test until instructed to do so!

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CSE 333 Midterm Exam 2/14/14

Question 1. [15 marks]

CSE 333 Autumn 2013 Midterm

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CS Operating system Summer Midterm I -- July 11, 2017 You have 115 min (10:00am-11:55pm). Good Luck!

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

Department of Electrical and Computer Engineering The University of Texas at Austin

Today s Learning Objectives

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

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

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

High Performance Programming Programming in C part 1

ECE264 Spring 2013 Final Exam, April 30, 2013

The University of Nottingham

ESC101N: Fundamentals of Computing End-sem st semester

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

Arrays and Pointers in C. Alan L. Cox

Dynamic memory. EECS 211 Winter 2019

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

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

Kurt Schmidt. October 30, 2018

Final CSE 131B Spring 2005

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

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

CS61, Fall 2012 Section 2 Notes

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

Dynamic arrays / C Strings

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

Midterm Exam 2 Solutions, C programming

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

COMP 524 Spring 2018 Midterm Thursday, March 1

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

CSE 303 Winter 2008 Midterm Key

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

211: Computer Architecture Summer 2016

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Class Information ANNOUCEMENTS

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Solutions and grading standards

CSE 333 Autumn 2014 Midterm Key

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

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

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

Make sure the version number is marked on your scantron sheet. This is Version 1

Computer Architecture I Mid-Term I


Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Largest Online Community of VU Students

CSE 333 Midterm Exam Sample Solution 7/28/14

CS61C : Machine Structures

COMP 2001/2401 Test #1 [out of 80 marks]

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CGS 3460 Summer 07 Midterm Exam

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ECE 15B COMPUTER ORGANIZATION

Transcription:

CS 1713 Introduction to Programming II Spring 2014 Midterm 2 -- April 24, 2014 You have 75 min. Good luck. You can use the 2-page C reference card posted in the class web page. Name: Score:./100 Background Survey (5pt bonus credit) A. Please complete the below table for the computer-programming-related courses that you have taken before Spring 2014. Programming courses CS 1063 Intro to Comp Prog I (in Java) CS 1713 Intro to Comp Prog II (in C) TAKEN AT UTSA, (Yes / No) If you have taken equivalent courses from a different school, please give the school name and the programming language used. (Say NO if this is the first time you are taking it) B. How would you evaluate your programming skills and background? (circle one) 5: excellent, developed some programs, received A in all Intro programming courses. 4: good, developed a few programs, received B in Intro programming courses. 3: fair, developed very simple programs, received C in Intro programming courses 2: beginner, have not take any programming course, never wrote any program. 1: What is "programming"? C. Do you think you had sufficient background to take cs1713? Definitely. Somewhat. I don't know. Not really. Definitely not. D. If you feel that you had (or did not have) sufficient background, what should be done (both on your part and in the curriculum) so that you can be successful in cs1713? (Please comment) 1

Name: 1. (20 pt) Trace the following program, show how values change in the memory, and give the output. #include <stdio.h> main() int x, y, z[3]=8,5,4, *p1, *p2; p1 = &x; *p1 = 9; p2 = z + 2; y = *p2--; *(p2-1) = 7; *++p2 = 3; name Add ress x 12? 9 2 y 16? 4 13 z[0] 20 8 7 11 z[1] 24 5 z[2] 28 4 3 p1 32? 12 Content/Value p2 36? 28 24 28 20 MEMORY printf("%d %d %d %d %d \n", x, y, z[0], z[1], z[2]); y = f1(&x, *p1, p2-1, &p2); printf("%d %d %d %d %d \n", x, y, z[0], p1, p2 ); printf("%d %d \n", *p1, *p2); 100 a 104 12 b 108 9 c 112 24 d 116 36 x 120 5 y 124 7 int f1(int *a, int b, int *c, int **d) int x=5, y=7; *a = y % x; *d = c-1; 128 OUTPUT: 9 4 7 5 3 2 13 11 @12 @20 2 11 **d = *a + b; return **d + *a; 2

Name: 2. (20pt) You are asked to implement the following function using pointers and pointer arithmetic int substrindex(char *str, char *substr); which returns -1 if substr is not in str; otherwise, it returns the index value for the first appearance of substr in str. You can assume that both str and substr are NULL terminated strings. For example, substrindex("abcabcdef", "abc") returns 0, substrindex("abcabcdef", "abcde") returns 3, substrindex("abc", "xy") returns -1 int substrindex(char *str, char *substr) /* FOR FULL CREDIT DON'T USE ARRAY NOTATION! * IF YOU SOLVE IT USING ARRAY NOTAION, YOUR MAX WILL BE 10Pt */ char *s1, *s2; int i=0; while(*str) if (*str == *substr) s1=str + 1; s2=substr + 1; while(*s1 && *s2 && *s1 == *s2) s1++; s2++; if(*s2=='\0') return i; str++; i++; return -1; 3

Name:. 3. (20pt) Develop a function char *my_concat(char *s1, char *s2); that concatenates two strings by joining them end to end. This function should dynamically allocate the memory for the new string and copy the original strings end to end into the new string. For example, after calling s = my_concat("abc", "DE"); s should be pointing to a new string "ABCDE". If needed, you can use the standard string functions from <string.h> library. char *my_concat(char *s1, char *s2) char *s; int len1, len2; if (s1 == NULL s2 == NULL) prinf("null string passed to my_concat\n"); return NULL; len1 = strlen(s1); len2 = strlen(s2); s = malloc(len1 + len2 + 1 ); if (s==null) return NULL; strcpy(s, s1); strcpy(s + len1, s2); /* or strcat(s,s2); */ return (s); 4

Name:... 4. (20pt) Suppose we have the following structure and variable declarations. typedef struct int x; int y; mydatat; mydatat a, *b, **c; a) (2pt) Set all the fields of variable a to 0. a.x = 0; a.y = 0; b) (3pt) Let b = &a; Then through pointer b set all the fields to 1. b->x = 1; b->y = 1; c) (5pt) Allocate an array of 50 pointers to mydatat and save the address of that array into c. c = (mydatat **) malloc( 50 * sizeof(mydatat *) ); if(c==null) printf("error"); exit(-1); d) (5pt) Suppose the memory allocation in part (c) is successful and c is now the array of 50 pointers to mydatat. Now, dynamically allocate an actual mydatat structure for each element of the array c and save its address in c[i]. for(int i=0; i < 50; i++) c[i] = (mydatat *) malloc( sizeof(mydatat) ); if(c[i]==null) printf("error"); exit(-1); e) (5pt) Free all the memory that you allocated in part (c) and part (d). for(int i = 0; i<50; i++) free(c[i]); free(c); 5

5. (20pt) We maintain two files to store the IDs and homework grades of the students in two sections. At the end of the semester, I need to merge these two files into a single file. Each student has nine homework grades after his/her ID, but the number of students in a file could be anything! Also the IDs in the input files are already sorted. Now you are asked to write a program that takes three file names as command line arguments, and merges the data in the first two files into the third file while keeping the IDs sorted. Here is an example. Suppose we have the following two files: section1.txt and section2.txt 12 3 5 2 5 3 5 2 5 3 18 5 2 5 3 3 5 2 5 3 25 3 5 2 5 3 2 5 3 5 When we execute your prog as follows > prog section1.txt section1.txt section-all.txt it should print out the following lines into section-all.txt 12 3 5 2 5 3 5 2 5 3 14 3 5 2 5 3 5 2 5 3 17 5 2 5 3 3 5 2 5 3 18 5 2 5 3 3 5 2 5 3 22 3 5 2 5 3 2 5 3 5 25 3 5 2 5 3 2 5 3 5 30 5 3 3 5 2 5 3 5 2 Implement your program by completing the following program also use the next page. #include <stdio.h> int main(int argc, char *argv[]) FILE *infp1, *infp2, *outfp; int id1, id2, end1=0, end2=0; 14 3 5 2 5 3 5 2 5 3 17 5 2 5 3 3 5 2 5 3 22 3 5 2 5 3 2 5 3 5 30 5 3 3 5 2 5 3 5 2 /* if you need declare more var */ if (argc!=4) printf("you need to give three file names\n"); return -1; if ( (infp1=fopen(argv[1], "r" )) == NULL (infp2=fopen(argv[2], "r" )) == NULL (outfp=fopen(argv[3], "w" )) == NULL ) printf("some files cannot be opened\n"); return -1; 6

/* Name: */ if (fscanf(infp1, %d, &id1)!=1) end1 = 1; if (fscanf(infp2, %d, &id2)!=1) end2 = 1; while(!end1!end2 ) if (end2 id1 < id2) fprintf(outfp, %d, id1); copy_grades(infp1, outfp); if (fscanf(infp1, %d, &id1)!=1) end1 = 1; else fprintf(outfp, %d, id2); copy_grades(infp2, outfp); if (fscanf(infp2, %d, &id2)!=1) end2 = 1; fclose(infp1); fclose(infp2); fclose(outfp); void copy_grades(file *infp, FILE *outfp) int i, hw; for(i=1; i<=9; i++) fscanf(infp, %d, &hw); fprintf(outfp, %d, hw); fprintf(outfp, \n ); 7