ENGG 233 - Lab 02 -Fall- 2012_final

  • View

  • Download

Embed Size (px)

Text of ENGG 233 - Lab 02 -Fall- 2012_final

ENGG 233 - Computing for Engineers I Lab Assignment Fall 2012Lab: Title: Starts: Due Dates: 2 Introduction Algorithm Development, the Microsoft Visual Studio, and Basic C++ Week of September 24, 2012 Exercise 1 (pre-lab exercise) at the start of your scheduled lab session. Exercise 2 (in-lab exercise) by the end of your scheduled lab session. Exercises 3 and 4, Week of October 1, 2012

This lab consists of four exercises. The first one is a pre-lab exercise that the answer must be provided by you before going to your scheduled lab session, and MUST be handed in (paper copy), personally to the lab TA at the beginning of the lab. The second exercise is an in-lab exercise that MUST be implemented during the scheduled lab session, and must be handed in (paper copy), before the end of the lab to your TA. Other exercises must be handed in electronically, via email to your TA, anytime before the indicated due date for your lab section.



The objectives of this lab are as follows: Learn how to use flowcharts as means of describing an algorithm Get familiar with creation of a simple Win32 console application with Visual Studio Get familiar with variable declaration and implementation of basic arithmetic operations Writing and testing basic C++ programs from scratch.


Relevant Course Material

This lab deals with basic C++ programs including variable declaration, use of built-in functions, mathematical expressions and strings. In order to derive the maximum benefit from the lab, students are encouraged to read the following material prior to completing the exercises: Lecture Notes Horstmann: Sections 1 and 2 (basic syntax) Sections 1.1 to 1.9; 2.1 to 2.4

Exercise 1 Pre-lab Exercise: Algorithm Development (22 pts)Note: This exercise must be completed prior to your scheduled lab period and a hardcopy must be submitted to your TA at the start of the lab period.Programming is as much about developing and describing algorithms as it is about actually writing source code. In fact, in many situations, the development of the algorithm is done separately from the writing of the source code (possibly by different people). It is therefore critical that your algorithms be well described. To this end, there are two options; pseudocode and flowcharts. The following tasks will get you to describe an algorithm using both of these methods.

Describing an Algorithm using PseudocodePseudocode is a method of describing the steps associated with an algorithm in plain English. There is no standard for how to write pseudocode. However, please read the following sections of the textbook to get some good suggestions for how to address the problem (another example is also given Task 1.2 below) How To 1.1 on page 20 (2nd edition) and page 24 (older edition) . Worked Example 1.1 which is available at the following webpage: http://higheredbcs.wiley.com/legacy/college/horstmann/0470927135/workedex/ch 01_we1.pdf In addition to the above, some key points to remember are Make sure you satisfy the three requirements of any algorithm; it must be unambiguous, executable and terminating. Do not use language-specific (e.g., C++-specific) terminology. In fact, the sign of good pseudocode is that it can be used to implement an algorithm in any language, not just C++!

With this in mind, please read the following problem statement and answer the question below.

Problem StatementYou have been given the problem of writing an algorithm to solve quadratic equations. The input to the algorithm are the three coefficients, , and of the quadratic equation to be solved

Then, the solutions to the quadratic equations are given by

The algorithm should be able to output both real and complex results (i.e., those results which require taking the square root of negative number; complex result, consider the following equationx 2 2x 1 0

). As an example of a

whose solutions are given by

Note, however, that a computer cannot compute the square root of a negative number. If this is required, your algorithm should compute the square root of the absolute value of the number and prefix the output (scaled by the denominator) with an . Mathematically, this is equivalent to writing the solution to the quadratic equation as

Question Write pseudocode to describe an algorithm to solve the above problem.

Describing an Algorithm using FlowchartsFlowcharts are often used to describe an algorithm visually. For the purpose of ENGG 233, we will only consider three flowcharts components, as described in Table 1 below. With this in mind, read the following pseudocode to determine the level of discount a buyer receives based on the amount of products being purchased, and to output the discounted price Prompt the user for the total regular price of the purchase Read in the total regular price of the purchase If the total regular price is greater than $100, then discount = 0.2 (20 %), otherwise, discount = 0.1 (10 %)

Compute: discounted price = ( 1 - discount ) x total regular price Output the discounted price

Table 1 - Flowchart Components and Descriptions Component Description This indicates the start and/or end of the algorithm. Every algorithm must have a start and end. Input/Output. This is used to indicate that information is being output or being read in. It should be clear as to whether the component refers to input or output. Statement or process. This describes a specific instruction (or group of similar instructions). A statement will have one or more entry points but only a single exit point. Decision. Used to select an execution path based on a decision. There are one or more entry points, and two exit points. The exit points correspond to when the decision is true (yes) or false (no), and must be labelled appropriately. Example See component

Question Draw a flowchart to represent the above algorithm.

What to Hand InFor this exercise, submit the paper copy of the pseudocode from Task 1.1 and the paper copy of the flowchart for Task 1.2.

Exercise 2 In-Lab Exercise: Introduction to Microsoft Visual C++ (7 pts)Note: This exercise must be completed during the lab period and a hardcopy must be submitted to your TA, by the end of the lab session.Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It supports different programming languages such as Visual Basic, Visual C#, Visual C++ etc. An IDE is a software application that provides comprehensive facilities to computer programmers for software development. In this course, we will use MS Visual C++ environment to study C or C++. We will learn how to use MS Visual C++ to develop console applications. The development of graphical user interface applications is for advanced users which is not included in this course. In the following, we will learn how to create a simplest console application using MS Visual C++.

Creating a Console Application Using MS Visual C++To launch Microsoft Visual C++, from the start menu select ProgramsMicrosoft Visual Studio 2010Microsoft Visual Studio 2010. If this is the first time you are using this software on the computer, it will ask you to choose your default environment settings. You need to select Visual C++ Development Settings from the list, and then click on Start Visual Studio. Visual Studio will configure the IDE in Visual C++ development settings. It may take a few minutes to configure. Be patient. The Start Page should now appear.

Creating a new projectWhen MS Visual Studio 2010 opens, you will see a Start Page. This is just Microsoft propaganda and should be ignored. There is also a window of recent projects and a window of Getting Started. I would also suggest ignoring the Getting Started tutorials as they are too complicated and are aimed at professional developers. Instead, create a new project by selecting FileNewProject. A new project dialog box will pop up as shown in Figure 1. As the IDE is set as Visual C++ development environment, by default, Visual C++ and Win32 Console Application in the Templates panel are highlighted. To develop different applications, select different modules such as MFC Application and Win32 Project etc. For all labs in ENGG 233 select Visual C++ and Win32 Console Application in the New Project dialog box. Type a name for the application in the Name box, and in the Location box, type the path to where you want the application to be created. The default location is somewhere in your home drive, H:\My Documents\Visual Studio 2010\Projects, which is not convenient to use. Change the location to the root directory of Home drive H and type

lab2 in the Name textbox. Unselect the Create directory for solution in the bottomright of the new project dialog box, and press OK. A dialog box called Found a suitable location for browsing database and IntelliSense files will appear. This dialog appears because we are creating a project on a network drive. If you create a project on a local drive, the dialog wont appear. You need to click OK, and the Win32 Application Wizard will appear. On the left of the box, select Application Settings. In the Applications Settings Window, make sure that Console Application is selected (the default). In the Additional options, select Empty Project instead of Precompiled Header. Your selections here are very important so be very careful with this step. Different selections will cause totally different results. Make sure your selections are the same as those shown in Figure 2, and then click Finish. A folder lab2 will be created in the H drive, which contains an empty Win32 console project that Visual Studio generated for you.

Figure 1 New project dialog box

Figure 2 Application settings dialog box

Once you click on Finish, the Application settings dialog b