Applicable Program Outcomes, Course Outcomes, and Learning Objectives: PO-5, CO-6, LO-10

CPET 1104, Microcomputer Applications

Laboratory Exercise 11

Introduction to C/C++ Programming using Borland C++

Set 1

Applicable Program Outcomes, Course Outcomes, and Learning Objectives: PO-5, CO-6, LO-10, LO-11

Objective:

To introduce you to some of the fundamentals of the C/C++ language by studying, writing, and running some short programs.

The general procedure that you will be asked to use in this exercise set is to:

® type in a program.

® save the program.

® compile the program; if there are any compile errors, make corrections and compile again.

® run the program.

® check the program's output for errors; if any run-time errors are found, make corrections
in the program, save it again, compile it again, and run it again.

In this exercise set you will be given a sample program to type in, save, compile and run. After you have gotten a sample program working properly, then you will be asked to write a similar

program of your own. This procedure will be repeated throughout the exercise sets.

The elements of the C/C++ language that will be covered in this exercise set are:

· comment characters //

Example: // This is a comment.

Comments are not necessary for running a program – the computer doesn't need
them. Comments are useful for identifying things about the program for the benefit of us
humans. Another way to insert a comment is /* This is a comment. */ which uses the older
C language convention.

Revised 2-28-07

· variables

Examples: ch = 'A'; weight = 20.5; num = -24;

A variable corresponds to a memory location in the computer's memory where a value can be
stored. In the C++ language, variables must be declared before being used in a program (See
the next item below.).

· declaration of data types for variables

Examples: char ch; float weight; int num;

ch = 'A'; weight = 20.5; num = -24;

A variable of type character char is used to hold a single 8-bit alphanumeric character.
Integer int, holds whole numbers and may be signed (-32,768 to 32,767 in size). A variable
that is declared float holds a signed floating-point value, that is, a decimal type number.

· program output - output to the screen using “cout” (console output)

Examples: cout < "This will print to the screen";

cout < "The character ch is " < ch;

cout < "The integer num is " < num;

cout < "The weight is " < weight;

· input from the keyboard using “cin” (console input)

Examples: cout < "enter a character ";

cin > ch; //keyboard input

cout < "enter a number ";

cin > num; //keyboard input

cout < "enter a weight ";

cin > weight; //keyboard input

· arithmetic operators

Examples: sum = 8 + 2; // addition

cout < "the sum is " < sum;

diff = 12 - 7; // subtraction

cout < "the difference is " < diff;

product = 3 * 9; // multiplication

cout < "the product is" < product;

quotient = 8.4 / 2.1; // division

cout < "the quotient is" < quotient;

modulus = 11 % 3; // modulus

cout < "the modulus is " < modulus;

The modulus operator returns the remainder for integer division. So, in the last example above,
the modulus operation 11 % 3 would yield a value of 2. Notice that 15 % 5 would yield zero
(i.e., zero remainder).

Now use the directions in the following sections for an introduction to some of the essential elements of C/C++.

1. Getting started, saving and retreiving a file

Put a diskette in the drive and double-click the Borland C++ icon on the Windows 2000 desktop. If the blue editor screen with E:\Temp\NONAME00.CPP on the title bar doesn't appear, then select File | New | Text Edit from the Borland C++ menu bar to open the text editor.

If your C++ editor window is not already maximized (i.e., fills most of the screen), then maximize it.

Move the mouse cursor across the icons on the Tool Bar, stopping on some of the icons to read the descriptions for some of the operations.

The Tool Bar operations that will be most often used in this exercise are indicated in the figure below.


Type the // comment statements given below. Start typing in the first column on the screen. In place of the letters FML put your initials (for example, for Rita L. Smith you would use RLS??.CPP ).

//

// your name

// CPET 1104 - your section

// today's date

// FML??.CPP (Remember: FML = your initials)

//

1

CPET 1104 C/C++ Set 1

Comments are not actually necessary for the running of a program (The computer doesn't need them.), but they are useful for identifying things about the program. Comments may generally be placed anywhere in a program.

As indicated in the box below, save these comment statements in a file so that they can be used over again as a heading for each new program you write.

After the file is saved, check the Title bar at the top of the editor window; it should show the new path and filename a:\heading.cpp, as shown below.

Next, clear the editor window by following the directions below.

Now retrieve the file HEADING.CPP from your diskette A: as described in the box below. In Step 3, select HEADING.CPP for the filename.

2. FML01.CPP - the "cout" statement

The comments in your HEADING.CPP file should still be on the screen. If not, then retrieve the file HEADING.CPP from your diskette .

Change the comment // FML??.CPP to // FML01.CPP (Replace FML with your initials, First, Middle, Last everywhere FML appears.). Type the rest of the program as given on the next page. (The lines in a program are called "source code".) In the first cout statement, type your own name in place of “your name”.

Be careful to include semicolons ( ; ) at the ends of the lines where they appear in the program. All C/C++ statements must end with a semicolon. Notice that some lines are not considered to be regular statements and therefore don't end with a semicolon.

Include blank lines and indent lines (as shown below) to improve readability. The term endl is a newline manipulator (Note: endl ends with the letter l, not the number 1 . Note that the last character in the variables length1, width1, and num1 is the number 1 .). The { } characters are called "curly braces" and are used to form groups of C/C++ statements.

NOTE: The getch() function at the end of the program above is used to pause the output screen which otherwise would appear too briefly when you Run your program.

Save this file as FML01.CPP on your diskette A: . IMPORTANT: You will lose your work if you don’t save it on your floppy diskette A: before you start a new program or leave your PC.

Using the instructions given below, compile this program FML01.CPP from the folder E:\TEMP.

The compiler translates your C/C++ code to an intermediate form of machine code that the computer needs to prepare your program for running.

NOTE: If you have too many windows open, then select Window | Close All from the Menu Bar, and then open the file needed.

When you have made corrections so that there are no more Compile errors, then you can run your program, as shown below.

After running your program, check your output against that shown on the next page. If there are differences, then you should look for "run-time" errors in your program. Correct the errors, save again, compile again from e:\temp, and run again. Repeat this cycle until your output is correct. IMPORTANT: Be sure that you have saved the most recent version of your program on your diskette before starting a new program.

In the output shown above, notice that the average did not equal 4.5, as you might expect. In integer math the fractional part of a result is discarded (truncated). In your program, change (num1+num2)/2 to (num1+num2)/2.0 and notice the new result for the average when you compile and run again.

3. FML02.CPP - write your own program

Clear the editor window using Window | Close all | Editor and retrieve HEADING.CPP from your diskette. Change the comment // FML??.CPP to // FML02.CPP (FML = your initials ). Write a program using "cout" statements that is similar to FML01.CPP, but having an output as shown in the box below. See the next page for some start-up code (unfinished program).

To finish the program, you should add the calculations for perimeter and circumference and the appropriate cout statements to the Start-up code shown below. The perimeter of a rectangle is (2 * length + 2 * width) and the circumference of a circle is (PI * 2 * radius). Refer to program FML01.CPP for additional help.

Use the same procedure as before: type the program, save the program (save on A: first and then in e:\temp ), compile the program from e:\temp and then run the program. If there are any Compile errors, then make corrections to your source code and compile again. If there are any errors in your output when you Run, then make corrections and compile and run again.

NOTE: You can actually skip the Compile operation and just do a Debug | Run (or click the Run icon), since a Compile will automatically be done if needed. (Be sure that you have saved the program on e:\temp before selecting Debug | Run.)

Print your program FML02.CPP by selecting File from the Menu Bar and then Print . Click OK .

4. FML03.CPP - variable assignments, calculations

Make sure that you have saved your last program FML02.CPP on your floppy diskette and then clear the editor window by selecting Window | Close all | Editor from the Menu Bar. Retrieve HEADING.CPP from your diskette and change the comment // FML??.CPP to // FML03.CPP . Type the rest of the program as given below.

Don't forget to include semicolons (;) at the ends of the statements where they are shown.

//

// your name

// CPET 1104 your section

// today's date

// FML03.CPP

//

// This program calculates total and average weight.

// Weight values are assigned within the program.

//

#include <iostream.h> //header file for I/O functions

#include <conio.h> //used for the getch() function

void main()

{ float weight1, weight2, weight3, total, average;

cout < "your name, output for FML03.CPP" < endl < endl;

more…

weight1 = 10.0;

weight2 = 15.0;

weight3 = 20.0;

total = weight1 + weight2 + weight3;

cout < "The total weight is " < total < endl;

cout < endl < endl; // prints two blank lines

average = total/3;

cout < "The average weight is " < average < endl;

cout < endl < endl; // prints two blank lines

getch(); //used to pause the output window

} //End of main()

Save this file as FML03.CPP on your diskette A: and then in the folder e:\temp before you Compile.

Before (or after) you run a program you should do hand calculations to determine if the program gives the correct results. In the box below, notice how the hand calculations for FML03.CPP have been done, and also notice in the box on the next page that the program’s output is shown after doing a Run .

If the hand calculations don't agree with the results appearing in the output window, then the program should be checked carefully for errors (This assumes that your hand calculations have been done correctly!).

A common error is to "spell" a variable name in two different ways. For example, in the program FML03.CPP, if you used weight1 in one place and then wieght1 in another part of the program, then the Compler will issue an error message about this.

If the program FML03.CPP has any run-time errors (i.e., the hand calculations don't agree with the results on the output window), then correct the errors and save your program again. Don't change the filename when you save - use FML03.CPP again. Then compile and run your program once more. Continue this cycle of running, looking for errors, correcting errors, saving, compiling and running again until the program is correct.

5. FML04.CPP - write your own program

Write your own program using the problem statement below. Follow the same format as given for the previous program FML03.CPP .

Problem statement: Three similar cars were tested for their miles per gallon performance. The results were as follows (in miles per gallon): Car 1 - 23.4, Car 2 - 26.1, Car 3 - 20.9 . Write a program that will have an output similar to what is shown below.

Use the same sequence of operations as before: type the program, save the program (as FML04.CPP on A: and then in e:\temp ), compile the program from e:\temp, run the program, do hand calculations, correct any errors, and repeat the cycle until the program runs correctly.

In your program, use descriptive variable names like mpg for miles per gallon. Spaces are not allowed in variable names, but underscores can be used, such as, mpg_car_1 - refer to the start-up code for FML04.CPP on the next page to see some possible variable names for this program.

To help you get started, on the next page there is some start-up code that provides an outline of the program that you are to write. NOTE: As you develop your program, you will need to include some additional variables in the float statement.