Elementary Programming in C++: Writing a Simple Program

Writing a program involves designing a strategy for solving a problem and then using a programming language to implement that strategy.

First, let’s consider the simple problem of computing the area of a circle. How do we write a program for solving this?

Writing a program involves designing algorithms and translating them into program-ming instructions, or code. An algorithm describes how a problem is solved by listing the actions that must be taken and the order of their execution. Algorithms can help the programmer plan a program before writing it in a programming language. Algorithms can be described in natural languages or in pseudocode (natural language mixed with some programming code). The algorithm for calculating the area of a circle can be described as follows:

1.   Read in the circle’s radius.

2.   Compute the area using the following formula:

area = radius X radius X π

3.   Display the result.

When you code—that is, when you write a program—you translate an algorithm into a pro­gram. You already know that every C++ program begins its execution from the main function. Here, the outline of the main function would look like this:

int main()

{

// Step 1: Read in radius

// Step 2: Compute area

// Step 3: Display the area

}

 

The program needs to read the radius entered by the user from the keyboard. This raises two important issues:

  • Reading the radius
  • Storing the radius in the program

Let’s address the second issue first. In order to store the radius, the program needs to declare a symbol called a variable. A variable represents a value stored in the computer’s memory.

Rather than using x and y as variable names, choose descriptive names: in this case, radius for radius, and area for area. To let the compiler know what radius and area are, specify their data types. That is the kind of the data stored in a variable, whether integer, floating-point number, or something else. This is known as declaring variables. C++ provides simple data types for representing integers, floating-point numbers (i.e., numbers with a deci­mal point), characters, and Boolean types. These types are known as primitive data types or fundamental types.

Declare radius and area as double-precision floating-point numbers. The program can be expanded as follows:

int main()

{

double radius;

double area;

// Step 1: Read in radius

// Step 2: Compute area

// Step 3: Display the area

}

The program declares radius and area as variables. The reserved word double indicates that radius and area are double-precision floating-point values stored in the computer.

The first step is to prompt the user to designate the circle’s radius. You will learn how to prompt the user for information shortly. For now, to learn how variables work, you can assign a fixed value to radius in the program as you write the code; later, you will modify the pro­gram to prompt the user for this value.

The second step is to compute area by assigning the result of the expression radius * radius * 3.14159 to area.

In the final step, the program will display the value of area on the console by using cout << area.

The complete program is shown in Listing 2.1.

Listing 2.1 ComputeArea.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    double radius;

7    double area;

8

9    // Step 1: Read in radius

10    radius = 20;

11

12    // Step 2: Compute area

13    area = radius * radius * 3.14159;

14

15    // Step 3: Display the area

16    cout << “The area is ” << area << endl;

17

18    return 0;

19 }

Variables such as radius and area correspond to memory locations. Every variable has a name, type, size, and value. Line 6 declares that radius can store a double value. The value is not defined until you assign a value. Line 10 assigns 20 into radius. Similarly, line 7 declares variable area and line 13 assigns a value into area. If you comment out line 10, the program will compile and run, but the result is unpredictable, because radius is not assigned a proper value. In Visual C++, referencing an uninitialized variable will cause a runtime error. The table below shows the value in the memory for area and radius when the program is executed. Each row in the table shows the new values of variables after the statement in the corresponding line in the program is executed. This method of reviewing how a program works is called tracing a program. Tracing programs can help you understand how programs work and find program errors.

Line 16 sends a string “The area is ” to the console. It also sends the value in variable area to the console. Note that quotation marks are not placed around area. If they were, the string “area” would be sent to the console.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *