Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 1 Engineering Problem Solving.
Advertisements

EC-111 Algorithms & Computing Lecture #1 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 1: Overview of Computers & Programming
Chapter 6: Algorithmic Problem Solving 1 Chapter 6 Algorithmic Problem Solving.
CS1010 Programming Methodology
COSC 120 Computer Programming
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Three types of computer languages
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Guide To UNIX Using Linux Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Topic 1: Introduction to Computers and Programming
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
1 Chapter Two Introduction to the Programming Language C.
Chapter 1 Introduction to Programming. Computer Hardware CPU Memory –Main or primary –Secondary or auxiliary Input device(s) Output device(s)
Chapter 1: Introduction To Computer | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1 Introduction To Computers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Introduction to Computers and C Programming Outline Introduction What Is a Computer? Computer Organization.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
CS102 Introduction to Computer Programming
Introduction to Computers and Python. What is a Computer? Computer- a device capable of performing computations and making logical decisions at speeds.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
COMPUTER SCIENCE I C++ INTRODUCTION
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Spring 2005, Gülcihan Özdemir Dağ BIL104E: Introduction to Scientific and Engineering Computing, Spring Outline 1.1Introduction 1.2What Is a Computer?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
1 The Art of Programming Programming Languages Flowcharts.
 2006 Pearson Education, Inc. All rights reserved Introduction to Computers, the Internet and World Wide Web.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages.
Programming Fundamentals1 Chapter 1 INTRODUCTION TO COMPUTER AND PROGRAMMING.
1 MIS 131 Introduction to Algorithms and Programming 2015/2016 Fall - Chapter 1 -
1 Programming Fundamentals How to Program in C++ How to Program in C++
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 1 st semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
The Hashemite University Computer Engineering Department
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Introduction to Computer Programming using Fortran 77.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Chapter 1: Introduction to Computers and Programming
Chapter 5: Preparing C Programs
Chapter 1 Introduction 2nd Semester H
CS1010 Programming Methodology
Engineering Problem Solving With C An Object Based Approach
Chapter 1: Introduction to computers and C++ Programming
Key Ideas from day 1 slides
Lecture 1: Introduction to JAVA
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
C++ Programming: From Problem Analysis to Program Design
Chapter 1: Introduction to Computers and Programming
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
The C Language: Intro.
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

Introduction to Computer & C

Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are calculators and game-playing machines Examples of general-purpose computers are personal computers and notebooks

Hardware/Software A computer consists of hardware and software The hardware consists of various physical devices that performs wired and basic operations The software consists of various programs that coordinates basic operations to accomplish flexible and complex tasks

Programs A Program is a sequence of instructions (basic operations) to control the operation of the computer Programming is the task of designing programs Programming languages are notations used to represent the instructions of computers

Hardware storage unit input unit primary storage (memory) unit output unit secondary storage unit control unit arithmetic and logic unit (ALU) central processing unit (CPU) I/O unit control bus data bus

Control Unit Control unit repeatedly fetches instructions from memory unit to control unit via data bus Control unit then interprets instructions, and coordinates the operations of other units via control bus

Arithmetic and Logic Unit ALU is responsible for performing calculations based on arithmetic operations such as addition, subtraction, multiplication, and division ALU is also responsible for making decisions based on logical operations such as equality (=, ≠) and relation (, ≧ )

Arithmetic and Logic Unit Arithmetic or logical operations performed by ALU are controlled by control unit via control bus Data on which operations are performed are transferred from memory unit via data bus Data resulted from operations are transferred to memory unit via data bus

Primary Storage Unit Memory unit stores both instructions and data It retains information that is actively being used by the computer It is the short-term, rapid-access, low-capacity warehouse of a computer

Secondary Storage Unit Secondary storage unit retains information that is not actively being used by the computer It is the long-term, slow-access, high-capacity warehouse of the computer Common secondary storage devices are disks and tapes

Input Unit Input unit transfers information from various input devices to memory unit It also transforms information in human- readable form to information in machine- readable form Common input devices are keyboards and mouse devices

Output Unit Output unit transfers information from memory unit to various output devices It also transforms information in machine- readable form to information in human- readable form Common output devices are screens and printers

Users Software Application Programs Operating System Hardware

Operating System The operating system provides efficient management of the hardware so that application programmers can easily use the computer without knowing the detailed operations of the hardware Common operating systems are DOS, Windows 2000, Windows NT, Unix, Linux

Application Programs An application program allows users to use a computer to solve problems in a specific domain without knowing the details of the computer Common application programs are MS Word, MS Excel, MS Access, MS Internet Explorer, Netscape Communicator Biological Applications: BLAST, FastA, DOCK, DSSP, …

High-level languages Machine language: computer’s native language. Add two numbers: Assembly language: uses mnemonics. Add two numbers: ADD R1 R2 An assembly code needs to be translated to machine code.

High-level languages High-level programming languages: bridging the gap between machine language and natural languages. Examples: COBOL, Fortran, Algol, C Add two numbers (in C): Z = A + B; Compilation: translation to machine code.

Compilers A compiler translates high-level language programs into assembly language programs or machine language programs High-level language C, C++, Pascal Interpreter Basic, PHP, … Each high-level instruction is usually translated into several assembly instructions

Algorithms An algorithm is an abstract strategy for solving a problem Solving a problem by computer consists of designing an algorithm and expressing the algorithm as a program

The Programming Language C C is a general-purpose programming language C is developed by Dennis Ritchie at Bell Laboratories C has become one of the most widely used languages in the world

The C Programming System C programming language A set of notations for representing programs C standard library A set of well-developed programs C programming environment A set of tools to aid program development

Programs A C program consists of functions and variables A function contains instructions that specify the operations to be done during the computation Variables denote memory locations that store data used during the computation

Functions Functions can be either user-defined functions or library functions A C program begins the execution at the beginning of the function named main A function may call other functions to help it perform one of its subtasks

The First C Program #include /* include library information */ main( )/* name of starting function */ {/* beginning of instructions */ printf(“hello, world\n”); /* call library function */ }/* end of instructions */

C Programming Environment Edit Compile Link Execute prog1.c, prog1.h, prog2.c, prog2.h prog1.obj, prog2.obj prog.exe lib.h lib.obj input output Debug

Compilation in C Use the cc or gcc compiler: cc prog.c Produces executable file a.out if no errors To specify name of executable file, use -o option: cc -o prog prog.c Name of executable file becomes ‘prog’. Workstation

Errors Compilation errors: occur during compilation. Reason: syntax errors. Easy to rectify. Run-time errors: occur during execution. Reasons: logic errors, data errors, computation errors. Harder to rectify.

Program development EDIT Understanding the problem Writing an algorithm Converting to code COMPILE RUN errors Result Verifying the algorithm Program = Algorithm + Data Structures

Algorithm Algorithm: a well defined computational procedure consisting of a set of instructions, that takes some value(s) as input, and produces some value(s), as output. Al-Khowarizmi  Algorismus  Algorithm

Algorithm InputOutputAlgorithm

Understanding the problem Writing an algorithm Converting to code Verifying the algorithm Algorithm embeds logic of solution. Algorithm is converted to program. Algorithmic problem solving: writing an algorithm -- tough translate algorithm to code -- easy

Euclidean algorithm First documented algorithm by Euclid (300 B.C.) to compute greatest common divisor (gcd). Examples: gcd(3,21) = 3; gcd(15,40) = 5. 1.Let A and B be integers with A > B  0. 2.If B = 0, then the gcd is A and the algorithm ends. 3.Otherwise, find q and r such that A = qB + r where 0  r < B Note that we have 0  r < B < A and gcd(A,B) = gcd(B,r). Replace A by B, B by r. Go to step 2.

Euclidean algorithm Walk through the algorithm with examples: A = 40, B =15. A = 2B + 10  A = 15 ; B = 10 A = 1B + 5  A = 10 ; B = 5 A = 2B + 0  A = 5 ; B = 0 gcd is 5 1.Let A and B be integers with A > B  0. 2.If B = 0, then the gcd is A and the algorithm ends. 3.Otherwise, find q and r such that A = qB + r where 0  r < B Note that we have 0  r < B < A and gcd(A,B) = gcd(B,r). Replace A by B, B by r. Go to step 2.

Data types and structures Data types: integer, real number, character, Boolean, pointer, etc. Examples: 32, , 'a', true. Data structures: arrays, records, files, etc. Program = Algorithm + Data Structures Data are stored in variables in a program. Variables take up memory space in the computer.

Euclid ’ s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } “New Style” function declaration lists number and type of arguments Originally only listed return type. Generated code did not care how many arguments were actually passed. Arguments are call- by-value mn mn mn

Euclid ’ s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } Automatic variable Storage allocated on stack when function entered, released when it returns. All parameters, automatic variables accessed w.r.t. frame pointer. Extra storage needed while evaluating large expressions also placed on the stack n m ret. addr. r Frame pointer Stack pointer Excess arguments simply ignored

Euclid ’ s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } Expression: C’s basic type of statement. Arithmetic and logical Assignment (=) returns a value, so can be used in expressions % is remainder != is not equal

Euclid ’ s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } High-level control-flow statement. Ultimately becomes a conditional branch. Supports “structured programming” Each function returns a single value, usually an integer. Returned through a specific register by convention.

Characteristics of an algorithm Each step must be exact. Must terminate. Must be effective. Must be general.

Pseudo-code How to represent an algorithm? Pseudo-code Flowchart Pseudo-code: a combination of English text, mathematical notations, and keywords from the programming language.

Pseudo-code To find average, min and max among a list. First, you initialise sum to zero, min to a very big number, and max to a very small number. Then, you enter the numbers, one by one. For each number that you have entered, assign it to num and add it to the sum. At the same time, you compare num with min, if num is smaller than min, let min be num instead. Similarly, you compare num with max, if num is larger than max, let max be num instead. After all the numbers have been entered, you divide sum by the numbers of items entered, and let ave be this result. End of algorithm.

Pseudo-code To find average, min and max among a list. sum  count  0{ sum = sum of numbers; count = how many numbers are entered? } min  ?{ min to hold the smallest value eventually } max  ?{ max to hold the largest value eventually } for each num entered, increment count sum  sum + num if num < min then min  num if num > max then max  num ave  sum/count

Flowchart Diagrammatic form: terminator connector process box decision box

Flowchart To find minimum and maximum among a list. start sum  count  0 min  ? max  ? end of input? increment count sum  sum + num num<min? num>max? min  num Yes No ave  num/count A A end Yes max  num Yes No

Exercise Dedign a flowchart for gcd function Write a program for gcd function