You are on page 1of 5

Unit 1

Programming Logic and techniques


Algorithms - An Algorithm is a sequence of precise instructions which leads to a solution. Flowcharts- A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows. This diagrammatic representation illustrates a solution to a given problem. 1. 2. 3. 4. 5. 6. Start Read n1, n2; Declare sum; Sum=n1+n2; Display sum; End

Introduction to C++ High-level Languages Common programming languages include C C++ Java Pascal Visual Basic FORTRAN COBOL Lisp Scheme Ada These high level languages resemble human languages and are designed to be easy to read and write. These languages must be translated to zeros and ones for the CPU to execute a program. Low-level Languages An assembly language command such as ADD X Y Z

might mean add the values found at x and y in memory, and store the result in location z. Assembly language must be translated to machine language (zeros and ones) to be executed. 0110 1001 1010 1011 Compilers Translate high-level language to machine language Source code The original program in a high level language Object code The translated version in machine language Linkers Some programs we use are already compiled. Their object code is available for us to use. For example: Input and output routines. A Linker combines the object code for the programs we write and the object code for the pre-compiled routines into the machine language program the CPU can run. Algorithm A sequence of precise instructions which leads to a solution Program An algorithm expressed in a language the computer can understand. Program Design A typical programming task can be divided into two phases: o Problem solving phase It produces an ordered sequence of steps that describe solution of problem. This sequence of steps is called an algorithm Implementation phase o Implement the program in some programming language Problem Solving Phase Be certain the task is completely specified What is the input? What information is in the output? How is the output organized? Develop the algorithm before implementation Experience shows this saves time in getting your program to run. Test the algorithm for correctness Implementation Phase

Translate the algorithm into a programming language Easier as you gain experience with the language Compile the source code Locates errors in using the programming language Run the program on sample data Verify correctness of results Results may require modification of the algorithm and program

Introduction to C++ Origin of C++ C++ was derived from the C language. C developed by Dennis Ritchie at AT&T Bell Labs in the 1970s. C++ developed by Bjarne Stroustrup at AT&T Bell Labs in the 1980s. Advantages of C++ over C Overcame several shortcomings of C Incorporated object oriented programming C remains a subset of C++ A Sample C++ Program Source Code: #include <iostream.h> int main() { cout << Hello World..!!!; return 0; } Output : Hello World..!!! #include <iostream.h> Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. int main () This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source

code. It is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them. Right after these parentheses we can find the body of the main function enclosed in braces {}. What is contained within these braces is what the function does when it is executed. cout << "Hello World!"; cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file. Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs. return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program. Running a C++ Program C++ source code is written with a text editor. The compiler on your system converts source code to object code. The linker combines all the object code into an executable program. To compile a C++ program press ALT+F9 and to execute the program press CTRL+F9. Testing and Debugging Bug - A mistake in a program Debugging - Eliminating mistakes in programs Program Errors Compilation Errors Compilation errors, also known as compiler errors, are errors that prevent your program from running. Most compiler errors are caused by mistakes that you make when typing code. For example, you might misspell a keyword or leave out some necessary punctuation. Run Time Errors

Run-time errors are errors that occur while your program runs. These typically occur when your program attempts an operation that is impossible to carry out. An example of this is division by zero. Suppose you had the following statement: Speed = Miles / Hours If the variable Hours has a value of 0, the division operation fails and causes a run-time error. The program must run in order for this error to be detected, and if Hours contains a valid value, it will not occur at all. Logic Errors Logic errors are errors that prevent your program from doing what you intended it to do. Your code may compile and run without error, but the result of an operation may produce a result that you did not expect.

You might also like