Get Adobe Flash player

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;
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
---------------------------------------------------------------------------------------------------------------------------------------

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Featured Posts


 

My Mobile Site

QR Code - scan to visit our mobile site

This is a 2D-barcode containing the address of our mobile site.If your mobile has a barcode reader, simply snap this bar code with the camera and launch the site.

Say something:

  • oyeng
    email
    wata fuck smart!!!!
    5 February 2012
  • yea
    email
    ewan ko bah, alam nyo mga company,mayayaman na kayo dahil sa mga subscribers,kaya sana naman eh maging fair kayo.. oo tama unlicall 25 pesos lang,pero kunsumisyon bago maga register., dahil iyon sa pag iipit nyo.,para gamitin nalang ang regular load namin, tama? buti pa reg. load ang BILIS!., pero ang bilis din ng load namin maubos!! make any sense?
    31 January 2012
  • russel
    email
    ano b yan? unli call and text 25... one day na unable to connect?!!!!!! kalokohan.. more incom sa smart.. hirap kumontak pag sa unli ggmitin mong call pero pag sa regular load deredretso lng.. ampf tlga
    30 January 2012
  • jhel
    email
    wla n bng unli25 ung unlicall & txt??
    27 January 2012
  • christine.barnal@yahoo.com
    email
    grabe nakakaloka wag na kayo mag benta ng lod di naman magmit laging busy at nakakabagot pa dhil sa di makareg hay nku nais kong sabihin sa company ng samart na bulok ang mga promos ninyo
    23 January 2012
  • mj lo
    email
    may unli call promo po kau sa other network?
    21 January 2012
  • alycha
    email
    pano po unli call to all networks?
    19 January 2012
  • trey
    email
    GREAT DEALS IN TOWN
    12 January 2012
  • Sheila Vinoya
    email
    hirap makatawag ng unlicall at text d naman magamit ang tawag!!!!
    10 January 2012
  • asdf
    email
    hello
    7 January 2012
Your Shout
weekly trend micro give aways