Docsity

C++ Programming Exercise: Using Visual C++ IDE and Solving Syntax Errors, Exercises of Computer Science

Toggle sidebar
bg1
First Programming Exercise
Objective
This exercise provides an introduction to the programming environment that you will be using
during this course. You will learn how to enter, edit, compile and run programs in this
environment. You will also see some of the syntax of the C++ language that you will be using
during the course and observe typical error messages that the compiler gives you when you
make syntax errors in your program.
Overview of Exercise
You will start with a separate set of instructions that tell you how to use Visual C++ integrated
development environment (IDE) for entering, editing, compiling and running programs. This
separate set of instructions is designed to be a standalone document that you can use for later
reference in the course. If you have not already obtained a user ID and password for the College
of Engineering and Computer Science network, you will have to do this before you begin the
exercise.1
Once you have started the IDE and are ready to enter a program, you will copy programs from
these notes into the IDE. The separate IDE instructions will tell you how to compile and run the
programs. These exercise notes give you four separate programs to run. The final program that
you will be given has syntax errors that will be found by the compiler. This will show you how to
correct errors that are found in the compilation. You should submit two printed programs as
discussed below.
Details of the Exercise
The programs to be entered are shown on the attached pages. Note that single lines starting with
two forward slashes (//) and all text between the symbols /* and */ are comments. These do not
affect the program execution. The use of comments is an important programming practice to
explain the operation of code.
Follow the steps below for this exercise.
1. Use the separate set of instructions for using the IDE to start the IDE, define a project and
prepare a blank source code file.
2. At step 8 in the IDE instructions, copy version one of the program shown on page three of
this assignment into the blank program file. Follow steps 9 to 14 of the IDE instructions to
compile and execute your program.
3. This program is supposed to print a table of the squares of all numbers from zero to ten.
Examine the results of the execution to make sure that they are correct.
4. Edit the original program to produce version two shown on page four. This should require the
addition of only four lines to provide input cues and read the numbers entered by the user.
Save the edited program with the same file name. Again follow steps 9 to 14 of the IDE
instructions to compile, link and run the program. Input values to compute the fourth power of
all numbers from zero to fifteen.
5. Edit version two to produce version three, shown on page five. Again, keep the same file
name. Compile and run the program to produce the following three tables: (1) the square of
1 Log on to the computer with the user name “account” and the password “account” and follow the online
directions for getting an account number
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download C++ Programming Exercise: Using Visual C++ IDE and Solving Syntax Errors and more Exercises Computer Science in PDF only on Docsity!

First Programming Exercise

Objective

This exercise provides an introduction to the programming environment that you will be using

during this course. You will learn how to enter, edit, compile and run programs in this

environment. You will also see some of the syntax of the C++ language that you will be using

during the course and observe typical error messages that the compiler gives you when you

make syntax errors in your program.

Overview of Exercise

You will start with a separate set of instructions that tell you how to use Visual C++ integrated

development environment (IDE) for entering, editing, compiling and running programs. This

separate set of instructions is designed to be a standalone document that you can use for later

reference in the course. If you have not already obtained a user ID and password for the College

of Engineering and Computer Science network, you will have to do this before you begin the

exercise.^1

Once you have started the IDE and are ready to enter a program, you will copy programs from

these notes into the IDE. The separate IDE instructions will tell you how to compile and run the

programs. These exercise notes give you four separate programs to run. The final program that

you will be given has syntax errors that will be found by the compiler. This will show you how to

correct errors that are found in the compilation. You should submit two printed programs as

discussed below.

Details of the Exercise

The programs to be entered are shown on the attached pages. Note that single lines starting with

two forward slashes (//) and all text between the symbols /* and */ are comments. These do not

affect the program execution. The use of comments is an important programming practice to

explain the operation of code.

Follow the steps below for this exercise.

1. Use the separate set of instructions for using the IDE to start the IDE, define a project and

prepare a blank source code file.

2. At step 8 in the IDE instructions, copy version one of the program shown on page three of

this assignment into the blank program file. Follow steps 9 to 14 of the IDE instructions to

compile and execute your program.

3. This program is supposed to print a table of the squares of all numbers from zero to ten.

Examine the results of the execution to make sure that they are correct.

4. Edit the original program to produce version two shown on page four. This should require the

addition of only four lines to provide input cues and read the numbers entered by the user.

Save the edited program with the same file name. Again follow steps 9 to 14 of the IDE

instructions to compile, link and run the program. Input values to compute the fourth power of

all numbers from zero to fifteen.

5. Edit version two to produce version three, shown on page five. Again, keep the same file

name. Compile and run the program to produce the following three tables: (1) the square of

(^1) Log on to the computer with the user name “account” and the password “account” and follow the online

directions for getting an account number

all numbers from zero to fifty, (2) the fifth power of all numbers from zero to ten, and (3) the

reciprocal (-1 power) of all numbers from zero to fifteen. Note what happens when you try to

compute the reciprocal power of zero.

6. Submit a copy of the listing for version three, modified as follows: The series of

comments below describe what different commands in the code are doing. Copy the

comments below (each // and the words that follow the //) to the place in the code just before

the C++ command that you think is doing the action described in the comment. You may not

be sure what the correct location is, but make your best guess about the correct location for

the comment. When you copy the comments, make sure that you do not eliminate any lines

of code or cause lines of code to be moved to the end of your comment.

// The line below reads in the power to which the number is raised

// The line below prints the original number and the number raised to a power to the screen

// The line below causes the program to repeat for each number in the table

// The line below causes the program to repeat as long as the user enters Y or y

7. Copy version four of the program from the online exercise into the IDE replacing the code

used for version three. Run the compiler on version four, which has several deliberate syntax

errors. The compiler will detect these errors and give you messages about the errors in the

window below the listing. You can quickly find the line to which the error message applies by

double clicking on the line number in the error message. This will set a blue pointer next to

the line associated with the error message.

8. Submit a copy of the listing for version four, modified as follows: Identify and correct

the errors in version four by comparing that version to version three. In some cases such as

a missing semicolon, the error may be on an adjacent line. In other cases the error message

may be misleading. For example, there is one case where a missing << is flagged as an

error for a missing semicolon. In other cases a single error may generate more than one

error message. Before correcting the error, copy the line with the error and turn it into a

comment as noted in the example below.

a. This example looks at the statement int x = 2 (which is not part of any program in this

exercise); this statement is incorrect because it is missing a semicolon

b. This error should be marked and corrected the error so that it appears as follows:

// int x = 2 missing semicolon -- correct line below int x = 2;

Submission Requirements Due Date: February 2.

Submit copies of the code (sometimes called program listings or listings) you prepared following

the directions in items 6 and 8 above: (6) a listing of version 3 with the comments giving your best

guess of where the code is performing the tasks indicated and (8) a corrected copy of version 4

with the errors left in the listing and changed to comments with a description of the error. Submit

a printed listing or send an email to [email protected] with your listings before 11:59 pm on the

due date.

Important points about C++ programs

Note the following important C++ syntax items in the listing.

1. The semicolon (;) is used to end statements in the C++ language?

2. Curly braces, { and }, are used to separate logical blocks of code. Such code blocks are

used in looping and conditional statements.

3. The command cout with the output (<<) operator is used for screen output and the

command cin with the input (>>) operator is used for keyboard input.

{ double result = pow( counter, power ); cout << counter << "\t" << result << endl; } return EXIT_SUCCESS; }

Second Version

The next program listing shows version two of the program in which four statements are added to

allow the user to specify the power used in the calculation and the number of entries in the table.

(The modifications to comment statements explain the changes, but these are not required for

program compilation and execution.) The new and modified statements are shown in bold to

simplify your making the changes. Note that as the program listing becomes longer additional

comment statements are added to set off the different parts of the code. Start your copy with the

line below.

First programming exercise for Comp 106, spring 2006

Programmer: Enter your name here

Date: January 31, 2006

Version: 2.

Environment: Microsoft Visual C++ .Net with Windows XP

This program computes a table of the powers of the numbers from zero to some maximum number. Both the power and the maximum number are entered by the user. */

#include #include using namespace std;

int main() { int power = 2, // Power used for exponentiation maximumNumber = 10; // Maximum number in table

// Input section

cout << "Enter the power for the exponentiation table: "; cin >> power; cout << "\n\nEnter the maximum number of table entries: "; cin >> maximumNumber;

// For loop does calculations and output

for (int counter = 0; counter <= maximumNumber; counter++) { double result = pow( double( counter ), power ); cout << counter << "\t" << result << endl; } return EXIT_SUCCESS; } // end copy with this line

Third Version

In this version, additional statements are added which allow the program to be run repetitively.

These new and modified statements (including comment statements) as shown in bold. Start

your copy with the line below.

/* First programming exercise for Comp 106, spring 2006

Programmer: Enter your name here

Date: January 31, 2006

Version: 3.

Environment: Microsoft Visual C++ .Net with Windows XP

This program computes a table of the powers of the numbers from zero to some maximum number. Both the power and the maximum number are entered by the user. The program runs repeatedly until the user signals that no more cases are to be run. */

#include #include using namespace std;

int main() { int power = 2, // Power used for exponentiation maximumNumber = 10; // Maximum number in table

// Program runs in infinite loop using while // statement below. At end of loop user is // prompted to enter another case of exit. If // user chooses to exit then program is halted.

char yesNo; // Gets user input to continue or quit do { // Input section

cout << "Enter the power for the exponentiation table: "; cin >> power; cout << "\n\nEnter the maximum number of table entries: "; cin >> maximumNumber;

// For loop does calculations and output

for (int counter = 0; counter <= maximumNumber; counter++) { double result = pow( double( counter ), power ); cout << counter << "\t" << result << endl; }

// Prompt for additional cases

cout << "\n\nDo you want to run another case Y[es]/N[o]? "; cin >> yesNo;

while ( (yesNo == 'Y') || (yesNo == 'y') ); return EXIT_SUCCESS; }