CS 1408 Intro to Computer Science with VB.NET



[pic] |Department of Computer

and

Mathematical Sciences |CS 1410

Intro to Computer Science with

C++ |5 | |

Lab 5: Syntax and Expressions

Objectives:

The main objective of this lab is to understand Syntax and Arithmetic Expressions in C++, and to gain experience with the Visual C++ debugging facilities.

Like text written in a natural language, such as English, a program written in C++ consists of a series of Tokens. A token is a symbol or group of symbols that the compiler interprets as a single unit, and which has a definite meaning or purpose in C++ code. There are many different types of tokens: keywords (like "int" or "if"); identifiers (like variable names); operators (like "+" or "="); constants (like "3.14" or 'A'); comments (program documentation); punctuation (like ";"); and compiler directives (lines beginning with "#"). Again as in English, C++ has rules of grammar that govern how tokens can be formed and combined to construct grammatically correct C++ instructions and programs. These rules are called the SYNTAX RULES of the language. The syntax rules for C++ are less complicated, but at the same time more precise, than the syntax rules for English. Where it has taken you several years of writing and speaking English to master its syntax rules (and most of us still make lots of mistakes!), you will find it takes only a few weeks to master C++ syntax. A great help in this task is the fact that a C++ program must be syntactically PERFECT before it can be compiled and executed. While it is possible to write an understandable English sentence that is full of grammar mistakes, like misspellings and improper punctuation, the compiler program cannot convert a C++ program with syntax mistakes to machine code instructions. The compiler finds and reports syntax errors in a program, usually down to the line number in the program file where the error occurs. So each time you write and compile a C++ program, you will get a miniature C++ syntax lesson, if the program contains mistakes. But even if a C++ program has correct syntax, this does not mean the program is SEMANTICALLY correct. That is, the program may still not work like programmer intends for it to work. These sorts of errors or BUGS cannot be detected by the compiler, but must be found by the programmer when he or she is testing the program, or when a user is actually running the program. For this reason, SEMANTICS errors are called "run-time" errors. Companies spend large sums of money testing software for run-time errors before it is put into production. Frequently, they will send test copies of the software to certain users before the final version is released, and ask the user to report any bugs they find in the program. This process is called BETA TESTING. Semantics errors occur in natural languages as well. The following sentence is syntactically (grammatically) correct. Is the sentence semantically correct? That is, does it make sense? THE GREEN CAT DRIVES A TREE.

Task 1: Syntax and Semantics

The purpose of this task is to help understand SYNTAX in C++, and the process of DEBUGGING a program.

As we previously mentioned, the compiler is capable of finding and reporting syntax errors in a program, usually down to the line number where the error occurs. The compiler is also sophisticated enough to often tell the programmer how the error can be fixed. As you will see in this task, however, sometimes the error messages from the compiler can be confusing or obscure, and sometimes a single syntax error can cause the compiler to generate a "chain reaction" of error messages. It takes some practice to become proficient at interpreting compiler messages.

Activity 1.1: Study the following program carefully. NOTE: The line numbers in this program are intended for your convenience in this task, but are not required in all C++ programs. These line numbers have been inserted as comments. One interesting aspect of this program is that it employs two FUNCTIONS, pow( ) and sqrt( ), to perform some of the necessary arithmetic computations. The definition of a "function" in programming is similar the mathematical use of this term. A FUNCTION is a named SUB-PROGRAM that accepts a certain number and type of data values as inputs, and then processes these inputs to produce a single output value. Functions are a very useful and important element of C++ programming, as this program illustrates. The function pow( ) accepts two floating-point values as inputs (also called ARGUMENTS or PARAMETERS) and then outputs (or RETURNS) the first input raised to the second input power. The output is a floating-point data value. The arguments are separated by a comma inside of parentheses. For example, pow(5,2) outputs the value 25.0, and pow(9,0.5) outputs the value 3.0. The use of a function in a C++ statement is said to be a FUNCTION CALL (like pow(5,2) ). On the other hand, the function sqrt( ) accepts a single floating-point argument, and returns the principal square root of this argument (as a floating-point data value). Thus sqrt(100) returns 10.0. For reasons that will be detailed more fully later on, the COMPILER DIRECTIVE "#include " must be inserted into a program before the functions pow( ) and sqrt( ) can be used (see line 4).

Activity 1.2: Predict the SYNTAX errors in the previous program by listing the statements that contain syntax errors. Give the line number of the error and a BRIEF description of the error. (Find as many as you can.)

| |

Activity 1.3: Start MS Visual Studio and create a C++ project called Lab5. Download Lab5Tsk1.cpp file from my webpage. Then add the file Lab5Tsk1.cpp to the project by choosing Add Existing Items... command from the Project menu. This cpp-type file holds the source code shown in Activity 1.1. Then compile the program Euclidean_Distance by choosing Build command from the Build menu. The compiler will inform you that this program contains syntax errors. A list of syntax errors as well as COMPILER WARNINGS will appear at the bottom of the C++ window. A compiler warning is not as serious as a syntax error but alerts you to items in your program that may cause run-time errors. Report all the syntax errors found when compiling the program in the box below, by listing the line number where each error occurs. Do not write the error message.

| |

Activity 1.4: Compare your predicted errors from Activity 1.2 to the observed errors in Activity 1.3. Explain any discrepancies as best you can. In other words, explain why you didn't find an error that occurred, or why you predicted an error that didn't actually occur.

NOTE: By double clicking on a particular error line in the list of error messages, a file edit window will open (this may take a few seconds), and the cursor will be positioned at the line in the program where the compiler detected that error.

| |

Activity 1.5: Now try to correct the syntax errors found by the compiler in Activity 1.3. To do this, go to the window under the tab Lab5Tsk1.cpp Such a window should already be open from the previous activities. Make whatever changes you feel are necessary to the program to correct the bugs. Then run the program. Of course, if the program still does not compile, you must read the error messages and try again to fix the bugs. If the program is now syntactically correct, try using the coordinates (3,-10) and (6,8) as the input. Record the COMPLETE output below. Then print and turn in a copy of the modified version of the source program along with the exercise.

| |

Task 2: Arithmetic Expressions

The purpose of this task is to help understand Arithmetic Expressions in C++.

Like in mathematics, an EXPRESSION in C++ is a collection of constants, variables, grouping symbols, and operators that can be simplified to a single value. This process is called evaluating the expression. Also as in mathematics, expressions are evaluated according to an Order of Operations that determines the order in which the operators are to be applied. However, since C++ has many more operators than common mathematics, there are many more types of expressions in C++, and the order of operations is more complicated. But for arithmetic expressions, that only involve standard arithmetic operations and functions, the order of operations is the same as you studied in algebra.

Activity 2.1: Study the following program carefully. Then predict the value of each Upper Case variable following execution of a statement that contains the Upper Case variable. There are two functions that require some explanation. The function ceil( ) rounds UP the input to the nearest whole number, and the function floor( ) rounds DOWN the input to the nearest whole number.

|#include |DNUMBER = 2*6 / sqrt(4.0); |

|#include |DNUMBER = |

|#include | |

|using namespace std; | |

|// Program name: Precedence |DNUMBER = 20 + 4 * 2 - 8; |

|int main(void) |DNUMBER = |

|{ | |

|int INUMBER; | |

|double DNUMBER; |INUMBER = (20 + 4) * 2 - 8; |

|DNUMBER = 2*6 / sqrt(4.0); |INUMBER = |

|DNUMBER = 20 + 4 * 2 - 8; | |

|INUMBER = (20 + 4) * 2 - 8; | |

|DNUMBER = 20-2*3-sqrt(25.0)/2; |DNUMBER = 20-2*3-sqrt(25.0)/2; |

|DNUMBER = 20.0 / 6.0; |DNUMBER = |

|INUMBER = 20 / 6; | |

|INUMBER = 40 / 6 % 2; | |

|INUMBER = (INUMBER - 120) / 3; |DNUMBER = 20.0 / 6.0; |

|INUMBER = INUMBER % 3; |DNUMBER = |

|DNUMBER = pow(sqrt(4.0), 3); | |

|DNUMBER = (10 + 30) / (2 * 3); | |

|DNUMBER = 10 + 31 / 2.0 * 3; |INUMBER = 20 / 6; |

|DNUMBER = 10 + 31 / 2 * 3.0; |INUMBER = |

|INUMBER = 10 + 20 / 3; | |

|INUMBER = ceil(29.63); | |

|INUMBER = floor(29.63); |INUMBER = 40 / 6 % 2; |

|INUMBER = 20 / 4 * 10; |INUMBER = |

|INUMBER = 10 * 20 / 4; | |

|cin.get(); cin.get(); | |

|return 0; |INUMBER = (INUMBER - 120) / 3; |

|} |INUMBER = |

| | |

| | |

| |INUMBER = INUMBER % 3; |

| |INUMBER = |

| | |

| | |

| |DNUMBER = pow(sqrt(4.0), 3); |

| |DNUMBER = |

| | |

| | |

| |DNUMBER = (10 + 30) / (2 * 3); |

| |DNUMBER = |

| | |

| | |

| |DNUMBER = 10 + 31 / 2.0 * 3; |

| |DNUMBER = |

| | |

| | |

| |DNUMBER = 10 + 31 / 2 * 3.0; |

| |DNUMBER = |

| | |

| | |

| |INUMBER = 10 + 20 / 3; |

| |INUMBER = |

| | |

| | |

| |INUMBER = ceil(29.63); |

| |INUMBER = |

| | |

| | |

| |INUMBER = floor(29.63); |

| |INUMBER = |

| | |

| | |

| |INUMBER = 20 / 4 * 10; |

| |INUMBER = |

| | |

| | |

| |INUMBER = 10 * 20 / 4; |

| |INUMBER = |

| | |

| | |

Activity 2.2: Delete the file Lab5Tsk1.cpp from the Lab5 project in Task 1 by selecting the icon [pic]in the Solution Explorer window. Then press Delete key. This will delete the file Lab5Tsk1.cpp Next download the file Lab5tsk2.cpp from my webpage and add the file Lab5tsk2.cpp to the project by choosing the Add Existing Items... command from the Project menu. This file contains the program shown in Activity 2.1. Then trace the program Precedence through the debugger by pressing F11. After F11 is pressed, the Watch 1 window will appear at the bottom of the screen. In the Watch 1 window, type in the variables that you want to observe (DNUMBER and INUMBER) on each line in the column Name as shown in the figure below.

[pic]

Use the F10 key repeatedly to step through each line of Precedence. After executing each statement, observe the value in the Value column and record the value of the variable. The value should appear in red.

|DNUMBER = 2*6 / sqrt(4); |DNUMBER = | |

|DNUMBER = 20 + 4 * 2 - 8; |DNUMBER = | |

|INUMBER = (20 + 4) * 2 - 8; |INUMBER = | |

|DNUMBER = 20-2*3-sqrt(25)/2; |DNUMBER = | |

|DNUMBER = 20.0 / 6.0; |DNUMBER = | |

|INUMBER = 20 / 6; |INUMBER = | |

|INUMBER = 40 / 6 % 2; |INUMBER = | |

|INUMBER = (INUMBER - 120) / 3; |INUMBER = | |

|INUMBER = INUMBER % 3; |INUMBER = | |

|DNUMBER = pow(sqrt(4.0), 3); |DNUMBER = | |

|DNUMBER = (10 + 30) / (2 * 3); |DNUMBER = | |

|DNUMBER = 10 + 31 / 2.0 * 3; |DNUMBER = | |

|DNUMBER = 10 + 31 / 2 * 3.0; |DNUMBER = | |

|INUMBER = 10 + 20 / 3; |INUMBER = | |

|INUMBER = ceil(29.63); |INUMBER = | |

|INUMBER = floor(29.63); |INUMBER = | |

|INUMBER = 20 / 4 * 10; |INUMBER = | |

|INUMBER = 10 * 20 / 4; |INUMBER = | |

Task 3: More Debugging Facilities

The purpose of this task is to demonstrate some more of the debugging facilities available in the Visual C++ programming environment.

Because programming is a complex task, programmers inevitably make many mistakes. In fact, virtually no program, no matter how simple, will compile or execute properly the first time. As you will discover as the course progresses, programming is a process of constant revision and correction. Some programming errors are relatively benign, such as typos, while others can involve subtle mistakes in syntax or logic which are difficult to find and correct. Correcting errors in program logic or syntax is known as DEBUGGING the program. Visual C++ contains many features that can help you debug your programs. Let's explore some of these features in this task.

Activity 3.1: Delete the file Lab5Tsk2.cpp from the Lab5 project in Task 2. Next download the file Lab5tsk3.cpp from my webpage. Then add the file Lab5tsk3.cpp to the project by choosing the Add Existing Items... command from the Project menu. This file contains the program shown below. Read the comments in the program to understand its purpose and how to use it.

Activity 3.2: Run the program by choosing Start Debugging from the Debug menu (Shortcut: Press F5). Test the program with each input below. This means you will have to run the program four times. After each execution you will need to close the program's user window. Does the program seem to be correct for each input? Report your answers in the box provided.

|i) 33 |ii) 99 |iii) 85 |iv) 0 |

| | | | |

| | | | |

| | | | |

| | | | |

Activity 3.3: Run the program with the input -17. Does the program appear to execute any differently with this input? Does the program warn the user that an impossible amount of money is input? How can this be considered a weakness of the program? Report your answers in the boxes below.

|Execute differently? (yes or no): | |Weakness: |

| | | |

Activity 3.4: Most programming environments such as Visual C++ contain debugging tools that are designed to aid in debugging a program. One tool of this type allows a programmer to TRACE a program, meaning execute a program one statement at a time, and watch the value of a variable change as a program executes. Remember, a Variable is a named location in the memory of the computer where data is stored while a program is executing. In this activity, we will trace the program and watch the variable AmountLeft. To trace the program, press F11 and add the variable AmountLeft to the Watch 1 window. Then each time you press F10, the next statement in the program is executed. The NEXT executable statement is indicated by a bright yellow arrow. Step through the program, using the input value 62. You can toggle back and forth between Visual C++ and the program window at any time by clicking on the CS_Project icon bar on the status bar. Try doing this after executing an output statement (a statement starting with the identifier "cout"), to see the effect of the output statement. Note the changes in the value of the variable AmountLeft as you trace the program. In the box below, record each value that AmountLeft assumes as the program runs.

| |

Activity 3.5: When Visual C++ compiles a project, it creates an Executable program file with the file extension ".exe" This file contains the machine-level instructions that the computer follows as it executes the program. (Actually, the compiler creates several additional files in the process of compilation.) This executable program can be run independently of the Visual C++ environment. In fact, it can be run on any machine that uses the Windows operating system. To demonstrate this, look for the Lab5 application file icon [pic] in the Debug folder under Lab5 folder. To run the program file Lab5, double click on the icon [pic]. After the application is executed, the DOS prompt window will appear. You can enter an input and get the output as usual.

| |

| |

[pic][pic]

-----------------------

#include /*1*/

#include /*2*/

#include /*3*/

using namespace std; /*4*/

/*5*/

//6 This program computes the euclidean distance (straight-line distance)

//7 between two points in the cartesian coordinate system, by using the

//8 Distance Formula.

/*9*/

//10 Program name: Euclidean_Distance

/*11*/

/*12*/ int main(void)

/*13*/ {

/*14*/ double X_Coord1, X_Coord2, Y_Coord1, Y_Coord2;

/*15*/ double Distance

/*16*/

/*17*/ cout>X_Coord1>>y_Coord1;

/*19*/ cout>Y_Coord2;

/*22*/

/*23*/ Radicand = pow(X_Coord2 - X_Coord1,2) + pow(Y_Coord2 - Y_Coord1,2);

/*24*/ Distance = sqrt(Radicand));

/*25*/

/*26*/ cout";

cin>>Amount;

AmountLeft=Amount;

Quarters=AmountLeft/25;

AmountLeft=AmountLeft%25;

Dimes=AmountLeft/10;

AmountLeft=AmountLeft%10;

Nickels=AmountLeft/5;

AmountLeft=AmountLeft%5;

Pennies=AmountLeft;

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download