Basic Guide in Programming
Part I. Introduction
How to Solve a Problem?
Programs often are written in response to some problems or task to be solved. For example, a bookstore enters into a file the title and publisher of each book it sells. The information entered in the order the books are sold. Every tweo weeks the owner computes by hand the number of copies of each title sold and the number sold from each publisher. The list is alphabetized by publisher and used for purposes of reordering. We have been asked to supply a program to do this work.
One method of solving a big problem is to break it down into a number of smaller problems. Ideallym these smaller problems are easier to solve and, taken together, solve our big problem. If our newly divided smaller problems are still too big to solve, we in turn break these problem into still smaller problems, continuing the process until, hopefully, we have a solution to each subdivided problem. This strategy is spoken of, variously, as divide and conquer and stepwise refinement. Our bookstore problem divides nicely into four subproblems, or task:
Tasks:
1. Read the sales file.
2. Count the sales by title and by publisher.
3. Sort the titles by publisher.
4. Write the results.
Tasks 1, 2, and 4 represents problems we know how to solve; they do not need to be broken down further. Item 3, howeverm is still a little more than we know how to do. So, we reapply our method to this item:
3.a Sort the sales by publisher.
3.b Within each publisher, sort the sales by title.
3.c Compare adjacent titles within each publisher group. For each matching pair, increment an occurence count of the first and delete the second.
Items 3a, 3b, and 3c also now represent problems that we know how to solve. Because we can solve all the subproblems that we have identified, we have in effect soved the original, bigger problem. Moreover, we see that the original order of tasks was incorrect. The sequence of actions required is the following:
1. Read the sales file.
2. Sort the sales file – first by publisher and then by title within the publisher.
3. Compact duplicate titles.
4. Write the result into a new file.
The resulting sequence of actions is referred to as an algorithm. The next step is to translate our algorithm into a particular programming language.
Using C++
In C++, an action is referred to as an expression. An expression terminated by a semicolon is referred to as a statement. The smallest independent unit in a C++ program is a statement. In a natural language, an analogous construct is the sentence.
Example of Statements in C++:
int book_count = 0;
book_count = books_on_shelf + books_on_order;
cout << “the value of book_count: ” << book_count;
cout << “the value of book_count: ” << book_count;
The first statement is a declaration statement. book_count is variously called an identifier, a symbolic variable (or variable for short) or an object. It defines an area of computer memory associated with the name book_count that holds integer values. 0 is a literal constant. book_count is initialized to a first value of zero.
The second statement is an assignment statement. It places in the are of computer memory associated with book-count the result of adding together books_on_shelf and books_on_order. Presumably, these are also integer variables defined and assigned values in earlier portions of the program.
The third statement is an output statement. cout is the output destination associated with the user’s terminal <<> string literal enclosed within quotation marks and then the value stored in the area of computer memory associated with the name book_count.
Statements are logically grouped into named units referred to as functions. For example, all the statements necessary to read the sales file are organized into a function called readIn(). Similarly, we organize sort(), compact(), and print() functions.
In C++, every program must contain a function called man(), supplied by the programmer, before the program can be run. Here is how main() might be defined for the preceding algorithm:
int main()
{
readIn();
sort();
compact();
print()
return 0;
}
A C++ program begins execution with the first statement of main(). In this case, the program begins by executing the function readIn(). Program execution continues by sequentially executing the statements within main(). the program terminates normally following execution of the last statement in main().
A function consists of four parts: a return type, the function name, a parameter list, and the function body. the first three parts are collectively referred to as the function prototype.
The parameter list, enclosed within parentheses, contains a comma-separated list of zero or more parameters. The function body is enclosed within a pair of curly braces. It consists of a sequence of program statements.
In this instance, the body of main() invokes the functions readIn(), sort(), compact(), and print(). When they have completed, the statement return 0 is executed. return 0, a predefined C++ statement, provides a method of terminating the execution of a function. When supplied with a value such as 0, that value becomes the return value of the function. In this case, a return value of 0 indicates the successful completion of main().
Lets turn now to how the program is made ready for execution. First, we must provide definitions of readIn(), sor(), compact(), and print(). At this point, the following dummy instances are good enough:
void readIn() { cout << “readIn()n”; }
void sort() { cout << “sort()n”; }
void compact() { cout << “compact()n”; }
void print() { cout << “print()n”; }
Note: the above codes are dummies only!
void is used to specify a function that does not provide a return value.
This incremental method of building programs provides a useful measure of control over the programming errors we inevitably make. Trying to get a program to work all at onece is simply too complicated and confusing.
#include
using namespace std;
iostream is the iostream library standard header file (note that is has no suffix). It contains information about cout that is necessary to our program. #include is a preprocessor directive. It causes the contents of iostream to be read into our text file.
The names defined in the C++ standard library, such as the name cout, cannot be used in our program unless we follow the
#include
preprocessor directive with the statement
using namespace std;
This statement is called a using directive. The names in the C++ standard library are declared in a namespace called namespace std and are not visible in our program text file unless we explicitly make them visible. The using directive tells the compiler to use the library names declared in namespace std.
Onces the program has been entered into a file, the next step is to compile it. The command name used to invoke the C++ compiler varies across implementations.
Part of the compiler’s job is to analyze the program text for correctness. A compiler cannot detect whether the meaning of a program is correct, but it can detect errors in the form of the program.
Two common form s of program error:
1. Syntax error – The programmer has made a “grammatical” error in the C++ language. For example:
int main ( { // error: missing ‘)’
readIn(): // error: illegal character ‘:’
return 0 // error: missing ‘:’
2. Type errors. Each item of data in C++ has an associated type. The value 10 for example, is an integer. The word “hello” surrounded by double quotation marks is a string. If a function expecting an integer argument is given a string a type error is signaled by the compiler.
It is a good practice to to correct errors in the sequence they are reported. Often a single error can have a cascading effect and cause a compiler to report more errors than actually are present. One the error has been corrected, the program should be recompiled. This cycle is often referred to as edit-compile-debug.
The second part of the compilers job is to translate formally correct program text . This translation, referred to as code generation , typically generates object or assembly instruction text understood by the computer on which the program is run.
The result of a successful compilation is an executable file.
– End of Part I.
– Part II will discuss about if statements (conditions), looping, comments, input/output and arrays.
Source: C++ Primer by Lippman & Lajoie
If you find this page useful give it a
---------------------------------------------------------------------------------------------------------------------------------------






