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

Similar documents
Engineering Problem Solving with C++, Etter

CMSC 202 Midterm Exam 1 Fall 2015

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

BITG 1113: Array (Part 2) LECTURE 9

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

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

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.

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

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

CS 115 Exam 3, Spring 2010

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

Copyright 2003 Pearson Education, Inc. Slide 1

Chapter 10. Arrays and Strings

Linked List using a Sentinel

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

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

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

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

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

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

Homework Assignment #2 (revised)

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

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

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

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

Pointers, Dynamic Data, and Reference Types

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

CS 103 Lab - Party Like A Char Star

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

Definition Matching (10 Points)

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

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Built-in Functions for NTCAs.

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

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

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):

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

1 Pointer Concepts. 1.1 Pointer Examples

CSCE 110 PROGRAMMING FUNDAMENTALS

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

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?

Computer Programming

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Exercise 1.1 Hello world

LECTURE 15. String I/O and cstring library

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

EE 355 Lab 4 - Party Like A Char Star

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

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

CS201- Introduction to Programming Current Quizzes

Arrays. Week 4. Assylbek Jumagaliyev

CS 103 Lab 6 - Party Like A Char Star

The C++ Language. Arizona State University 1

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

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

CMPS 221 Sample Final

Unit 14. Passing Arrays & C++ Strings

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

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

Structured Data. CIS 15 : Spring 2007

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

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:

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

System Design and Programming II

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CSCE 110 PROGRAMMING FUNDAMENTALS

1. (18 pts) Short Answer. Provide brief (1-3 sentence) answers to the following:

C++ For Science and Engineering Lecture 12

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

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Week 5. Muhao Chen.

Input And Output of C++

Lecture 12. We have already used strings. Strings. Hello Class is a string.

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

Objectives. In this chapter, you will:

conditional statements

Programming Language. Functions. Eng. Anis Nazer First Semester

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS 115 Final Review Quiz

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

C-String Library Functions

Unit 2: The string class

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

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

Getting started with C++ (Part 2)

Review. Relational Operators. The if Statement. CS 151 Review #4

Chapter 2: Basic Elements of C++

CS2255 HOMEWORK #1 Fall 2012

Object Oriented Pragramming (22316)

Object Oriented Programming COP3330 / CGS5409

Pointers. Developed By Ms. K.M.Sanghavi

Fundamentals of Programming CS-110. Lecture 2

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

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:

Transcription:

Name CPTR246 Spring '17 (100 total points) Exam 2 1. Pointer parameters (the old C way) In the following program, make all of the changes to convert the call-by-reference parameters in the function computeoptions to be call by reference using pointer parameters (known as the old C way). (8 points) #include <iostream> using namespace std; void computeoptions(double itemcost, int percent, double kohlscash, { double & costwithcoupon, double & costwithkohlscash) costwithcoupon = itemcost - (itemcost * percent / 100.0); costwithkohlscash = itemcost - kohlscash; int main() { double price, coupon, kohlscash, cost1, cost2; cout << "What is the cost of the item: "; cin >> price; cout << "Is your coupon 20%, 30%, or 40%? "; cin >> coupon; cout << "How much Kohl's Cash do you have? "; cin >> kohlscash; computeoptions(price, coupon, kohlscash, cost1, cost2); if (cost1 < cost2) cout << "Use the coupon." << endl; else cout << "Use your Kohl's Cash." << endl; return 0; 1

2. Pointer Basics In the box provided, write what would be displayed on the screen. (23 points) #include <iostream> #include <cstring> using namespace std; void what(char * x){ *(x + 5) = 't'; int main() { int x = 10; int * aptr; int * bptr; char * cptr; int numbers[6] = {3, 1, 4, 1, 5, 9; char adage[30] = "spring forward"; aptr = &x; bptr = numbers; cptr = adage; cout << *aptr << " " << *bptr << " " << *cptr << endl; cout << aptr[0] << " " << bptr[1] << " " << cptr[2] << endl; cout << *numbers << " " << (*numbers + 2) << " " << *(numbers + 2) << endl; if (numbers + 1 == numbers + 3) cout << "true" << endl; else cout << "false" << endl; Enter output here. bptr[2] = 0; bptr = aptr; *aptr = 5; *(cptr + 10) = '\0'; bptr = numbers + 2; cout << *aptr << " " << *bptr << endl; cout << x << " " << numbers[0] << endl; cout << adage << endl; for (int i = 0; i < 6; i++) cout << numbers[i] << " "; cout << endl; if (*bptr == numbers[2]) cout << "true" << endl; else cout << "false" << endl; what(adage); cout << adage << endl; return 0; 2

3. Structures: Assume that the following code appears in your program. struct employee { int ID; double hourlyrate; char name[30]; double hours; int weeks; ; // defines employee "type" // employee id number // pay rate // employee's first name // hours this week // number of weeks of vacation employee employee1 = {123, 12.75, "Mary", 40, 1; employee employee2 = {567, 10.75, "John", 50, 2; employee * empptr = & employee1; // first employee // second employee (a) For each of the following, indicate if the reference is a syntax error or not. If not, give the value of the reference? (8 points) Reference Error? (yes or no) If no, give its value employee1->id empptr->name[0] employee2.hours employee2.name[0] (b) Write if/else statement(s) that determine which employee has the highest hourly rate (assuming the hourlyrate has changed and you don t know the values) and displays his/her name on the screen. (Do not use literals, like Mary or John but use the name field instead. Display the literal none if they have the same hourly rate.) (4 points) (c) Complete the function below called GiveRaise that takes two parameters, an employee pointer and a double. The function is to increase the employee s hourly rate by the amount passed. It returns nothing. (4 points) void GiveRaise(employee * empptr, double raiseamount){ (d) Using the function in the previous question, write one line of code that gives employee2 a $3.00 an hour raise. (2 points) 3

4. Null-terminated Character Arrays: Use the handout (copies of pages 923-935 plus page 918) as needed for the following questions. (a) To the right of the following code, write what would be displayed on the screen. Reference Figure 22.21 on page 923. (12 points) #include <iostream> #include <cstring> using namespace std; int main() { char s1[20] = "apple"; char s2[20] = "peach"; char s3[20] = "orange"; char s4[20] = "pear"; char s5[20] = "banana"; char s6[20] = "grapes"; strcpy(s1, s2); strncpy(s3, s4, 3); strcat(s5, s6); cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; cout << s5 << endl; cout << s6 << endl; return 0; (b) Complete the following function that takes a null-terminated character array as a parameter and returns the number of uppercase letters in that string. For example, the function call howmanyuppercase( Hello World! ) would return a 2. Reference Figure 22.17 on page 918 (the last page of the handout). (4 points) int howmanyuppercase(char * str){ 4

5. Dynamically Allocated Arrays Assume that the following code has been written: int r,c; cout << "How many rows do you have?"; cin >> r; cout << "How many columns do you have?"; cin >> c; a. Write code to dynamically allocate an array of doubles called values that has r rows and c columns. (4 points) b. Write code that counts the number of positive values and negative values stored in the array values defined in the previous question. Zeroes are ignored. It should display those counts in a message such as There are positives and negatives. (6 points) 5

6. Command Line Arguments, File Processing, and Strings: (25 points) Write a program that takes a file of strings as input. There is one string per line, with some having a mix of characters, numbers, and whitespace. However, some of the lines may be blank (aka empty strings, i.e. their length is zero). The program is to determine (1) the number of strings in the file, (2) the number of those strings that are empty, and (3) the average length of the non-empty strings in the file. Display those values (identifying which is which) at the end. Important points: The name of the file MAY come from the command line. If it is NOT there, ask the user to enter it at the keyboard. Do NOT display any totals if the input file did not open properly. Display an appropriate error message and end the program instead. Do not assume that the file contains any strings (i.e., your program should handle an empty file gracefully that is, give appropriate messages). Do not assume that the file contains any non-empty strings (i.e., your program should handle this situation gracefully that is, give appropriate messages). You do NOT need to have more than the main function. You do NOT need to include any comments. Hints: This program can be written using either string objects or null-terminated character arrays (or a mixture of the two if you are so inclined). To get the length of a null-terminated character array, use the free function strlen - reference Figure 22.21 on page 924. To get the length of a string object, use the public method length( ). (An additional blank page has been added for your use. You may remove it if you like.) 6

7