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

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

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

Final Intro to C Review

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

CS 0449 Sample Midterm

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COP 3223 Final Review

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

Fundamental of Programming (C)

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

2. Which of the following will print the value 2 for the above code?

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

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

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

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

Principles of C and Memory Management

Subject: Fundamental of Computer Programming 2068

CGS 3460 Summer 07 Midterm Exam

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Government Polytechnic Muzaffarpur.

COP 3223 Final Review

ESC101N: Fundamentals of Computing End-sem st semester

Character Strings. String-copy Example

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

C programming basics T3-1 -

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

This is CS50. Harvard University Fall Quiz 0 Answer Key

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Final CSE 131B Spring 2005

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

Answer all questions. Write your answers only in the space provided. Full marks = 50

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

M1-R4: Programing and Problem Solving using C (JAN 2019)

Procedural Programming & Fundamentals of Programming

COMP1917 Computing 1 Written Exam Sample Questions

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

CSE 142 Wi01 Midterm 2 page 1 of 6

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Memory Allocation. General Questions

C Programming Language

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Arrays and Applications

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Practice problems Set 2

return return else return

Question 1. [15 marks]

Array Initialization

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

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

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

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

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

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CS270 Homework 5 (HW5)

UNIT - V STRUCTURES AND UNIONS

Procedural programming with C

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Solutions to Assessment

Arrays, Pointers and Memory Management

Decision Making and Loops

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Pointers, Dynamic Data, and Reference Types

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

C Program Development and Debugging under Unix SEEM 3460

Midterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.

Arrays and Pointers (part 2) Be extra careful with pointers!

Foundation Exam - Sample Problems for New Topics

EE 312 Fall 2017 Midterm 1 October 12, 2017

Introduction to C Language (M3-R )

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

Storage class and Scope:

ARRAYS(II Unit Part II)

ECE264 Fall 2013 Exam 1, September 24, 2013

Basic and Practice in Programming Lab7

Programming in C. Pointers and Arrays

'C' Programming Language

CSE 333 Midterm Exam 7/27/15 Sample Solution

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 Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Lectures 5-6: Introduction to C

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

Transcription:

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional................................. 5 3.2 Two Dimensional................................. 6 3.3 Strings and String Functions.......................... 6 4 File I/O 7 5 Pointers 8 6 Pass by Reference Functions 9 7 Dynamic Memory Allocation 10 8 Structs 12 8.1 Pointers to Structs................................ 12 9 Linked Lists 12 1

1 Code Tracing 1) What is the output of the following segment of code? int a = 5, b = 9; a = b + 15 * 2; b += a - 6 / 3; a += a + b; b++; printf("%d,%d\n", a,b); A) 120,43 B) 120,24 C) 124,47 D) 115,30 2) What is the output of the following segment of code? int x = 12, y = 4, z = 7; if(x > 2 * z) x = 3 * y; y += 5* z; z -= x * y + 4; else if(y * 2 > 6) x += 15 * 2; y = x + z * 2 / 7; z += x % y; else x = y + 12; y = x % z + 4; z = x + y * y % 2; printf("%d %d %d\n", x,y,z); 2

3) What is the output of the following segment of code? int a = 7, b, c; b = a % 6 + 5; c = b * a / (b % a); a /= (c % b); printf("%d, %d, %d\n", b, c, a); 4) What is the output of the following segment of code? (Hint: There may be more output lines than necessary) int i; int j; for(i = 0; i < 3; i++) for(j = i; j < 4; j++) printf("%d", i + j); if(j == 3) printf("\n"); else printf(" "); Output Line 1: Output Line 2: Output Line 3: Output Line 4: Output Line 5: Output Line 6: 3

5) What is the output of the following segment of code? int i = 25, a = 7, b = 13, c = 12; while(i % 10 == 0) a *= i % b * c + c * 2; b -= b % c * a / 4 * (b+a); c = c * a + b - (c * b + 2 * a); a -= b; b += a * 2; c += 5; i--; printf("%d %d %d\n", a, b, c); 6) What is the difference between a while loop, and a do while loop? 2 Pass by Value Functions 1) Write a function prototype for a function named findarea that returns an integer and has two integers named width and height as parameters 2) Given the function prototype above, fill in the function so that it finds the area of a square with dimensions width and height 4

3)What is the output of the following lines of code? int main(void) int x = 12, y = 15, z; // mult(a,b) returns a * b z = mult(y,3); // pow(a,b) returns a^b x = pow(1,2); z += pow(y,1) + x; printf("%d %d %d\n", x, y, z); return 0; 3 Statically Allocated Arrays 3.1 One Dimensional 1) Write a function that receives an integer array and an integer representing the size of the array, and returns the an integer that holds the largest value in that array 5

3.2 Two Dimensional 1) In main, write a program that creates a 4x4 two dimensional array and populates it with random values between 1-100 3.3 Strings and String Functions 1) Draw the boxes representing an array that contains the string hello 2) When using the string copy function, which argument is the destination and which is the source? 3) Does the string length function account for the null terminator? 4) True or False: strcat(str1, str2) returns the string str2str1 5) What does the string compare function return if two strings are lexicographically equivalent? 6

6) Fill in the following function that is given two strings of equal length and compares them. If they are the same, create a 3rd string that is the result of concatenating the second string with the first, and print that string void stringstuff(char str1[], char str2[]) 4 File I/O 1) Write a program that scans in all of the integers from a file named input.txt and outputs their sum. The file is of the following format: the first line contains an integer n, and will be followed by n integers. int main(void) 7

5 Pointers 1) What is wrong with the following lines of code? int x = 15; int *p = &x; *p = 9; printf("%d\n", p); 2) Fill in the blanks with the proper data types that each variable should have. r; c; h; i; a; r = &c; a = r; *r = 17; *h = a; *a = 99; i = &h; 3) How would we set the element at position 3 of an array called array to be equal to 7 using pointer notation? 8

6 Pass by Reference Functions 1) Fill in the following function that is given two pointers as parameters and then swaps their values void swap(int *x, int *y) 2) Use the function from the previous question so that the values of a and b are swapped int a = 5, b = 7; 3) What is the output of the following program? int foo(int *a, int *b) *a = 2 + *a - *b; *b = *b + *a; printf("a = %d, b = %d\n", *a, *b); return 2+(*b); int main() int x = 12, y = 4, z = 3; y = foo(&x, &z); printf("x = %d, y = %d, z = %d\n", x, y, z); return 0; 9

7 Dynamic Memory Allocation 1) What are the differences between the malloc function and the calloc function? 2) Fill in the following main function so that you ask the user to input an integer, and then use that integer to allocate an integer array of that size, and populate it with random values between 1-100 int main(void) 10

3) You are given a file called words.txt that contains an integer n, followed by n lines that contain one string each. Allocate a two dimensional character array that contains exactly n strings, and where each row of the array is allocated memory that is exactly equal to the size of the string int main(void) 11

8 Structs 1) Create a struct named Student that contains a character array of size 50 for their name, an int for their id, and a float for their height 2) Using the struct created above, create a variable named studenta with the name Bob, an id of 15, and a height of 5.5 8.1 Pointers to Structs 3) if we have a pointer x to the student struct from the previous two questions, show the two ways to change the height of the struct pointed to by x to 5.9 9 Linked Lists 1) What are the advantages and disadvantages of Linked Lists compared to Arrays? 12

2) Given the following struct for a linked list node, write a function that prints the contents of a linked list given a pointer to the head of the list typedef struct node int data; struct node *next; node; void printlist(node *head) 13