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

Similar documents
Lab Instructor : Jean Lai

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

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 (!

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

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)

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

C/C++ Functions. Mark Redekopp

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

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

1 Unit 8 'for' Loops

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

CSCE 206: Structured Programming in C++

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;


CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Practice test for midterm 1

Name SECTION: 12:45 2:20. True or False (12 Points)

CMSC 202 Midterm Exam 1 Fall 2015

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

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

Ch 6. Functions. Example: function calls function

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

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

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

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

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

Unit 7. 'while' Loops

CSCE Practice Midterm. Data Types

#include <iostream> using namespace std;

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

Introduction to C ++

Programming Language. Functions. Eng. Anis Nazer First Semester

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE Practice Midterm. Data Types

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

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:

Due Date: See Blackboard

Chapter 4 - Notes Control Structures I (Selection)

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Exam 3 Chapters 7 & 9

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 10 - Notes Applications of Arrays

CMPS 221 Sample Final

Introduction to C ++

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Unit 14. Passing Arrays & C++ Strings

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

Chapter 2. Procedural Programming

Summary of basic C++-commands

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

A First Program - Greeting.cpp

CS 103 Lecture 3 Slides

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

CSci 1113 Midterm 1. Name: Student ID:

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

6.096 Introduction to C++ January (IAP) 2009

Your first C++ program

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Local and Global Variables

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

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

Outline. 1 About the course

Input And Output of C++

The University of Nottingham

Objectives. In this chapter, you will:

Increment and the While. Class 15

Chapter Four: Loops II

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

C++ basics Getting started with, and Data Types.

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

Scientific Computing

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

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

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Chapter 13. Functions and Parameter Passing (Part 2)

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

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

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Programming. C++ Basics

ECE 462 Fall 2011, Third Exam

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

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

Exercise 1.1 Hello world

Object oriented programming

C++ Support Classes (Data and Variables)

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CE221 Programming in C++ Part 1 Introduction

Transcription:

1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to show on the screen as true or false when printed by cout). #include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) return (double)(x/y); int x = 10, y=4, a = 11, b = 8; double z = 12.5; cout << 19 % 5 << endl; cout << 5 / 2 << endl; cout << 5 + 3 % 2 * x / 4 << endl; cout << (a++ + b) << endl; cout << (++a - b) << endl; cout << boolalpha << (x && (y y/x)) << endl; cout << pow(2, y) << endl; cout << min(max(min(x,y),x-1),(int)z) << endl; cout << f1(12,b) << endl; return 0; 2

2. (4 pts) Given the following declarations, fill in the table with the type that will result from the given expression. We do not want a value only the type. int x; char word[10]; char** s; double* p; Expression &x word *word word[5] **s &p *(word+1)+1 s + 2 Type 3. (1 pts) True/False: In the code below, 3 will be printed. int data[3] = 3,2,1; cout << data << endl; 4. (2 pts) Given the following declarations and call to the function named doit, infer and write a correct prototype (i.e. pre-declaration of the function) for doit. char c= C ; char* x[10]; double z=3.14; bool result = doit(&c, x, &z); Prototype for doit: 3

5. (6 pts) The following function should be written to generate random numbers in the interval [0,99] (inclusive) and return the number of iterations required for the total sum of all generated numbers to reach at least 1000. Assume proper includes and that srand() has been called already. Fill in the details. int generate() int total =, cnt = 0; //write the loop kind and condition ( ) int r = ; total = ; cnt++; return ; 6. What will be printed by the program below. void mystery(int *a, int n) for(int i = 0; i<n; i++) a[i] = a[n-i-1]; int x[] = 3,7,4,-1,5,-2,8; mystery(x,7); for(int i=0; i < 7; i++) cout << x[i] << " "; return 0; Program output: 4

7. Using similar meaning of the image library used in class and lab, what 5x5 image does this program draw assuming an all white starting image: unsigned char image[5][5]; //code to initialize image to white not shown const int MAX = 5; int r = 0, c = 0, delta = 1; int *same = &c, *change = &r; for(int i=0; i < MAX; i++) for(int j = 0; j < MAX-i; j++) image[r][c] = 0; *change += delta; Show the resulting image using the grid below. *change -= delta; int* temp = change; change = same; same = temp; if( (i % 2) == 1) delta = -delta; *change += delta; 5

8. (7 pts) Fill in the missing parts for: bool isdigits(char* str); which should return true if all characters in the string are digits (ASCII '0'-'9') bool isdigits(char* str) // no other variables may be declared while(!= ) if(! (*str >= ) ( <= ) ) return false; ; return true; 6

9. (5 pts) Consider each option in the table directly below and circle it if it is a possible output from the program for some input combinations of x, y, and z. Do NOT circle an option if it cannot be output regardless of what the user types in. ABCF ABF AE ADE AF AB ADEF ABDF int x, y, z; cin >> x >> y >> z; if( y < x && y < z ) cout << "A"; if( y > 2 ) cout << "B"; if( x == 1 ) cout << "C"; else if ( z == 5 ) cout << "D"; if( x < z ) cout << "E"; else cout << "F"; cout << endl; return 0; 7

10. (9 pts.) Show what will be printed by this program (ignore missing #includes, etc.) if it is run via the command line:./prog1 aaab baabba bool f2(char s[], char c) int i=0; while(s[i]!= 0) if(s[i] == c) s[i] = '-'; return true; i++; return false; bool f1(char* s) while( f2(s, 'a') ) if(! f2(s, 'b') ) return false; return!f2(s, 'b'); int main(int argc, char* argv[]) char str1[10] = ""; Program output: if( f1(str1) ) cout << "str1 yes" << endl; else cout << "str1 no" << endl; cout << str1 << endl; if( f1(argv[1]) ) cout << "argv[1] yes" << endl; else cout << " argv[1] no" << endl; cout << argv[1] << endl; if( f1(argv[2]) ) cout << "argv[2] yes" << endl; else cout << " argv[2] no" << endl; cout << argv[2] << endl; return 0; 8

1. (9 pts.) Show what will be output by the cout s in this program. (Note: boolalpha simply causes Booleans to show on the screen as true or false when printed by cout): #include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, double y) return (int)(x*y); int x = 10, y=4; double z=10.5; cout << 15 % 7 << endl; cout << y/x << endl; cout << 5 + 3 % 2 * x / 4 << endl; cout << (x++ + y) << endl; cout << (++x - y) << endl; cout << boolalpha << (!x && (y y/x)) << endl; cout << pow(2, x) << endl; cout << max(min(max(x,y),x+y),(int)z) << endl; cout << f1(y,z) << endl; 2. (2 pts.) a) Given the declaration: char word[10]; What type will the expression word evaluate to? Circle your choice. a. char b. int* c. char* d. char** b) What value will the expression word evaluate to? 2

3. (4 pts.) Given the following declarations and call to the function named doit, infer and write a correct prototype (i.e. pre-declaration of the function) for doit. char c= C ; int X[10]; double z=3.14; bool result = doit(c, X, &z); Prototype for doit: 4. (5 pts.) Examine the program below (don t worry about #includes, etc.). What variables could legally be printed (i.e. would be in scope / accessible / visible) at each of the cout statements in the program. In the table below place a check mark or x in the corresponding cell if the variable IS in scope and can be printed by the corresponding cout. Leave the cell blank if the variable is NOT in scope for the given cout. int a; void f1(char c) cout << << endl; // cout1 int i = 0; for(;i<10;i++) double z = pow(y, i); cin >> a; if(a < 1) double y = 2.0/3.0; cout << << endl; // cout2 cout1 a c y i z cout2 3

5. (4 pts.) Given the following array declarations and indexed accesses, compute the address where the indexed value will be in memory. Assume the array starts at location 200 on a 64-bit computer. a. int x[10]; x[5] is at: b. char c[10][4]; c[2][1] is at: c. double d[3][4][4]; d[1][2][3] is at: d. char *n[10]; n[3] is at: 6. (2 pts.) Consider the following program fragment. The code is trying to find a random number n for which do_it() averages >= 3.5 for 10 iterations. do_it(n) always returns an integer in the range [0,5]. Find and fix the problem by marking up the code. double average=0.0; int n; while(average < 3.5) int r=0; n = rand(); for(int i=0;i<10;i++) r += do_it(n); average = (double)(r / 10); 4

7. (6 pts.) What does the following program output? #include <iostream> using namespace std; for(int i=0; i<10; (i<3?i++:i+=2) ) if(!(i%3)) continue; else if( i % 7 == 0) break; cout << i << endl; Program Output: 5

8. (12 pts.) Show the output of this program by tracing the code execution. For this code you can assume the first 10 calls to rand() returns the following sequence [18, 12, 8, 17, 1, 13, 10, 3, 16, 5] #include <iostream> #include <cstdlib> using namespace std; void m1(int *a, int *b) int c = *b; *b = *a; *a = c; void m2(int *X, int n) for(int i=0;i<n-2;i++) int j = rand() % (n-i); m1( (X+i), X+i+j); int array[] = 0,1,2,3,4; srand(1234); m2(array, 5); for(int i=0;i<5;i++) cout << array[i] << " "; cout << endl; Output: 6

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 10. (8 pts.) Billy Bruin was attempting to write a program that would read in up to 16 ones or zeros of a binary number. The program calculates the value of the number and prints it out. For example if you enter 101x it will output 101=5 He has several questions and a few bugs. Can you help him out? #include <iostream> #include <cmath> using namespace std; unsigned char str[ ], c; unsigned int val=0; int i; for(i=0;i<16;i++) cin >> c; if( c!= '0' c!= '1') ; str[i] = c; int len = i; for(i=0;i<len;i++) if( str[i] == '1') val += pow(2, len - i); cout << str << "=" << val << endl; a. Line 8: Billy wasn t sure how large to declare the array str. Please complete it for him. b. Line 16: Billy wanted to immediately quit the for loop if he received something other than a 1 or 0. Complete line 16. c. After making these changes, Billy s first for-loop never seems to do anything. Can you fix it? d. After that fix, Billy notices that the final cout sometimes prints extra garbage characters before the equals sign. Add something at line 20 to fix this. e. Finally, Billy notices that his code seems to be almost working, except all of his answers are too big by a factor of two. Find the mistake and write the correction below: Line num: Changed code: 8

1. Short answer. Fill in the best response (5pts). The name of an array is an expression. What value does that expression have? What are the four attributes of a variable in C/C++? In what order are the four pieces of a for() loop executed? If you know the type of a variable in C++ you know how much has been allocated to that variable. Memory address of X is another way of saying a to X. A C-string is not a type, it is a on how to use a char array to store text. The parameters of a function definition are known as _, while the parameters passed to a function when called are. Functions in C/C++ use pass-by- for the input parameter and return value. In C/C++ any integer value can be considered true or false. What convention is used? Make the following expressions true: sizeof(char) == sizeof(double) == sizeof(int) == 2

2. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to show on the screen as true or false when printed by cout). #include <iostream> #include <algorithm> #include <cmath> using namespace std; double f1(int x, int y) return (x/y); int x = 10, y=4, a = 11, b = 8; double z = 12.5; cout << 19 % 5 << endl; cout << 5 / 2 << endl; cout << 5 + 3 % 2 * x / 4 << endl; cout << (a++ + b) << endl; cout << (++a - b) << endl; cout << boolalpha << (x && (y y/x)) << endl; cout << pow(2, y) << endl; cout << min(max(min(x,y),x-1),(int)z) << endl; cout << f1(12,b) << endl; return 0; 3

3. (4 pts) Given the following declarations, fill in the table with the type that will result from the given expression. We do not want a value only the type. int x; char word[10]; char** s; double* p; Expression &x word *word word[5] **s &p s + 2 *(s[1]) Type 4. (1 pts) True/False: In the code below, 3 will be printed. int data[3] = 3,2,1; cout << data << endl; 5. (2 pts) Given the following declarations and call to the function named doit, infer and write a correct prototype (i.e. pre-declaration of the function) for doit. int f=100; char x[10]; double z=3.14; bool result = doit(f, x, &z); Prototype for doit: 4

6. (5pts) What does the following program output? #include <iostream> using namespace std; for(int i=0; i<10; (i<3?i++:i+=2) ) if(!(i%3)) continue; else if( i % 7 == 0) break; cout << i << endl; Program Output: 5

7. (6 pts) The following function should be written to generate random numbers in the interval [0,99] (inclusive) and return the number of iterations required for the total sum of all generated numbers to reach at least 1000. Assume proper includes and that srand() has been called already. Fill in the details. int generate() int total =, cnt = 0; //write the loop kind and condition ( ) int randnum = ; total = ; cnt++; return ; 8. (7 pts) Fill in the missing parts for: bool isdigits(char* str); which should return true if all characters in the string are digits (ASCII '0'-'9') bool isdigits(char* str) // no other variables may be declared while(!= ) if(! ((*str >= ) ( <= ) )) return false; ; return true; 6

9. (5pts) Show the output of this program by tracing the code execution. For this code you can assume the first 10 calls to rand() returns the following sequence [18, 12, 8, 17, 1, 13, 10, 3, 16, 5] #include <iostream> #include <cstdlib> using namespace std; void m1(int *a, int *b) int c = *b; *b = *a; *a = c; void m2(int *X, int n) for(int i=0;i<n-2;i++) int j = rand() % (n-i); m1( (X+i), X+i+j); int array[] = 0,1,2,3,4; srand(1234); m2(array, 5); for(int i=0;i<5;i++) cout << array[i] << " "; cout << endl; Output: 7

1. (4 pts) Short answer / Choice a) The name of an array is an expression. What value does that expression have? b) In what order are the four pieces of a for() loop executed? c) The parameters in a function definition are known as, while the parameters passed to a function when called are. d) Functions in C/C++ use pass-by- for the input parameters and return value. e) In C/C++ any integer value can be considered true or false. What convention is used? f) The C/C++ compilers are known as compilers. (circle): top-down, single pass OR multi-pass g) Memory for arrays is allocated. randomly OR contiguously h) In C++ a representation is called a. variable OR type 2

2. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to show on the screen as true or false when printed by cout). #include <iostream> #include <algorithm> #include <cmath> using namespace std; double f1(int x, int y) return (x/y); int x = 10, y=4, a = 11, b = 8; double z = 12.5; cout << 19 % 5 << endl; cout << 5 / 2 << endl; cout << 5 + 3 % 2 * x / 4 << endl; cout << (a++ + b) << endl; cout << (++a - b) << endl; cout << boolalpha << (x && (y y/x)) << endl; cout << pow(2, y) << endl; cout << min(max(min(x,y),x-1),(int)z) << endl; cout << f1(12,b) << endl; return 0; 3

3. (1 pts) True/False: In the code below, 3 will be printed. int data[3] = 3,2,1; cout << data << endl; 4. (2 pts) Given the following declarations and call to the function named doit, infer and write a correct prototype (i.e. pre-declaration of the function) for doit. int f=100; char x[10]; double z=3.14; bool result = doit(f, x, z); Prototype for doit: 5. (5pts) What does the following program output? #include <iostream> using namespace std; for(int i=0; i<10; (i<3?i++:i+=2) ) if(!(i%3)) continue; else if( i % 7 == 0) break; cout << i << endl; Program Output: 4

6. (6 pts) The following function should be written to generate random numbers in the interval [0,99] (inclusive) and return the number of iterations required for the total sum of all generated numbers to reach at least 1000. Assume proper includes and that srand() has been called already. Fill in the details. int generate() int total =, cnt = 0; //write the loop kind and condition ( ) int randnum = ; total = ; cnt++; return ; 7. (5 pts) Fill in the missing parts for: bool isdigits(char str[]); which should return true if all characters in the string are digits (ASCII '0'-'9'). You don t need the ASCII table for this! bool isdigits(char str[]) // no other variables may be declared int pos = 0; while(!= ) if( _ ) return false; ; return true; 5

8. (3 pts) What will be printed by the program below. void mystery(int a[], int n) for(int i = 0; i<n; i++) a[i] = a[n-i-1]; int x[] = 3,7,4,-1,5,-2,8; mystery(x,7); for(int i=0; i < 7; i++) cout << x[i] << " "; return 0; Program output: 9. (1 pts) What does the following code print out? int x=1; int y[] = 1,2,3; bool z=true; cout << y && (!x ( z &&!( x 1) )? Hello : Goodbye! << endl; Output: 6

10. (5 pts) Consider each option in the table directly below and circle it if it is a possible output from the program for some input combinations of x, y, and z. i.e Considering any possible values the user could type in, circle the possible output. Do NOT circle an option if it cannot be output regardless of what the user types in. ABCF ABF AE ADE AF AB ADEF ABDF int x, y, z; cin >> x >> y >> z; if( y < x && y < z ) cout << "A"; if( y > 2 ) cout << "B"; if( x == 1 ) cout << "C"; else if ( z == 5 ) cout << "D"; if( x < z ) cout << "E"; else cout << "F"; cout << endl; return 0; 7