Introduction to Algorithmic and Programming in C PDF
Document Details
Uploaded by CommendableFable
Tunis Business School
null
Tags
Summary
This document is an introduction to algorithmic and programming using the C language. It explains what algorithms and programming are, and describes the steps involved in the compilation process. Includes examples and characteristics of algorithms.
Full Transcript
Chapter 1: Introduction to algorithmic and programming in C language 1 Chapter outcomes By the end of this chapter, the students will understand what an algorithm is used for and be able to tell the differences between an algorithm and a progra...
Chapter 1: Introduction to algorithmic and programming in C language 1 Chapter outcomes By the end of this chapter, the students will understand what an algorithm is used for and be able to tell the differences between an algorithm and a program, be able to write a basic algorithm to solve a simple problem, understand what a program is used for, learn what the compilation process consists of and the different steps composing it. 2 Introduction Computer Science Computer Science is not limited to the study of computers. is also the study of problems, problem-solving, and the solutions that come out of the problem-solving process. 3 Introduction Problem solving phases Analysis and specification o Understand (define) the problem and what the solution must do. General solution o Specify the required data types and the logical sequence of steps that describe one possible solution of the problem an algorithm Verification o Follow the steps exactly to see if the proposed algorithm really does solve the problem. Implementation o Implement that algorithm in one (or more) 4 programming language a program Introduction From a problem to programs 5 What is an algorithm? 6 What is an algorithm? The word algorithm comes from the name of a Persian mathematician Abu Ja’far Mohammed ibn-i Musa al Khawarizmi. An algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output in a finite amount of time. 7 Example of an algorithm 8 Characteristics of an algorithm An algorithm should have the following characteristics: Unambiguous Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. Input It should have 0 or more well-defined inputs. Output It should have 1 or more well-defined outputs, and should match the desired output. Finiteness It must terminate after a finite number of steps. Feasibility Should be feasible with the available resources. Independent 9 An algorithm should have step-by-step directions, which should be independent of any programming code. How to write an algorithm There are NO well-defined standards for writing algorithms Algorithms are NEVER written to support a particular programming code. As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-else), etc. These common constructs can be used to write an algorithm. We write algorithms in a step-by-step manner, but it is not always the case. 10 Algorithm general structure 11 Example Problem − Design an algorithm to add two numbers and display the result. 12 Example Problem − Design an algorithm to add two numbers and display the result. Algorithm ADD Algorithm ADD Step 1 − START Var a, b, c: integer Step 2 − declare three integers a, b & c Step 3 − define values of a & b BEGIN Step 4 − add values of a & b 1− read(a , b ) Step 5 − store output of step 4 to c 2−c←a+b Step 6 − print c 3 − write(c ) Step 7 − STOP END A correct algorithm Another correct algorithm 13 What is a program ? Programming is the implementation of an algorithm in a programming language. It is the process of taking an algorithm and encoding it into a notation (programming language), so that it can be executed by a computer. There are many programming languages: Pascal, C, C++, Cobol, Fortran, Java, Python, Delphi, etc. This course will focus on learning programming using the C programming language 14 Example From an algorithm to a C program Algorithm ADD Var a,b,c: integer BEGIN 1− read( a , b) 2−c←a+b 3 − write( c) END 15 Creating and running a C program Compiling: The process of transforming a high level language (source code (human readable)) into a low level language (machine code or executable (computer language)). Compiling a C program is a multi-stage process. It can be split into 3 main stages: preprocessing, compilation and linking. 16 Creating and running a C program Preprocessing: this phase includes Removal of Comments Expansion of Macros Expansion of the included files. Compiling: making the object file Translate the filename.c and produce an intermediate compiled output file filename.obj. This file contains machine level instructions. Linking: putting it all together All the linking of function calls with their definitions are done. Linker knows where all these functions are 17 implemented (in other.obj files or in header files.h). The output of this step is an executable file.exe. Creating and running a C program 18 Creating and running a C program The compiler ensures that your program is TYPE correct. For example, you are not allowed to assign a string to an integer variable. ensures that your program is syntactically correct. For example, "x * y" is valid, but "x @ y" is not. does NOT ensure that your program is logically correct. 19