Monday, August 16, 2010

Programming With C++ Language

Hmmm... what should I say yeh??? Actually, these explanation I took/copied from http://www.cprogramming.com/tutorial/lesson1.html.

From this site, " a C++ program is a collection of commands, which tell the computer to do "something". This collection of commands is usually called C++ source code, source code or just code. Commands are either "functions" or "keywords". Keywords are a basic building block of the language, while functions are, in fact, usually written in terms of simpler functions.

Think of it a bit like an outline for a book; the outline might show every chapter in the book; each chapter might have its own outline, composed of sections. Each section might have its own outline, or it might have all of the details written up.) Thankfully, C++ provides a great many common functions and keywords that you can use.

But how does a program actually start? Every program in C++ has one function, always named main, that is always called when your program first executes. From main, you can also call other functions whether they are written by us or, as mentioned earlier, provided by the compiler.

So how do you get access to those prewritten functions? To access those standard functions that comes with the compiler, you include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. "

Hahahahaha..sorry for just putting down some copied resources... By the way, just look at the example below. I have noted a simple explanation at the left side of the program.

Eg1. The Example Program of HelloWorld.cpp

The explanation. Please refer to the program code that is written in a yellow box. I love to refer this program source codes that's using Microsoft Visual C++ Compiler. In this compiler, we can see the differences by looking at difference colour of statement.

Comment. Usually we can recognize comment in green. Actually there are two types of comments in Microsoft Visual C++. Comment for a single line or comment for multilines.
If we want to write comment for a single line, we just start with  // at the beginning of the comment line.For examples
            
            //include this file for cout
            //Filename: HelloWorld.cpp

Got it? ok now, let's look at a comment for multiple lines. If there is so, we use the /* symbol start the comment and we end the comment with */. Looks easy?. Look at the example

           /*My first program in this
             basic programming course is start with Hello World!*/

Have you seen the difference??

Ok next syntax is a preprocessor directive. We always used #include to write a command for preprocessor directive (in blue colour). This command allowed us to embed some library files or header files into our own program or else, we actually call for another program files that recognized as a header files into our own file.  As an example

          #include<iostream.h>
          #include<math.h>

Next is a main function. For every C++ program there will be only one  main function. main is a name for a function and from a main function, we can call another function that written by us. 

Then, we look at the body of a function. We will start the block function with { symbol and at the end of the block we should end with } symbol. (I call these { and } symbol as a curly braces). So, our program coding will be written between these two braces.

So, this is what C++ all about... It just a beginning. There will be more syntax afterwards in next entries.

Steps in Programming

There are 6 steps in programming

  1. Defining and analyzing problems
    • Programming begin with a specification of problems.
    • This steps is to identify and understand what are the problems to resolve.
    • The problems must be clearly define, explicit and the requirements in resolving it.
    • Analyzing the problems will determine the input, output and information required to solve these problems, as follows:
      • input – data to be processed
      • output – the desired result
      • the constraint and additional features in resolving the problems
  2. Planning of variables
    • Variables are simply references to memory locations.
    • A well plan use of variables will produce an efficient execution of program in terms of speed and memory utilization.
  3. Drawing the flowchart
    • Flowchart represents algorithm in graphic form comprising of geometrical symbols which is interrelated by line of flow.
  4. Program writing
    • In the design of program it should be written as simple as possible.
    • The main objective is to give a clear, readable programs through an orderly and disciplined approach to programming.
  5. Testing and debugging program
    • Once the program has been written it must be compiled and executed.
    • This is accomplished by an editor and compiler.
    • An editor lets us type a program, makes changes and save it to a file.
    • The compiler then translates the program into a form that the computer can read.
    • Once the program has been compiled and executed the presence of errors will be readily apparent.
    •  There two types of errors
      • Syntactic and execution errors
        • usually result in the generation of error when compiling or executing a program.
        • Error of this type is usually quite easy to find and correct.
      • Logical errors
        • Since the output resulting from logically incorrect program may appear to be error free.
        • Thus a good bit of probing may be required which is known as logical debugging
  6. Documentation of program
    • Program must be documented for future references and maintenance process.
    • A well documented program will make it easier for the original programmer or other programmer to follow the program logic and design.
    • Program document should consist of :
      • An accurate specification of requirement
      • Detail input, output, constraint and formula for the above problems
      • Algorithm in the form of flowchart or pseudo code
      • Program source complete with comment
      • Sample program which had been run and executed and the tested data.
      • Guideline on how to use the program.

Components of Program Structure

There are 6 components of program structure, declarations, Input, Storage, Operation, Control and Output.

A declarations associates a group of variables with a specific data type. All variables must be declared before they can appear in executable statements. E.g

     int height, weight;
     float area;
     char choice, name[25];

Input is a set of information called data that will be entered into the computer from keyboard, floppy disk, hard disk etc. and stored in a portion of the computer memory. This input which is an input to the computer will then be processed to produced the desired result.

Storage refers to every piece of information that are stored within the computer’s memory which is encoded as some unique combination of zeros and ones. Computers also employ auxiliary storage e.g. disks in addition to their primary memories which allowed information to be stored permanently and can be physically disconnected when not in use.

Operation is how the program will run. There are two ways of Operation :


i. Batch mode
The program and the data are typed into the computer and stored within computer’s memory and processed in its proper sequence. Large quantities of information can be transmitted into and out of the computer without the user present while the job being processed. Batch processing can be undesirable for simple jobs.

ii. Interactive mode
The user and the computer are able to interact with each other during computational session.

Control refers to the order of execution of instructions in a program. The instruction can be executed
     i. sequentially – one by one
     ii. from top downwards or
     iii. non sequential execution

Most real life problems require some kind of decision making to take a certain course of action. This means that instruction or a whole block of instructions can be executed, repeated or skipped.

Output refers to the processed data which produced certain result. The output data will be presented in a sheet of paper through the printer or display on a monitor.

Introduction to the Programming Concept

As usual, before we start doing our program, we have to know the concepts exist in programming. We have to know what is program, programming and programmer.

So Program is a set of instruction that tell computer what to do. Means, the instruction will allow you to control your computer, making it do what you want it to do. In another words (much more complete definition), the program is written as a series of  human understandable computer instructions that can be read by a compiler and linker, and translated into machine code so that a computer can understand and run it.

Then, what  is a programmer? A programmer is a person who is writing codes or instructions to the computer to make computer do what sould it do.. :-).... I call this process as a computer programming. Not yet understand what a is meaning of programming? Hmmm...

Ok, the computer programming actually is a process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language. The code may be a modification of an existing source or something completely new. Then, the purpose of programming is to create a program that exhibits a certain desired behavior. Hmmm.. looks easy hmm???

Programming languages can be used to create programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication. C++ is a programming language. It needs a compiler to make computer understand what programmers wrote. A compiler turns the program that you write into an executable that your computer can actually understand and run. So in doing C++ programming, you need C++ compiler such as Microsoft Visual C++, Borland C++ or else.

Regarding to http://www.cprogramming.com/tutorial/lesson1.html , Each of these compilers is slightly different. Each one should support the ANSI/ISO standard C++ functions, but each compiler will also have nonstandard functions (these functions are similar to slang spoken in different parts of a country). Sometimes the use of nonstandard functions will cause problems when you attempt to compile source code (the actual C++ written by a programmer and saved as a text file) with a different compiler. These tutorials use ANSI/ISO standard C++ and should not suffer from this problem (with sufficiently modern compilers). Note that if you are using an older compiler, such as TCLite, you should read check out some compatability issues.


Lets leave about C++ compiler for a while, we look at the basic concepts of programming first. I just cover for some concepts when we are doing programming. These are integrity, clarity, simplicity, effiiciency, modularity and generality.

Integrity is the accuracy of the calculations since it will be meaningless if calculations are not carried out correctly.

Clarity means the overall readability of the program, with particular emphasis on its underlying logic. If a program is clearly written, it should be possible for programmer to follow the program logic with ease.

Simplicity refers to the clarity and accuracy of a program are usually enhanced by keeping things as simple as possible, consistent with the overall program objectives.

Efficiency is concerned with execution speed and efficient memory utilization.

Modularity refers to breaking down the program into a series of identifiable subtask that enhances accuracy and clarity of a program and facilitates future program alterations and
 
Generality is to make a program to be as general as possible, within reasonable limits. A considerable amount of generality can be obtained with very little additional programming effort.
 
Ok, enough for this entry. Next issue, I will write about the components of program structure and the steps in doing programming..

Welome to My Blog

Assalamualaikum and good day..

Actually this is my second time to be a blogger. :-)..I'm not good in programming either (am I??)  but well, in this blog, I just want to share a little bit of knowledge in programming especially in Visual C++. It just a simple notes in doing programming or actually I just grab :-)  it from my lecture notes in Module E3062.. So, to my students especially from my polytectnic (Polisas), lets we share our knowledge together, do correct the mistakes (either it's come from my side or yours) because this is another way for us to learn.... So, let us start!