From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.

Slides:



Advertisements
Similar presentations
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Advertisements

C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Introduction to C Programming
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
CSE1301 Computer Programming Lecture 4: C Primitives I.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
Three types of computer languages
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
Introduction to C Programming
C programming Language and Data Structure For DIT Students.
The C Programming Lecture 24.
Computer Science 210 Computer Organization Introduction to C.
COMPUTER SCIENCE I C++ INTRODUCTION
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
A Review of Programming and C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Numeric Types, Expressions, and Output ROBERT REAVES.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Chapter 2: Using Data.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Khalid Rasheed Shaikh Computer Programming Theory 1.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Computer Science 210 Computer Organization
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Introduction to C Programming Language
CS1101 Computational Engineering
Introduction to C Programming
Computer Science 210 Computer Organization
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Lecture3.
CS150 Introduction to Computer Science 1
Introduction to C Topics Compilation Using the gcc Compiler
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language Program: –talking to computer (compiler) –can be regarded as a “formal expression” of an algorithm

High-Level Language Compilers and linkers translate a high level program into executable machine code. #include int main() { printf(“Hello World”); return 0; } Source code Executable code etc...

Why C? Flexible language: –Structured language –Low level activities possible Standard library exists, allowing portability It can produce lean and efficient code Wide availability on a variety of computers Widely used

History of C Developed in 1972 by Dennis Ritchie on a DEC PDP11 at Bell Systems Lab as a system development language –Derived from the language B of Ken Thompson, which itself was based on BCPL, developed by Martin Richards For many years the de-facto C standard was the version provided with Unix System V –The C ProgramminLanguage, Brian Kernigham and Dennis Ritchie, Prentice-Hall 1978 In 1983 ANSI creates a group to begin the standardization of C –ANSI C is finalized in 1989 (C89), and ISO adopts it in 1990 –ANSI updated the standard and C99 was adopted in 2000

Basic Structure of a C Program output “Hello World!” Algorithm: #include int main() { printf(“Hello World!”); return 0; } C Program: Example: Hello World

Basic Structure of a C Program (cont) #include int main() { printf(“Hello World!”); return 0; } C Program : Example: Hello world Includes declarations for the standard input/output library of procedures. Read: “Hash-include”

Basic Structure of a C Program #include int main() { printf(“Hello World”); return 0; } C Program: Curly braces mark the beginning and end of a block of instructions. Example: Hello World

Basic Structure of a C Program #include int main() { printf(“Hello World”); return 0; } C Program: Instruction (function call) to output “Hello World” Example: Hello World

Basic Structure of a C Program #include int main() { printf(“Hello World”); return 0; } C Program: “Statements” (lines of instructions) always end with a semi-colon ( ; ) Example: Hello World

int main() { return 0; } Example -- Count to 10 Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

#include int main() { return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

#include /* Print out numbers 0 to 9 */ int main() { return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Comment

#include /* Print out numbers 0 to 9 */ int main() { int count; return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Variable declaration

#include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

#include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Assignment of a value (right expression) to a variable (left).

#include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } No semi- colon here!

#include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

#include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count } Format string

#include /* Print out numbers 0 to 9 */ int main() { int count; count = 0; while ( count < 10 ) { printf(“%d\n”, count); count=count+1; } return 0; } Example -- Count to 10 (cont) Print out numbers 0 to 9 set count to 0 while ( count is less than 10 ) { output count add 1 to count }

Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign?

Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)

Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( number < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)

Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)

Find the sign of a number output “Enter a number” input num if (num is less than 0) then { output num “ is -’ve” } else { output num “ is +’ve” } #include /* Find the sign of a number */ int main() { float num; printf(“Enter a number: “); scanf(“%f”, &num); if ( num < 0 ) { printf(“%f is -’ve\n”, num); } else { printf(“%f is +’ve\n”, num); } return 0; } Example -- What’s your sign? (cont)

C Values and Variables Basic Types: –Integers –Floating point numbers –Characters –Character Strings

Basic Types: int and float Integers ( int ) Floating point numbers ( float ) e-1 1e1

Basic Types: char Characters ( char ) ’a’ ’z’ ’A’ ’Z’ ’?’ ’0’ ’9’ - Special Characters: preceded by \ ’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.

Basic Types: character string Character Strings (a string of char -s) Examples: –”Hi there!” –”Line 1\nLine 2\nLine 3” –”” –”\”\””

Arithmetic Expressions take arithmetic (numerical) values and return an arithmetic (numerical) value Are composed using the following operators: + (unary plus) - (unary minus) + (addition) - (subtraction) * (multiplication) / (division or quotient) % (modulus or remainder)

Precedence in Expressions Defines the order in which an expression is evaluated

Precedence in Expressions -- Example * / 5 = B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction. B.O.D.M.A.S. 1 + (2 * 3) - (4 / 5)

More on precedence *, /, % are at the same level of precedence +, - are at the same level of precedence For operators at the same “level”, left-to-right ordering is applied – 1 = (2 + 3) – 1 = 4 2 – = (2 – 3) + 1 = 0 2 * 3 / 4 = (2 * 3) / 4 = 6 / 4 2 / 3 * 4 = (2 / 3) * 4 = 0 * 4

Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

Precedence in Expressions – Example (cont) Integer division results in integer quotient * / 5 = 1 + (2 * 3) - (4 / 5)

Precedence in Expressions – Example (cont) = 0 D’oh * / 5 = 1 + (2 * 3) - (4 / 5)

Precedence in Expressions – Example (cont) * / 5 = 1 + (2 * 3) - (4 / 5)

int -s and float -s float is a “communicable” type Example: * / 5 = 1 + (2 * 3) - (4.0 / 5) = = 6.2

int -s and float -s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0

int -s and float -s – Example 2 (cont) ( ) * (3 - 4) / 5 = (( ) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6

int -s and float -s – Example 3 ( ) * ((3 - 4) / 5) = ( ) * (-1 / 5) = 3.0 * 0 = 0.0

Unary operators Called unary because they require one operand. Example i = +1;/* + used as a unary operator */ j = -i;/* - used as a unary operator */ The unary + operator does nothing – just emphasis that a numeric constant is positive. The unary – operator produces the negative of its operand.

Increment and decrement operators ++ is the increment operator i++; is equivalent to i = i + 1; -- is the decrement operator j--; is equivalent to j = j - 1; (King, pp53-54)

Evaluate an expression set result to * / 5 output result #include Example -- Simple Expressions

Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ Example -- Simple Expressions (cont)

Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { return 0; } Example -- Simple Expressions (cont)

Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; return 0; } Example -- Simple Expressions (cont)

Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; result = * / 5; return 0; } Example -- Simple Expressions (cont)

Evaluate an expression set result to * / 5 output result #include /* Evaluate an expression */ int main() { float result; result = * / 5; printf(“%f\n”, result); return 0; } Example -- Simple Expressions (cont)

Evaluate an expression set result to * / 5 output result Output: #include /* Evaluate an expression */ int main() { float result; result = * / 5; printf(“%f\n”, result); return 0; } Example -- Simple Expressions (cont)