Download presentation
Presentation is loading. Please wait.
1
Chapter 2 Elementary Programming
2
Motivations In the preceding chapter, you learned how to create, compile, and run a program. Starting from this chapter, you will learn how to solve practical problems programmatically. Through these problems, you will learn primitive data types and related subjects, such as variables, constants, data types, operators, expressions, and input and output.
3
Objectives To write C++ programs to perform simple calculations (§2.2). To read input from the keyboard (§2.3). To use identifiers to name elements in the program (§2.4). To use variables to store data (§§ ). To program with assignment statements and assignment expressions (§2.6). To name constants using the const keyword and #define directive (§2.7). To declare variables using numeric data types (§2.8). To use operators to write numeric expressions (§2.8). To convert numbers to a different type using casting (§2.9). To represent character using the char type (§2.10). To become familiar with C++ documentation, programming style, and naming conventions (§2.12). To distinguish syntax errors, runtime errors, and logic errors (§2.13). To debug logic errors (§2.14).
4
Introducing Programming with an Example
Listing 2.1 Computing the Area of a Circle This program computes the area of the circle. ComputeArea Run
5
Trace a Program Execution
animation Trace a Program Execution allocate memory for radius #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area cout << "The area is "; cout << area << endl; } radius no value
6
Trace a Program Execution
animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius no value area no value allocate memory for area
7
Trace a Program Execution
animation Trace a Program Execution assign 20 to radius #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } radius 20 area no value
8
Trace a Program Execution
animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius 20 area compute area and assign it to variable area
9
Trace a Program Execution
animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * ; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius 20 area print a message to the console
10
Reading Input from the Keyboard
You can use the cin object to read input from the keyboard. #include <iostream> int main() { // Step 1: Read in radius double radius; std::cout << "Enter a radius: "; std::cin >> radius; // Step 2: Compute area double area = radius * radius * ; // Step 3: Display the area std::cout << "The area is " << area << std::endl; system("PAUSE"); return 0; } ComputeArea1.cpp
11
Reading Multiple Input in One Statement
Example : Compute average of three numbers. #include <iostream> int main() { double x1,x2,x3, average; std::cin>> x1>> x2>> x3; average=(x1+x2+x3)/3; std::cout << "The average is " << average << std::endl; system("PAUSE"); return 0; }
12
Identifiers An identifier is a sequence of characters that consists of letters, digits, and underscores (_). An identifier must start with a letter or an underscore. It cannot start with a digit. An identifier cannot be a reserved word. (See Appendix A, “C++ Keywords,” for a list of reserved words.) An identifier can be of any length, but your C++ compiler may impose some restriction. Use identifiers of 31 characters or fewer to ensure portability. For example: area and radius are legal identifiers, whereas 2A and d+4 are illegal identifiers. (compiler reports syntax errors).
13
Variables // Compute the first area radius = 1.0;
area = radius * radius * ; cout << area; // Compute the second area radius = 2.0;
14
Declaring Variables int x; // Declare x to be an // integer variable;
double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; Variables of the same type can be declared together: Example: int i,j,k; //declare i,j,and k as int variables.
15
Assignment Statements
x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; x=5*(3/2)+2 //Assign the value of the expression to x. x=y // assign the addition of y and 1 to x. x=x //the result of x+1 is assigned to x. If x is 1 before the statement is executed, then it becomes 2 after the statement is executed.
16
Declaring and Initializing in One Step
int x = 1; This is equivalent to: int x; x=1; double d = 1.4; int i=1, j=2; int i(1), j(2);
17
Named Constants const datatype CONSTANTNAME = VALUE;
const double PI = ; const int SIZE = 3;
18
Named Constants ComputeArea4.cpp #include <iostream>
using namespace std; int main() { const double PI = ; // Step 1: Read in radius double radius = 20; // Step 2: Compute area double area = radius * radius * PI; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; system("PAUSE"); return 0; } ComputeArea4.cpp
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.