Tuesday, November 11, 2014

Getting Started

1. Problem Solving

Programs often are written in response to some problem or task to be solved. Let's look at an example. A bookstore enters into a file the title and publisher of each book it sells. The information is entered in the order the books are sold. Every two weeks the owner by hand computes 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. You 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. Hopefully, these smaller problems are easier to solve. Our bookstore problem divides nicely into four sub-problems, or tasks:
1.1 Read in the sales file.
1.2 Count the sales by title and by publisher.
1.3 Sort the titles by publisher.
1.4 Write out the results.

Items 1.1, 1.2 and 1.4 represent problems we know how to solve; they do not need to be broken down further. Item 1.3, however, is still a little more than we know how to do. So we reapply our method to this item:
1.3.1 Sort the sales by publisher.
1.3.2 Within each publisher, sort the sales by title.
1.3.3 Compare adjacent title within each publisher group. For each matching pair, increment an occurrence count of the first and delete the second.

Items 1.3.1, 1.3.2 and 1.3.3 also now represent problems that we know how to solve. Since we can solve all the sub-problems that we have generated, we have in effect solved the original, bigger problem, Moreover, we see that the original order of tasks was incorrect. The sequence of actions required following:

a. Read in the sales file.
b. Sort the sales file -- first, by publisher, then, by title within publisher.
c. Compact duplicate titles.
d. Write out the results 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 in this case, C++.

2. The C++ Program
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 until in a C++ Program is a statement. In natural language, an analogous construct ix the sentence. The following, for example, are statements in C++;

         int value;
      value = 7 + 1;
      cout << value;

The first statement is referred to as a declaration statement. It defines an area of computer memory associated with the name value that can integer values. The second statement is referred to as an assignment statement. It places in the area of computer memory associated with the value the result of adding 7 and 1. The third statement is an output statement. cout is the output destination associated with the user's terminal. << is the output operator. The statement writes to cout -- that is, the user's terminal, the value stored in the area of computer memory associated with the name value.

Statements are logically grouped into named units referred to as functions. For example, all the statements necessary to read in the sales file are organized into a function called readIn(). Similarly, we organize sort(), compact() and print() function.

In C++ , every program must contain a function called main(), 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(). A program terminates normally following execution of the last statement of main().

A function consists of four parts: a return type, the function name, an argument list, and the function body. The first three parts are collectively referred to as the function prototype. The argument list, enclosed within parentheses, contains a comma-separated list of zero or more arguments. The function body is enclosed within a pair of braces ("{}"). It consists of a sequence of program statements.

In this instance, the body of main() calls for execution the functions readIn(), sort(), compart() and print(). When these have completed, the statement return 0; is executed. return, 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().

Let's turn now to how the program is made ready for execution. First we must provide definitions of readIn(), sort(), compact(), and print(). At this point, the following dummy instance are good enough.

    void readIn() { cout << "readIn()\n";}
    void sort() { cout << "sort()"\n";}
    void compact() { cout << "compact()"\n;}
    void print() { cout << "print()"\n";}

void is used to specify a function that does not provide a return value. As defined, each function will simply announce its presence on the user's terminal when invoked by main(). Later, we can replace these dummy instance with the actual functions as they are implemented. This sort of 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 once is simply too complicated and confusing.

I will continue on my next blog soon....


 

No comments:

Post a Comment