Introducing%20C %20Elements - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Introducing%20C %20Elements

Description:

An algorithm is based on the notion of sequence, which is the ordering of steps/statements. ... Comments make a program easier to understand ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 22
Provided by: michae1315
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Introducing%20C %20Elements


1
Introducing C Elements
2
Outline
  • Main algorithms constructs
  • General form of a C program section 2.5
  • C language elements section 2.1
  • Executable statements section 2.4
  • Reserved words and symbols section 2.2
  • Data types section 2.3

3
Main Algorithms Constucts
  • An algorithm is written as a step-by-step
    procedure (sequence) in which choices can be made
    where necessary (selection), and all or part of
    the algorithm can be repeated (repetition).
  • Thus, the basic control structures of algorithms
    are
  • 1- Sequence
  • 2- Selection
  • 3- Repetition

4
Sequence
  • An algorithm is based on the notion of sequence,
    which is the ordering of steps/statements.
  • Step n cannot be started until step n-1 is
    complete.

5
General Form of a C Program
  • // File filename
  • // Program description .
  • include directives
  • using namespace std
  • void main()
  • Variables declaration section
  • Executable statements section

6
General Form of a C Program (contd)
  • Function (a collection of related statements) is
    the basic unit in C
  • A C program must contain a main function
  • void main ()
  • void - function returns no value
  • main - lower case followed by ()
  • - braces define the function body

7
General Form of a C Program (contd)
  • General form of function body parts
  • Declaration statements
  • Variables and constants
  • Executable statements
  • C statements

8
Comments
  • Comments make a program easier to understand
  • They are ignored (i.e. not translated) by the
    compiler
  • // used to signify a comment on a single line
  • / Text text / used for comments on multiple
    lines

9
Compiler Directives
  • include
  • Compiler directive
  • Processed during compilation process
  • Instructs on what you want in the program
  • include ltiostreamgt
  • Adds library class/file called iostream to
    program
  • Used with lt gt
  • Also user defined

10
Program Processing Diagram
11
Program Processing Diagram (2)
12
ltiostreamgt
  • Included in iostream
  • cout refers to the standard output device i.e.
    the screen
  • cout ltlt "Hello!"
  • ltlt output operator
  • (insertion operator)
  • cin refers to the standard input device i.e. the
    keyboard
  • cin gtgt N1 gtgt N2
  • gtgt input operator
  • (extraction operator)
  • directs input to variable

13
Executable Statements
  • cout displays output on the screen
  • cout ltlt Enter the distance in miles
  • cin gets input from the keyboard
  • cin gtgt miles
  • Assignment
  • kms KM_PER_MILES miles

14
Reserved Words and Symbols
  • Reserved words have special meanings
  • Can NOT be used for other purposes (const, float
    and void are some examples)
  • Special symbols / delimiters
  • C has rules for special symbols
  • ( ) // ltlt gtgt , -

15
Data Types
  • Predefined data types
  • int (integer)
  • Positive or negative whole number
  • 1000 12 199 100000
  • The size of an int depends on the machine and the
    compiler. On a PC it is usually a word (16 bits).
  • Other integers types are
  • short uses less bits (usually a byte)
  • long typically uses more bits (usually 2 words)

16
Data Types (contd)
  • float (floating point / real number)
  • Positive or negative decimal number
  • 10.5 1.2 100.02 99.88
  • Integer part and fraction part
  • The number 108.1517 breaks down into the
    following parts
  • 108 - integer part
  • 1517 - fractional part
  • Other floating-point data types are
  • double
  • long double
  • bool (boolean)
  • true
  • false

17
Data Types (contd)
  • char (character)
  • Represents a character
  • Individual character value (letter or number)
  • Character literal enclosed in single quotes A
  • Characters are encoded using a scheme where an
    integer represents a particular character

18
Exercise
  • Problem
  • Analyse, design (using a flow chart), and
    implement (using C) an algorithm that
    calculates, and outputs the sum (sum) of three
    numbers (n1, n2 n3) input by the user.
  • Analysis
  • Input
  • float n1 first variable
  • float n2 second variable
  • float n3 third variable
  • Output
  • float sum sum of n1, n2, n3

19
Exercise (contd)
  • Implementation (C)
  • include ltiostreamgt
  • using namespace std
  • void main ()
  • float n1, n2, n3, sum
  • cout ltlt Please input three numbers
  • cin gtgt n1 gtgt n2 gtgt n3
  • sum n1 n2 n3
  • cout ltlt The sum is ltlt sum
  • Design

START
Processing
INPUT n1, n2, n3
sum n1 n2 n3
OUTPUT sum
STOP
20
Addition.cpp
  • / FILE Addition.cpp
  • PROGRAM Adds three numbers input by the user
    /
  • include ltiostreamgt
  • using namespace std
  • void main ()
  • float n1, n2, n3, sum // declaring
    variables
  • cout ltlt Please input three numbers
  • cin gtgt n1 gtgt n2 gtgt n3 // inputting 3 variables
  • sum n1 n2 n3 // adding the 3 variables
  • cout ltlt The sum is ltlt sum // outputting
    sum

21
  • Next lecture will be about
  • Arithmetic Expressions
About PowerShow.com