Welcome Application. Introduction to C++ Programming (Solutions) 2004 by Deitel & Associates, Inc. All Rights Reserved.

Similar documents
Appendix M: Introduction to Microsoft Visual C Express Edition

Getting Started with Visual Studio

Introduction to Programming

Chapter 1 Introduction to Computers and C++ Programming

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Fundamentals of Programming Session 4

Lab 4: Introduction to Programming

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Welcome Application. Introducing the Visual Studio.NET IDE. Objectives. Outline

TREX Set-Up Guide: Creating a TREX Executable File for Windows

C: How to Program. Week /Mar/05

CST8152 Compilers Creating a C Language Console Project with Microsoft Visual Studio.Net 2005

Visual C++ Tutorial. For Introduction to Programming with C++ By Y. Daniel Liang

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CST8152 Compilers Creating a C Language Console Project with Microsoft Visual Studio.Net 2010

Chapter 2 - Introduction to C Programming

Chapter 2, Part I Introduction to C Programming

Understanding main() function Input/Output Streams

CST8152 Compilers Creating a C Language Console Project with Microsoft Visual Studio.Net 2003

MS Visual Studio.Net 2008 Tutorial

Your First C++ Program. September 1, 2010

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

4. Structure of a C++ program

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

IT 374 C# and Applications/ IT695 C# Data Structures

download instant at Introduction to C++

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Chapter 1 & 2 Introduction to C Language

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 2: Basic Elements of C++

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++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Fundamentals of Programming. Lecture 3: Introduction to C Programming

2 nd Week Lecture Notes

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

WRITING CONSOLE APPLICATIONS IN C

EECE.2160: ECE Application Programming Spring 2018 Programming Assignment #1: A Simple C Program Due Monday, 1/29/18, 11:59:59 PM

Tutorial 2: Compiling and Running C++ Source Code

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Objectives. In this chapter, you will:

Chapter 1 - What s in a program?

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Introduction to Java Applications

Chapter 2 Basic Elements of C++

6.096 Introduction to C++ January (IAP) 2009

download instant at Introduction to C++

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

NetBeans Tutorial. For Introduction to Java Programming By Y. Daniel Liang. This tutorial applies to NetBeans 6, 7, or a higher version.

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

Introduction to C++ IT 1033: Fundamentals of Programming

My First Command-Line Program

Lab 1: Introduction to C Programming. (Creating a program using the Microsoft developer Studio, Compiling and Linking)

Programming with C++ as a Second Language

Programming I Laboratory - lesson 01

Chapter 1 INTRODUCTION

3. NetBeans IDE 6.0. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

esignal Formula Script (EFS) Tutorial Series

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Program Organization and Comments

Chapter 2 C++ Fundamentals

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

IS 0020 Program Design and Software Tools

Programming for Engineers Introduction to C

Chapter 2: Overview of C++

LAB 1: FAMILIARITY WITH NETBEANS IDE ENVIRONMENT

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Programming in Microsoft Visual C++ Programming in Microsoft Visual C++

Computer Programming : C++

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Getting Started (1.8.7) 9/2/2009

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

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

Object Oriented Design

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

This manual will explain how to do a mail merge in Cordell Connect, using the following Windows programs:

Full file at C How to Program, 6/e Multiple Choice Test Bank

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Introduction to C# Applications

Enterprise Modernization for IBM System z:

Using OpenGL & GLUT in Visual Studio.NET 2003

A A B U n i v e r s i t y

RVDS 3.0 Introductory Tutorial

Creating a new CDC policy using the Database Administration Console

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Engr 123 Spring 2018 Notes on Visual Studio

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

Some (semi-)advanced tips for LibreOffice

[ Getting Started with Analyzer, Interactive Reports, and Dashboards ] ]

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

A Guided Tour of Doc-To-Help

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

Introductionto C++ Programming

Transcription:

T U T O R I A L 2 Welcome Application Introduction to C++ Programming (Solutions) 1

2 Introduction to C++ Programming (Solutions) Tutorial 2 These solutions will not be available to students. Instructor s Manual Exercise Solutions Tutorial 2 MULTIPLE-CHOICE QUESTIONS 2.1 Use the object to display text on the screen. a) screen b) cout c) output d) iostream 2.2 To compile an application in Visual Studio.NET, select. a) File > Compile Solution b) Build > Compile Solution c) Build > Build Solution d) Compile > Build Solution 2.3 must begin the body of every function. a) A newline character b) The int keyword c) A return statement d) A left brace ({ ) 2. Syntax errors in a C++ application. a) can cause subtle errors when the application runs 2.5 Files with a.obj extension contain. 2. Operator << is the operator. 2.7 The escape sequence sounds the system bell. 2.8 To run an application using Visual Studio.NET, select. 2.9 A is an example of whitespace character. b) prevent the application from compiling correctly c) are ignored by the compiler d) are detected after the application has been compiled a) source code b) an executable image c) machine code d) preprocessor information a) stream insertion b) stream extraction c) display d) cout a) \b b) \a c) \sb d) None of the above. a) Run > Start Without Debugging b) Build > Start c) Debug > Start Without Debugging d) Run > Start a) tab b) space c) newline d) All of the above. 2.10 The escape sequence places the cursor at the beginning of the current line. a) \n b) \r c) \t d) \b Answers: 2.1) b. 2.2) c. 2.3) d. 2.) b. 2.5) c. 2.) a. 2.7) b. 2.8) c. 2.9) d. 2.10) b. EXERCISES 2. ( Alarm Application) Many universities provide computer networks that enable multiple users to access information on the local area network (LAN) and on the Internet. To increase security, the system will log off a user if that user has been inactive for a significant period of time. Suppose the system administrator wants the system to warn the user before logging off. In this exercise, you will create an application that displays a message to the user

Tutorial 2 Welcome Application 3 and sounds the system bell three times to indicate that the user will be logged off in one minute (Fig.2.35). Figure 2.35 Alarm application output for completed application. Answer: a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\Alarm to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project Alarm. Save this project in your SimplyCpp directory.then add the existing source code file, Alarm.cpp, which is located in the C:\SimplyCpp\Alarm directory. c) Opening the template file. Open the Alarm.cpp file in your text editor or IDE. d) Displaying a message to the user. Before sounding the system bell, you should display a warning message to the user.after line 9 in the template code, insert a statement to display a newline and the message, "Warning: You have been inactive for over 30 minutes.". Do not terminate the statement yet. Insert a second stream insertion operator on the next line of your application s source code to display the text, "You will be logged out in one minute." This text should be displayed on a new line, using the newline escape sequence. Two newlines should be inserted after this message is displayed. Remember to terminate the second line of your statement with a semicolon. e) Sounding the system bell. To sound the system bell, display a string that consists of three alert escape sequences. f) Save your modified source code file. g) Compile the completed application. h) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the program output to the one shown in Fig. 2.35 to ensure that you displayed the message to the user correctly. [Note: Depending on the system you are using, you may not hear the computer s system bell or the three bells may sound so quickly that you may hear the system bell sound only once when the application runs.] i) Close the Command Prompt window. j) Close your text editor or IDE. 1 // Exercise 2. 2 // Application that prints a message and sounds an alarm. 10 cout << "\nwarning: You have been inactive for over 30 minutes." << "\nyou will be logged out in one minute.\n\n";

Introduction to C++ Programming (Solutions) Tutorial 2 cout << "\a\a\a"; 1 15 return 0 ; // indicate that program ended successfully 1 17 } // end function main 2. ( Table of Powers Application) Throughout this book, you will be asked to create C++ applications that display tables of values. In this exercise, you will use the tab and newline escape sequences to display a table of the squares and cubes of the numbers from zero to 5 (Fig. 2.3). In a later tutorial, you will modify the application to calculate the values displayed in Fig.2.3. Figure 2.3 Table of Powers application output. a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\TableOfPowers to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project TableOfPowers. Save this project in your SimplyCpp directory. Then add the existing source code file, TableOfPowers.cpp, which is located in the C:\SimplyCpp\TableOfPowers directory. c) Opening the template file. Open the TableOfPowers.cpp file in your text editor. d) Displaying the table header. When displaying a table of values, each column of values typically is separated by at least one tab character.you should always begin the table with a header, which is a line of text that labels each column, so that the user can identify the contents of each column. In this application, the first column will display a number, the second column will display its square and the third column will display its cube. Display a line of text containing the words number, square and cube, separated by tab escape sequences.terminate the output string with a newline escape sequence. e) Displaying the values. For the first row of values, display three zeros, each separated by tab escape sequences.terminate the output string with a newline escape sequence. For the next five rows, use the values in Fig.2.3, separating each value by a tab escape sequence and ending each output string with a newline escape sequence.add a second newline escape sequence to the last output string. f) Save your modified source code file. g) Compile the completed application. h) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the output of your completed Table of Powers application with the output shown in Fig. 2.3 to ensure that you wrote your application correctly. i) Close the Command Prompt window. j) Close your text editor or IDE.

Tutorial 2 Welcome Application 5 Answer: 1 // Exercise 2. 2 // Create an application that displays a table of powers. 10 1 15 1 17 cout << "Number\tSquare\tCube\n"; cout << "0\t0\t0\n"; cout << "1\t1\t1\n"; cout << "2\t\t8\n"; cout << "3\t9\t27\n"; cout << "\t1\t\n"; cout << "5\t25\t5\n\n"; 18 return 0 ; // indicate that program ended successfully 19 20 } // end function main 2. (Restaurant Survey Application) In the next tutorial, you will learn how to create applications that retrieve input that the user types at the keyboard. To improve ease of use, many C++ applications include a numbered list of options, called a menu, from which users choose. Instead of typing the name of an option, the user simply enters the option s number. For example, many restaurants allow you to place an order using a number corresponding to a particular menu item. In this exercise, you display a menu of options for a restaurant (Fig.2.37). In later tutorials, you will create applications that respond to the user s selection. Figure 2.37 Restaurant Survey application output. a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\RestaurantSurvey to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project RestaurantSurvey. Save this project in your SimplyCpp directory. Then add the existing source code file, RestaurantSurvey.cpp, which is located in the C:\SimplyCpp\RestaurantSurvey directory. c) Opening the template file. Open the RestaurantSurvey.cpp file in your text editor or IDE. d) Displaying the menu header. Before displaying the menu, you should display a message instructing the user to select one of the options that will follow. Use a cout statement to display a blank line followed by the message, "Select a menu option", followed by a newline escape sequence.

Introduction to C++ Programming (Solutions) Tutorial 2 Answer: e) Displaying the first menu option. Each menu item should consist of the option s number and a description of the option. The options should be presented in increasing numerical order. In this case, the first option is a hamburger. Therefore, display the message "1 - Hamburger" and insert a newline escape sequence to terminate the line. f) Displaying the remaining menu options. The second menu option is Hot dog. Display the option number followed by its description, as you did in Step e. Similarly, display the third option, Salad. Finally, display the fourth option, which is Exit. Each option should appear on its own line. Finally, on a new line, display a question mark (? ), followed by a space.this is where the user will type input to make a selection. In the next tutorial, you will learn how to read the user s response from the keyboard. g) Save your modified source code file. h) Compile the completed application. i) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the output of your completed Restaurant Survey application with the output shown in Fig. 2.37 to ensure that you wrote your application correctly. j) Close the Command Prompt window. 1 // Exercise 2. 2 // Create an application that displays a menu of options. 10 1 15 1 cout << "\nselect a menu option\n"; cout << "1 - Hamburger\n"; cout << "2 - Hot dog\n"; cout << "3 - Salad\n"; cout << " - Exit\n"; cout << "? \n\n"; 17 return 0 ; // indicate that program ended successfully 18 19 } // end function main What does this code do? 2.1 What does the following code do? In particular, what does it display? 1 // Exercise 2.1: WDTCD.cpp 2 // What does this code do? 10 cout << "\n *\n ***\n*****\n ***\n *\n\n"; return 0 ; // indicate that program ended successfully 1 } // end function main

Tutorial 2 Welcome Application 7 Answer: The application displays a diamond. What s wrong with this code? 2.15 The following code segment should display a table listing an item for sale in the first column and the item s price in the second column (Fig.2.38). Figure 2.38 Correct application output. Find the error(s) in the following code: 1 // Exercise 2.15: WWWTC.cpp 2 // This application displays prices for certain items 10 cout << "Item\tPrice"; cout << "Hammer\t$10"; cout << "Nails\t$2"; cout << "Saw\t$15"; 1 15 return 0 ; // indicate that program ended successfully 1 17 } // end function main Answer: In the original code, the output statement is missing a newline escape sequence to allow the next item to be displayed on the next line.also, the first line of Fig. 2.38 indicates that there should be a blank line before the table is displayed. So, there is also a newline missing at the beginning of the corrected code is displayed below. Modified lines are highlighted. 1 // Exercise 2.15: WWWTC.cpp 2 // This application displays prices for certain items 3 // in a store #include <iostream> // required to perform C++ stream I/O 5 using namespace std; // for accessing C++ Standard Library members 7 8 // function main begins program execution 9 int main() 10 { cout << "\nitem\tprice\n";

8 Introduction to C++ Programming (Solutions) Tutorial 2 1 15 cout << "Hammer\t$10\n"; cout << "Nails\t$2\n"; cout << "Saw\t$15\n\n"; 1 return 0 ; // indicate that program ended successfully 17 18 } // end function main Programming Challenge 2.1 (Enhanced Welcome Application) The Welcome application you created in this tutorial displayed plain text. A series of symbols (such as asterisks) can be used to display simple graphics. In this application, you will modify the Welcome application to display a message containing a border, centered text and the word C++ displayed using asterisks (Fig.2.39). Figure 2.39 Enhanced Welcome application output. a) Copying the template to your working directory. Copy the directory C:\Examples\ Tutorial02\Exercises\EnhancedWelcome to your C:\SimplyCpp directory. If you are not using Visual Studio.NET, skip to Step c. b) Creating a new project. If you have not already done so, start Visual Studio.NET.To create a new project, select File > New > Project, causing the New Project dialog to display. Click the Visual C++ Projects folder in the Project Types: pane to display the list of Visual C++.NET project types in the Templates: pane. Select Win32 Console Project from the Templates: pane. In the Win32 Application Wizard, click the Application Settings link, select the Empty project check box, then click Finish. Name the project EnhancedWelcome. Save this project in your SimplyCpp directory. Then add the existing source code file, Welcome.cpp, which is located in the C:\SimplyCpp\EnhancedWelcome directory. c) Opening the template file. Open the Welcome.cpp file in your text editor or IDE. d) Creating the top border. The first line of output should be the top border for your text. In the template code, insert a cout statement that displays a newline, then 3 asterisks followed by a newline. e) Creating the second line of output. To create the left and right borders, each output line should begin and end with an asterisk. Also, each output line must be exactly 3 characters wide. The second line of output does not contain any text, so the line should output an asterisk, followed by 32 spaces, an asterisk and a newline escape sequence (in that order). f) Displaying Welcome to. To center the text Welcome to, display spaces after the first asterisk before displaying Welcome to. Remember to insert a newline at the end of every line you display. g) Displaying C++ Display a line containing only the left and right borders.then use the next seven lines of output to create the C++ out of asterisks, as shown in Fig. 2.39. Then, display a line containing only the left and right borders. Display the bottom border, which should appear exactly as the top border from Step d. Finally, display two newlines. h) Save your modified source code file.

Tutorial 2 Welcome Application 9 Answer: i) Compile the completed application. j) Running the completed application. Select Debug > Start Without Debugging to run your application. Compare the output of your completed Welcome application with the output shown in Fig. 2.39 to ensure that you wrote your application correctly. k) Close the Command Prompt window. l) Close your text editor or IDE. m)(optional) Using a single statement to display the welcome message.try displaying the entire welcome message using a single statement. Note that your statement may continue on multiple lines of source code when you use multiple stream insertion operators. 1 // Exercise 2.1 2 // Create an application that displays a welcome message 10 1 15 1 17 18 19 20 21 22 23 cout << "\n**********************************\n"; cout << "* *\n"; cout << "* Welcome to *\n"; cout << "* *\n"; cout << "* *** * *\n"; cout << "* * * * *\n"; cout << "* * * * * *\n"; cout << "* * *** *** * *\n"; cout << "* * * * * *\n"; cout << "* * * *\n"; cout << "* *** * *\n"; cout << "* *\n"; cout << "**********************************\n\n"; 2 return 0 ; // indicate that program ended successfully 25 2 } // end function main