CMPS 221 Sample Final

Similar documents
Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

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

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

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

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Arrays. Week 4. Assylbek Jumagaliyev

Introduction to C++ (Extensions to C)

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CMSC 202 Midterm Exam 1 Fall 2015

Programming with C++ as a Second Language

BITG 1113: Array (Part 1) LECTURE 8

This watermark does not appear in the registered version - Slide 1

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

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

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Week 3: Pointers (Part 2)

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Problem Solving: Storyboards for User Interaction

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

W3101: Programming Languages C++ Ramana Isukapalli

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

Ch 1. Algorithms and Basic C++ Programming

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Chapter 12. Streams and File I/O

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

BITG 1233: Introduction to C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

The C++ Language. Arizona State University 1

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.

Objectives. In this chapter, you will:

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

File I/O Christian Schumacher, Info1 D-MAVT 2013

Definition Matching (10 Points)

Exercise 1.1 Hello world

Strings and Stream I/O

Chapter 2. Procedural Programming

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Homework Assignment #2 (revised)

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

WARM UP LESSONS BARE BASICS

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

CSCE Practice Midterm. Data Types

Unit 14. Passing Arrays & C++ Strings

Sample Code: OUTPUT Daily Highs & Lows

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Practice test for midterm 1

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

Lab Instructor : Jean Lai

I/O Streams and Standard I/O Devices (cont d.)

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Computational Physics

Fundamentals of Programming CS-110. Lecture 2

Introduction to Programming EC-105. Lecture 2

Independent Representation

CSCE Practice Midterm. Data Types

Programming in C++: Assignment Week 1

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Local and Global Variables

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Integer Data Types. Data Type. Data Types. int, short int, long int

CS242 COMPUTER PROGRAMMING

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

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

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

EP241 Computing Programming

C++ For Science and Engineering Lecture 12

Increment and the While. Class 15

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Name Section: M/W or T/TH. True or False (14 Points)

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }

A SHORT COURSE ON C++

True or False (12 Points)

Apllications. February 23, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications. pointers and const

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

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

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

Programming in C++: Assignment Week 8

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Using Parallel Arrays. Parallel Array Example

Scientific Computing

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Transcription:

Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str, 80); and cin >> str;. For example, if you were to type Hello world!, what would be the value of str after each? 3. What is an index out of range error with an array? Give an example of accessing an array element in the array int arr[5]; that would cause an index out of range error. 4. How do classes differ from structures? In particular, what do classes contain that structures do not? 5. A C style string is a character array that ends with the null character \0. What is the purpose of the null character? 6. One cannot use + to concatenate two C style strings or = to assign one C style string to another. What functions does one use for concatenation and assignment of C style strings? 7. You wish to prompt the user for a filename, read in the filename they have typed and then open that file. Write the code for doing these steps. 8. You already have successfully opened a file through the input file stream variable called fin, created an integer array with the line int array[50]; and created an integer variable called count. Give the code for the loop that reads all the variables from the file into the array. 9. Write a void function called get input that has an integer array parameter, an integer parameter for the maximum size of the array and a call-by-reference integer parameter to store the number of elements read in. Prompt the user for the number of elements he wishes to enter. Verify that this value is not greater than the maximum size of the array. Then read in that many values from the keyboard. 10. Write an integer function called find minimum that has an integer array parameter and an integer parameter for the number of elements in the array. The function should find the smallest value in the array and return that value. 11. Write the complete code for the following programming project: Write a function that reads in a weight in pounds and ounces and outputs the equivalent weight in kilograms and grams. There are 2.2046 pounds in a kilogram and 16 ounces in a pound.

Name: 2 12. You currently have two integer pointers called p1 and p2 and two integer variables called v1 and v2 that have the following values: Address: 80000 80004 80008 80012 80016 80020 80024 Value: 0 0 0 0 58 0 17 Variable: p1 p2 v1 v2 Show the effect of executing the following code: p1 = &v1; p2 = &v2; p2 = p1; *p1 = 30; Address: 80000 80004 80008 80012 80016 80020 80024 Value: Variable: p1 p2 v1 v2

Name: 3 13. What would be the output of the following code snippet? Trace the output in the space provided. #include <iostream> #include <iomanip> using namespace std; int main() { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout.unsetf(ios::right); cout.setf(ios::left); cout << " " << setw(10) << "Name" << " " << setw(10) << "Integer"; cout << " " << setw(10) << "Double" << " " << endl << " "; cout << setw(10) << "Alice"; cout << " " << setw(10) << 5 << " " << setw(10) << 6.2 << " " << endl; cout << " " << setw(10) << "Bob"; cout << " " << setw(10) << 8 << " " << setw(10) << 13.34 << " " << endl; cout << " " << setw(10) << "Carol"; cout << " " << setw(10) << 12 << " " << setw(10) << 19.0 << " " << endl; return 0; }

Name: 4 14. There are 5 errors in the following code snippet. Identify each one by giving the line number on which it occurs. Describe the error and how to fix it. 1 #include <iostream> 2 using namespace std; 3 4 void print_array(const int a[][], int rows); 5 void clear_keyboard(); 6 7 int main() 8 { 9 int arr[3]; 10 11 cout << "Enter 9 integers: "; 12 for(int i = 0; i < 9; i++) 13 { 14 cin >> arr[i]; 15 clear_keyboard; 16 } 17 18 print_array(arr, 3); 19 return 0; 20 } 21 22 void print_array(int a[], int rows) 23 { 24 void clear_keyboard() 25 { 26 char c; 27 do 28 { 29 cin.get(c); 30 } while(c!= \n ); 31 } 32 33 for(int i = 1; i < 3; i++) 34 { 35 cout << setw(5) << a; 36 } 37 }

Name: 5 15. In the following code, what is the scope of each of the following variables: num1, j, ret, arr and i. If the scope is local, name the function or code block that contains the variable. Alternatively, list the line numbers for the scope of each variable or block out the scope of each variable next to the code. 1 #include <iostream> 2 using namespace std; 3 4 int arr[5]; 5 6 double find_average(const int a[], int elements); 7 8 int main() 9 { 10 double num1; 11 12 cout << "Enter 5 integers: "; 13 for(int i = 0; i < 5; i++) 14 { 15 cin >> arr[i]; 16 } 17 18 num1 = find_average(arr, 5); 19 cout << "The average is " << num1 << ".\n"; 20 21 return 0; 22 } 23 24 double find_average(const int a[], int elements) 25 { 26 double sum = 0, ret; 27 28 for(int j = 0; j < elements; j++) 29 { 30 sum = sum + a[j]; 31 } 32 ret = sum / elements; 33 return ret; 34 }