EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

Similar documents
Week 4 EECS 183 MAXIM ALEKSA. maximal.io

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

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

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

EECS402 Lecture 02. Functions. Function Prototype

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

CSCE 206: Structured Programming in C++

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lab Instructor : Jean Lai

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Name. CPTR246 Spring '17 (100 total points) Exam 2

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Programming Language. Functions. Eng. Anis Nazer First Semester

Arrays in C++ Instructor: Andy Abreu

Review Summer CSC1322 Adv Programming in C++ Professor: Zhang

Computer Programming

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

Getting started with C++ (Part 2)

a. a * c - 10 = b. a % b + (a * d) + 7 =

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CMSC 202 Midterm Exam 1 Fall 2015

Computer Programming : C++

CS2141 Software Development using C/C++ C++ Basics

The C++ Language. Arizona State University 1

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

C++ Programming: From Problem Analysis to Program Design, Third Edition

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Computer Science II Lecture 1 Introduction and Background

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Fundamentals of Programming CS-110. Lecture 2

CSCI 102 Fall 2010 Exam #1

CSCE Practice Midterm. Data Types

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

PIC 10A. Lecture 17: Classes III, overloading

WARM UP LESSONS BARE BASICS

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

Topics. Functions. Functions

BITG 1113: Function (Part 2) LECTURE 5

Study Guide for Test 2

PIC 10A. Review for Midterm I

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Exercise 1.1 Hello world

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS Homework 10 p. 1. CS Homework 10

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Introduction to Programming EC-105. Lecture 2

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

2 nd Week Lecture Notes

Lab # 02. Basic Elements of C++ _ Part1

Ch 6. Functions. Example: function calls function

CMSC202 Computer Science II for Majors

Unit 7. 'while' Loops


Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Help File on C++ and Programming at CSUSM by Rika Yoshii

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Programming in C++: Assignment Week 8

Do not turn to the next page until the start of the exam.

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

Lecture 3 The character, string data Types Files

pointers + memory double x; string a; int x; main overhead int y; main overhead

Fast Introduction to Object Oriented Programming and C++

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

C++ For Science and Engineering Lecture 12

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Pointers, Dynamic Data, and Reference Types

CPE Summer 2015 Exam I (150 pts) June 18, 2015

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

A First Program - Greeting.cpp

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Transcription:

EECS 183, Week 5 General 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library? Variables 3. Name main data types in C++. 4. Is string a native data type in C++? What do you need to do atop the.cpp file to use strings? 5. How are these two lines of code different? int x = 5; x = 6; 6. Any problems with this line of code? int somenumber = 1000 * 1000 * 1000 * 1000; 7. What are the differences between variables and constants? What are naming conventions for each? 8. Which of the following are valid variable declarations? int 1f; int if; int if1; int f+1; 9. What prints? int x = 18 / 5 5 / 2; cout << x << endl; double y = 18 / 5 5 / 2; cout << y << endl; 10. Given two ints, a and b, how to divide a by b and get a double back? 11. What s the best way to compare two variables of type double for equality? I/O 12. cin >> str; vs. getline(cin, str);? When to use each? 13. What do \', \", \n, \t and \\ do? 14. Consider the following code: int x = 2, y = 4; char ch; cin >> x >> ch >> y; cout << x * y << endl; 15. What prints if the user types the following? 2.5 * 2.0<Enter> Maxim Aleksa (maximal@umich.edu) 0<7 http://umich.edu/~maximal/eecs183

EECS 183, Week 5 Functions 16. In the following code, mark declaration, prototype, function call, definition, implementation, signature, header, argument/parameter list, return type, return statement. #include <iostream> using namespace std; /** * Requires: Nothing. * Modifies: Nothing. * Effects: Returns sum of a and b. */ int add(int a, int b); int sum = add(1, 2); cout << sum << endl; int add(int a, int b) { int sum = a + b; return sum; 17. What is the difference between prototype and implementation? 18. Which of the following are valid function prototypes? char void(double x); int maximum(x, y); int calc(void x); void squareroot(double x); 19. If a variable fillcolor is accessible only within function drawrect, then fillcolor is known as... 20. What is RME? 21. How is RME interpreted by the C++ compiler? 22. Implement max, a function that returns the maximum of the three arguments passed in. int max(int a, int b, int c); Maxim Aleksa (maximal@umich.edu) 1<7 http://umich.edu/~maximal/eecs183

Conditions 23. (T/F) if is a function 24. How are these two snippets of pseudocode different? if conditiona: do something if conditionb: do something else if conditionc: do this other thing if conditiona: do something else if conditionb: do something else else if conditionc: do this other thing 25. For each expression in the left column, find its complement in the right column and write the letter of the complement on the blank. price < 50 20 <= price && price < 50 20 > price price > 50 price < 50 && instock!instock price < 50 26. (T/F) Order of conditions in a sequence of if / else if / else statements matters. 27. (T/F) It is possible to write every sequence of if / else if / else statement as a switch statement. 28. (T/F) It is possible to write every switch statement as a sequence of if / else if / else statements. 29. What does this code print? Does the program crash? if ((3 == 4) && (3 / 0 == 4)) { cout << "true" << endl; else { cout << "false" << endl; 30. What prints? int x = 0; if (x = 0) { cout << "zero" << endl; else { cout << "non-zero" << endl; 31. Find (and fix!) errors with in the snippet of code below. int lecture = getlecturenumber(); if (lecture == 1 3) { string instructor = "Bill Arthur"; else if (lecture == 2) { string instructor = "Héctor García"; else if (lecture == 3) { string instructor = "Mary Lou Dorf"; else { string instructor = "Staff"; 32. Rewrite this expression without using &&: A. price < 20 price >= 50 B. price >= 50 && instock C. price >= 50 &&!instock D. price >= 50!inStock E. price >= 50 cout << "Your professor is " << instructor << endl; student && age >= 18 F. price >= 20 && price <= 50 Maxim Aleksa (maximal@umich.edu) 2 < 7 http://umich.edu/~maximal/eecs183

Loops 33. For loops vs. while loops? When to use each? 34. (T/F) It is possible to write every for loop as a while loop. 35. (T/F) It is possible to write every while loop as a for loop. 36. Rewrite this for loop as a while loop: for (int i = 10; i > 0; i--) { cout << i << endl; 37. Write a loop that prints a triangle of stars of height 4: * ** *** **** 38. Write a loop that prints an upside-down triangle of stars of height 4: **** *** ** * 39. What does this snippet of code print? int x = 100; while (x >= 0) { x /= 5; cout << x << " "; 40. What does this loop print? for (int i = 0; i >= 3; i++) { cout << 'X'; 41. Implement countones, a function that counts how many numbers are in the integer passed as an argument and returns the count. Here s its declaration: int countones(int n); Maxim Aleksa (maximal@umich.edu) 3 < 7 http://umich.edu/~maximal/eecs183

ASCII Maps characters to their numerical representation. Don t memorize the table. Remember this though: 'A' is 65 'a' is 97 'A' comes before 'a' 'Z' and 'a' and are not next to each other '0' is not 0 42. Suppose you want to determine whether the user typed in a letter or not. What condition goes inside of the if statement? char character; cin >> character; if ( ) { cout << "letter" << endl; else { cout << "not a letter" << endl; ) 43. Write some code uses loops to print a battleship board. 1 2 3 4 5 6 7 8 9 10 A o o o o o o o o o o B o o o o o o o o o o C o o o o o o o o o o D o o o o o o o o o o E o o o o o o o o o o F o o o o o o o o o o G o o o o o o o o o o H o o o o o o o o o o I o o o o o o o o o o J o o o o o o o o o o Strings String is a sequence of characters. Each character has an index. The index of the last character is length of the string 1. 0 K 1 Let s say we have a string name that we read from the user. string name; cin >> name; a 2 t 3 h 4 e 5 r 6 i 7 n 8 e 44. How can you get the length of name? int numberofcharacters = 45. How can we get the first character of name? char firstcharacter = Maxim Aleksa (maximal@umich.edu) 4 < 7 http://umich.edu/~maximal/eecs183

46. Write a complete program (with main and one other function) that asks the user for a string, and then prints that same string with the first character capitalized (if a letter). The program should behave per the sample output below: Please give me a string: hello Hello Pass-by-Value and Pass-by-Reference 47. What is pass-by-value? 48. What is pass-by-reference? 49. What prints? void increment(int x); int x = 1; increment(x); cout << "x is " << x << endl; void increment(int x) { x += 1; 50. What prints? void foo(int& x, int y); int a = 3; int b = 4; foo(b, a); cout << a << " " << b << endl; void foo(int& x, int y) { y += 2; x *= y; Maxim Aleksa (maximal@umich.edu) 5 < 7 http://umich.edu/~maximal/eecs183

51. Fix the swap function. void swap(int a, int b) { int temp = a; a = b; b = temp; EECS 183, Week 5 52. For each main function, pretend to be a C++ compiler and figure out what prints. Draw a memory diagram for each function to keep track of variables. Common functions main function Memory diagrams Output int foo(int x) { cout << x--; return x; int bar(int y) { cout << y; return y + 2; int baz(int &z) { z++; cout << z; return z; z += 2; int a = 2; cout << a; foo(a); cout << a; int b = 3; cout << b; b = bar(b); cout << b; int c = 4; cout << c; c = baz(c); cout << c; 53. Consider the following C++ function: int triple(int& x) { x = x * 3; return x; int d = 2; int e = 3; d = baz(d + e); cout << d; int f = bar(foo(2)); cout << f; int g = foo(baz(4)); cout << g; What does the following code print? int y = 1; cout << triple(y) + triple(y); Maxim Aleksa (maximal@umich.edu) 6 < 7 http://umich.edu/~maximal/eecs183