Showing posts with label Programming Basics. Show all posts
Showing posts with label Programming Basics. Show all posts

Tuesday, August 11, 2009

Basic data types

Computer programs produces results by manipulating data. A programming language must have a way of representing and handling data. For this reason there are:
- Basic data types for representing data
- Reserved words and operators for handling it.

In general, objects can be grouped into types according to their properties, i.e. the same operations can be performed on them.

Basic data types are built in into the language. The language recognizes them automatically. They usually require only a little if any non hardware support for their implementation.

All data has a type and only values of the same type can usually operate together. First, a quick look at some basic data types in C++ called arithmetic types.

Normal arithmetic operations can be performed on these types. In C++ these types are:

-int for integer (whole numbers and their negatives) i.e. -2, 123456.
-double (float) for real numbers, i.e. 3.1425, 1.4142
-char for character data (letters, digits, symbols, punctuation) i.e. 'a', '\n'

In fact character data is stored inc computers as numeric codings in C++.

The type for a sequence of characters in C++ is called string. string is a basic type for all practical purposes, but in reality it is more complex.


Previous post

Next post




Sunday, August 9, 2009

Syntax and semantical errors

Syntax error

Syntax errors occur when the program code violates the syntax i.e. the grammar rules of the language. A missing ";" in the end of a statement is an example of a syntax error in C++. A compiler finds all the syntax errors.

When an error has been located an error message is printed and the line number where the error is located is given. The error message usually starts with "syntax error" or "parse error".



Semantical errors

Semantical errors occur when the code is syntactically correct but the compiler can't understand it anyway. A good example of semantical error is if "endl" is misspelled "ndl". These kinds of errors are always found by the compiler.

In case of semantical error an error message is also printed. A semantical error usually confuses the compiler and then there is an abundance of error messages. It is recommended always to correct the errors in the order the compiler gives them.


Previous post

Next post




Saturday, August 8, 2009

Logical errors in program

Logical errors occur when the programmer forgets to consider a certain aspect of a program or just misunderstands it. The aspect overlooked can be miniscule. Logical errors are often typos or errors in the algorithm design.

When there is a logical error in the program, the program doesn't work correctly. A compiler cant spot these kinds of errors.

Remedy:
Locating logical errors can be troublesome. The only way of avoiding these errors is to plan and write the program with extreme care.


Previous post

Next post




Friday, August 7, 2009

Programming Language & compilation

A programming language is a language that can be used to communicate with a computer. It is similar to spoken language but much simpler. All computers understand only their machine codes i.e. sequence of 0s and 1s. Since machine code is tremendously difficult there is higher level languages. C, C++, Java etc. are examples of higher level languages. A program called compiler converts the entire higher level language to machine code which is understandable to computer.




Fig: The compilation process


Previous post

Next post




What is algorithm?

An algorithm is a step by step finite set of instruction for solving a problem. Its a description, a computational method of how a certain set of input is transferred to a desired set of output.

A good algorithm has at least the following properties:
  • It solves the given problem
  • It is precise and unambiguous i.e. it is obvious and totally clear what each instruction is meant to accomplish
  • It is simple and detailed enough for a computer to follow
  • It does what it is meant to do i.e. it works correctly
  • It defines the correct order of execution
  • It is finite. The algorithm must terminate after a finite number of operations

Following three constructs are needed to describe an algorithm:

1. Sequence: The instruction must be followed in a given order.
2. Selection: Execution of the path of a part of the algorithm depends on the circumstances. Example: if... else.
3. Repetition(iteration): A part of the algorithm may be repeated several times.


Previous post

Next post




Thursday, August 6, 2009

Idea of Programming

Programming is at heart problem solving. A successful programming task consists of

1. Problem solving phase:
  • Analyze and understand the problem that needs to be solved and figure out what is needed to be solved
  • General solution: Design an algorithm to solve the problem
  • Check the answer: By using a pen and paper check that your answer works properly.

2. Implementation phase:
  • Write the program: Implement the program with some available programming language i.e. C++, Java, C etc.
  • Test: Run the program with different inputs
  • Correct the errors: If error occurs, think hard "why?", analyze the algorithm/program and fix them

3. Maintenance phase:
  • Use the program: The program is used for its intended purpose
  • Remove any further errors and add new features

Design/implementation ratio of work and time used should be 50%/50%.


Previous post

Next post




Wednesday, August 5, 2009

What is Programming?

Programming, on a general level, means making a system, machine, person etc. do something commanded by someone else.

A system is programmable if it can
-Receive and understand given orders
-Execute those orders in correct sequence

Programming consists of the following phases:
1. Understanding the problem and the desired result
2. Creating a solution of the problem at hand
3. Explaining the solution in detail to the programmed system so that the system understand it thoroughly

Phase 2 is usually the most difficult. It requires a knowledge of how the problem should and can be solved. Phase 3 requires the knowledge of the system that is programmed.

From this point onwards programming means programming a computer i.e. the process needed to make the computer do what the user wants it to do. A computer is a programmable system.


Previous post

Next post