ENGG 233 - Lab 02 - Fall - 2012 - Final
ENGG 233 - Lab 02 - Fall - 2012 - Final
Lab: 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.
1B
Objectives
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.
2B
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
With this in mind, please read the following problem statement and answer the question below.
Problem Statement
You 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
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 equation
x 2 2x 1 0
). As an example of a
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.
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 In
For this exercise, submit the paper copy of the pseudocode from Task 1.1 and the paper copy of the flowchart for Task 1.2.
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.
Once you click on Finish, the Application settings dialog box will disappear and a new window labeled Solution Explorer will appear in the top right corner of the IDE (or top left on some versions). This window allows you to see what files are parts of your Solution. A Solution in MS Visual Studio is allowed to contain multiple projects. In the file tree displayed you will see your project with the name you gave it on the second level, below it are four folders for External Dependencies, Header Files, Resource Files, and Source Files. If you try to expand these folders you will see that they are empty.
in this and all future labs! You can also select AddExisting Item to add an existing .cpp file into the project, which you will use in the Exercise 3. // ENGG 233 Lab 2 // File: helloWorld.cpp // Author: Student Name, ID# // Created: Date // Last Modified: Date // Topic: Using Create and Run a Hello, World! Program #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
linking errors if you are missing files, or you have called functions in one file that are not defined where they are supposed to be in another file.
What to Hand In
For this exercise, please submit the following paper copies to your TA: The source code for the program (i.e., *.cpp files). The output of the program.
Basic Arithmetic
To begin this task, go to Blackboard and download the file called lab2_3.cpp and put it in a new directory called lab23. Create an empty Win32 console application using Visual Studio, and add lab2_3 into the project. To add a file to a project, there two ways: One easy way is to locate the file in the folder lab23, then drag the file (lab2_3.cpp) and drop it into the Source Files folder on your Solution Explorer pane on your Visual Studio. Or, another way is to take the following steps: Right click on the Source Files folder and select the AddExisting Item. Locate the directory that you just created (lab23), select the .cpp file (lab2_3.cpp) and click on Add to add it to the project. Now the process of adding file lab2_3.cpp to your project is completed and you start working on your code. This file contains most of a program to perform various numerical calculations. At the point labelled 1), write the code to add the values of num1 and num2 and to store the result in the variable sum. At the point labelled 2), write the code to compute the square of the sum of num1 and num2 and to store the result in the variable sumSquared. Use the value of the variable sum computed in the previous step. At the point labelled 3), double the value of the sumSquared variable and store the result in sumSquared. Compile your program. Run the program and record the output.
What to Hand In
Email an electronic copy of the following files to your TA. The TAs list and their email addresses are posted on the Blackboard. The source code for the above task The output of the programs
Figure 1 Figure 2
However, there is a problem. How to retrieve each item from the list?? Of course, we can copy-and-paste multiple times, and change the item number by 1 for each statement. However, this is tedious, difficult to read, and resource-inefficient (e.g., imagine if the list had 1,000 elements!). Remember our goal is not just to write a computer program to accomplish a task, but to accomplish it elegantly and efficiently.
To that end, we can use loops and create a new variable called counter in order to retrieve each item from the list. Lets look at the following statements (Figure 4). First, we declare a new variable called counter, and initialize it (i.e., set its value to 1). Then we create a loop that repeats times. The first time through the loop we retrieve the first item of the list (because we set the counter to 1), and then ask the sprite to say it out. Then we change counter by 1 and repeat the loop. Therefore the second time through the loop we can retrieve the second item in the list (because the counter is now 2), and so on and so forth. By using loops and a counter, we dont need to copy-and-paste multiple times. Isnt it much more elegant?
Figure 3
Write a Scratch Programme to Find the Maximum and Minimum Value from a List
Create a new file in Scratch, and save the file as myscratchLab2.sb. Then create a program to accomplish the following tasks. Create a list called numbers. Add 10 random integers ranging from 1~100 into the list (Hint: Look in the Operators block.). Find the maximum value and the minimum value from the list. Output the maximum and minimum value in the following format: Think The maximum value is for 2 seconds and then Say out the maximum value, and wait for 2 seconds. Think The minimum value is for 2 seconds and then Say out the minimum value, and wait for 2 seconds. Tip #1: Dont forget to initialize the variables with proper values. Tip #2: If you do not delete elements from your list, the number of elements in the list will grow each time you run your program. This is not desired. Tip #3: You can find the algorithm of how to find max and min value from an array on textbook page 250but its in C++.
What to Hand In
Submit your myscratchLab2.sb file electronically to your TA via email (no hardcopy required).