Site hosted by Angelfire.com: Build your free website today!

 

Why Use Programming Language VS Spoken Language

We use programming languages for one reason: Programming languages are not ambiguous. English, which is the most widely used language (at least for business purposes) is ambiguous -one word many have multiple meanings. If the compiler used English as its language, there would be problems in the compiling process due to the ambiguity. For example the word "Light" has multiple meanings:

  1. Light as in the opposite of heavy.
  2. Light as in the opposite of dark
  3. Light as in "Turn on the light/lamp"
  4. Light as in to touch something gently

Let us assume that we did not have programming languages such as C++, instead there was conventional English. If you typed the term "light" and clicked on a button to compile your programme, the compiler would stumble upon the term "light" and would not know what you are referring to. The compiler would not know if you are referring to light as in not heavy or light as in the opposite of dark, etc and would consequently produce garbage for the output or perhaps even crash.

FUNDAMENTALS:

 

 

You may think "English is ambiguous, so why not use Chinese or French". The same answer is applicable here: they contain ambiguous terms as well. The only way to remove the ambiguity is to create a specific programming language in which the compiler knows that there is specifically one and only one meaning to the words used.

C++, as you know is a programming language. Your computer also has a language called machine language. The compiler is a tool for extracting the C++ language and converting it to machine language for your programme to run. You may ask yourself: "Why can't my computer language be C++?". There is such a multitude of programing languages that making the computer run C++ would be inconvienient. The same applies to any other language such as PERL. PERL comes with a compiler which converts the PERL language to machine language in order for your PERL programme to run.

Programming Procedures

This section applies to all programming languages, not just C++. When someone such as your professor gives you an assignment to complete, you cannot immediately go to your computer and start typing code. Actually you can do that, but it is not considered wise. The first step is to analyse the problem given to you by your instructor. Let's assume your professor told you to add 10 numbers and then multiply the 10 numbers. Your first step which consists of analysing the problem would consist of you asking yourself what exactly your professor wants.

1. Analyze the Problem

Questions include:

  1. What is the problem I must solve? ***MOST IMPORTANT STEP!!!!!***
  2. What is the input required? Is it from a user or from a file?
  3. What is the output required? Is it to a file or to the screen?

Why did I highlight question number one as being the most important step? The answer is because if you don't know or understand what the problem is then you cannot even hope to solve it!! If you do not understand the problem, DON'T EVEN THINK OF GOING FURTHER!! You will only be wasting your time!!

2. Design a Solution

This is the section where, after you have fully understood the problem/task, you design an algorithm to accomplish this task. You have the freedom to solve any algorithm the way you or your professor/ boss etc feels necessary. In this step you would ask yourself if a specific algorithm would be better solved using pointers or using a stack vs a queue, or how many functions your programme should consist of, etc. Let us assume your instructor wanted you to accept the 10 numbers from the user and print the sum and product to the screen. For this example, you would state "In order to add 10 numbers, I would have to divide this task into subtasks and tackle each one like so:

  1. Accept 10 numbers from user
  2. Add the 10 numbers
  3. Print the sum of the 10 numbers to the screen
  4. Multiply the 10 numbers
  5. Print the product of the 10 numbers to the screen

3. Code your Algorithm

As stated, this is where you sit down and code your algorithm. This code will be explained in this section and next section, so don't worry.

FIGURE 2.1

#include<iostream.h>
int main(){    int num1 = 0;    int count = 0;    int product = 0;    int sum = 0;
    cout  <<  "\nEnter a number and press return. After you enter"            <<  "\n10 numbers I will print their sum.";
   do     {        cout  <<  "\nEnter a number: ";        cin >> num1;        sum = sum + num1;        count++;     }while (count <= 9);
    cout  << endl <<  "The sum of the 10 numbers is: " << sum;
   num1 = 0;   count = 0;
   cout  <<  endl  << "Enter a number and press return. After you enter "           << endl  <<  "10 numbers I will print their product.";
   do     {        cout  <<  "\nEnter a number: ";        cin >> num1;
        if (count == 0)        {           product = num1;        }        else        {           product = product * num1;        }        count++;      }while (count <= 9);
     cout  << endl <<  "The product of the 10 numbers is: " << product;
    return(0);}

4. Test your Code

As stated, this is where you test your code to ensure it works properly and accomplish the specific task desired. If your code does not work, perhaps your algorighm was incorrect or you made a mistake in your code.

Programming Format

When programming in C++, as with any language, a specific format must be followed. The same is with writing a letter or resume-- a specific format must be used. You do not have to follow the specific format, however following a standard format makes the code easier to read for others as well as yourself. Try to keep the curved brackets { } aligned vertically. Keep spacing consistant. Always document your code neatly so that if you leave your programme for x amount of time, you can understand what you were doing once you return back to your code by the comments made. Keep the comments aligned and neat as well. Always end each statement with a semicolon ";"

The general format for C++ is to:

  1. Place directives at the top
  2. Function prototypes below (if any)
  3. Int main() (Body of Programme)
  4. Place return() statement at end of main()
  5. Declare variables at top function definitions

Notice below that the spacing is consistant and the brackets are aligned creating code that does not look confusing. Do not worry about what the code means, it will be explained as we go along.

FIGURE 2.2

#include<iostream.h>insert directives in this area
int main(){    insert variable declarations here;    int num2 = 32767;    int num3 = 327657;    float num4 = 327657;
    cout  <<  "\nNum1 is: " << num1;
    return(0);}

To illustrate the point of following a format, take a look at the code below:

FIGURE 2.3

#include<iostream.h>
int main(){    int num1 = 50;    int num2 = 32767;    int num3 = 327657;    float num4 = 327657;
    cout  <<  "\nNum1 is: " << num1;     cout  <<  "\nNum2 is: " << num2;     cout  <<  "\nNum3 is: " << num3;     cout  <<  "\nNum4 is: " << num4;
    return(0);}

Now look at the code below in Figure 2.4. Notice that the code below is the same as the code above, however it looks much neater and is easier to understand and follow.

FIGURE 2.4

#include<iostream.h>
int main(){    int num1 = 50;    int num2 = 32767;    int num3 = 327657;    float num4 = 327657;
    cout  <<  "\nNum1 is: " << num1;    cout  <<  "\nNum2 is: " << num2;    cout  <<  "\nNum3 is: " << num3;    cout  <<  "\nNum4 is: " << num4;
    return(0);}

Programming Statements

There are 6 different types of statements in C++:

TABLE 2.1

STATEMENT PURPOSE EXAMPLE
OUTPUT STATEMENT to send output to the screen or file cout <<
INPUT STATEMENT to accept input from the user or file cin >>
ASSIGNMENT STATEMENT assigns a value to a data type int answer = 22 + 2
CONDITIONAL STATEMENT to create conditions (if then else) see section on loops
LOOPING STATEMENT for, do while statements (repeats operations) see section on loops
FUNCTION CALL call to a function for a specific operation see section on functions


Directives

The #include statements followed by iostream.h, cstring.h, etc are called header files, the whole statement is called a directive. Directives tell the compiler which files to use in order to run your program. Reserved words and built in functions are defined in the header files. When you type cout, cin, etc in your programme, the compiler looks to see if they are defined. The #include statements tell the compiler "I would like to use cout, cin, int, char, etc in my program, therefore, I have included the files which define those reserved words."

It is the same thing as baking a cake. In order to bake a cake, you need eggs, flour, milk, sugar, etc. If you do not include one of the basic ingredients such as flour, the cake will be a disaster. You do not know very much about the process of making bleached flour, or the biochemical reactions which take place for the milk to be produced in the cow, however you know that it must be included in your cake. The same applies to the header files. You do not know what is in the header file or how it defines the reserved words (you will learn as you go on), but you know that without the files your programme will not work correctly.

Another example would be your car. You know that in order for your car to run, the oil must be changed, there must be gas in the car, the engine belt must be changed, the valves must be running properly, etc etc. You do not know how to change the oil or the belt. All you know is that in order for the car to run, the mechanic needs to change the oil.

Int Main() and Return()

You may have noticed the int main() inclosed in curved brackets { }part of the programme. It means exactly what it states "This is the main part of the programme and it starts right here". The main part of the programme ends when the compiler reads the return(0) statement. It basically states "Compiler, you have now reached the end of the main part of the programme. Stop here."

Output Statements

An output statement is denoted by: cout  << . It is read as "see out". It tells the compiler to print the output to the screen. For example, if you wanted to type out the statement "I am learning C++", it would be as follows:

FIGURE 2.5

#include<iostream.h>
int main(){    cout  <<  "\nHi! I am learning C++.";
    return(0);}

Input Statements

An input statement is used to denote what the programme will accept from the user, you are typing input to the programme. It is denoted by "cin >>" read as "see in" Let's say you type a programme asking how old some one is and the compiler prints it out:

FIGURE 2.6

#include<iostream.h>
int main(){    int age;
    cout  <<  "\nHi! What is your age? Type your age then press return.";    cin  >>  age;    cout  <<  "\nYou are "  <<  age  << "years young!";
    return(0);}

 

 

 

Data Types Strings I/OStream Structures Classes Operations Memory Branches Loops Functions Pointers Statements Compiler ADT Arrays Linked Lists Message Board Search Email Home

 
Site Credits| Questions and Enquiries | © 2001 | Contact