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

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Final Intro to C Review

COP 3223 Final Review

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CS 0449 Sample Midterm

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

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

Fundamental of Programming (C)

COP 3223 Final Review

Subject: Fundamental of Computer Programming 2068

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

CGS 3460 Summer 07 Midterm Exam

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

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

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

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

Principles of C and Memory Management

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

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

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

COMP1917 Computing 1 Written Exam Sample Questions

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

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

Government Polytechnic Muzaffarpur.

Final CSE 131B Spring 2005

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

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

Practice problems Set 2

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

Procedural programming with C

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

Decision Making and Loops

Arrays, Pointers and Memory Management

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

Memory Allocation. General Questions

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

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

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

C Programming Language

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

Arrays and Applications

C Program Development and Debugging under Unix SEEM 3460

Foundation Exam - Sample Problems for New Topics

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

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

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

Character Strings. String-copy Example

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C programming basics T3-1 -

Computer Hardware Engineering

Question 1. [15 marks]

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

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

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

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

This is CS50. Harvard University Fall Quiz 0 Answer Key

Lectures 5-6: Introduction to C

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

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

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Array Initialization

Procedural Programming & Fundamentals of Programming

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

Please note that, for all What will be output questions, the answer could be error or indefinite. However, there is no compile error.

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

ESC101N: Fundamentals of Computing End-sem st semester

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

COP 3223 Section 4 Exam #2 Form A Multiple Choice Fall /1/2014

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

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

CSE 333 Midterm Exam 7/27/15 Sample Solution

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

SOFTWARE Ph.D. Qualifying Exam Fall 2017

CS270 Homework 5 (HW5)

return return else return

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Basic and Practice in Programming Lab7

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

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

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

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

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

CMPE-013/L. Introduction to C Programming

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

Important Questions for Viva CPU

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

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

Data Type. structure. C Programming Lecture 11 : Structuret. struct union. typedef. A collection of one or more variables

EM108 Software Development for Engineers

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

Transcription:

COP 3223 Introduction to Programming with C - Study Union - Spring 2018 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 + 3 * 2; b += a - 6 / 3; a = a + b; b++; printf("%d,%d\n", a,b); A) 23,37 B) 36,22 C) 37,23 D) 38,24 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 += y % x; 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? (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

4) 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); 5) 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 findsum that returns an integer and has one integer named input as a parameter 2) Given the function prototype above, fill in the function so that it finds the sum of the numbers from [0,input] inclusive. 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 to the bth power 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 amount of even numbers in the 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) What would be the array elements of a string that contains the word 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 a single string. If the string contains every letter of the alphabet, return 1, otherwise return 0. void stringstuff(char str[]) 4 File I/O 1) Write a program that scans in all of the integers from a file named input.txt. The file is of the following format: the first line contains an integer n, and will be followed by n integers. Print out the amount of numbers that are divisible by n. int main(void) 7

5 Pointers 1) What is wrong with the following lines of code? How can it be fixed? 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) Call 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 the array with values that are the negative of their index. 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 Employee that contains a character array of size 50 for their name, an int for their id, and a float for their wage 2) Using the struct created above, create a variable named employee1 with the name Bob, an id of 15, and a wage of 18.50 8.1 Pointers to Structs 3) if we have a pointer x to the employee struct from the previous two questions, show the two ways to change the wage of the struct pointed to by x to 19.00. 9 Linked Lists 1) What are some differences between Linked Lists and Arrays? 12

2) Given the following struct for a linked list node, write a function that is given the head of the list, and the value of the largest number in the list, and returns the value that appears most often in the list. typedef struct node int data; struct node *next; node; int mostoften(node *head, int largestnum) 13