Arash Rafiey - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Arash Rafiey

Description:

cout 'Enter your dessert: ' endl; cin dessert; ... cout name ' has selected: ' dessert endl; return 0; The String class ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 56
Provided by: Ara8
Category:
Tags: arash | dessert | rafiey

less

Transcript and Presenter's Notes

Title: Arash Rafiey


1
Arash Rafiey
  • arafieyh_at_cs.sfu.ca
  • TA Xu Cheng
  • xuc_at_cs.sfu.ca
  • Office Hours
  • M-W 1030 1130

2
How Create a C Program
3
  • includeltiostreamgt
  • using namespace std
  • void main()
  • coutltltHello World
  • If your compiler gives error then

4
  • includeltiostream.hgt
  • void main()
  • coutltltHello World
  • coutltltendl // for going to new line
  • coutltlt good bye
  • cout is an object for printing out some thing
  • on the screen.

5
  • includeltiostream.hgt
  • void main()
  • coutltltHello World \n
  • coutltlt good bye
  • we can use \n instead of coutltltendl

6
How to run your program
  • Using Linux or unix
  • g myFile.cpp
  • ./a.out
  • g myFile.cpp myFile.out
  • ./myFile.out

7
Declare Statement Variable
  • Each variable must be declare before use
  • Each variable has a type
  • For example
  • int , char, float.
  • int for Integer type
  • char for character like A
  • float for real number

8
Example
  • int LuckyNumber17
  • float RealNumber
  • char aA

9
Identifiers
  • Identifier name of a variable, function, or
    class
  • Rules for identifiers in C
  • 1 Can be made up of letters, digits, and the
    underscore (_) character
  • 2 Cannot start with a digit
  • 3 Cannot use other symbols such as ? or
  • 4 Spaces are not permitted inside
    identifiers
  • 5 You cannot use reserved words
  • 6 They are case sensitive

10
Self Check
  • 1. What is the type of the values 0 and 0?
  • 2. Which of the following are legal identifiers?
  • Greeting1gvoid101dalmatiansHello,
    Worldltgreetinggt

11
Answer
  • int and char
  • Only the first two are legal identifiers

12
Syntax Variable Definition
  • typeName variableName valueor typeName
    variableName
  • Example
  • int numbe 12
  • Purpose
  • To define a new variable of a particular type and
    optionally supply an initial value

13
The Assignment Operator
  • Assignment operator
  • Not used as a statement about equality
  • Used to change the value of a variable
  • int number1
  • int number2, number3 number1number2number3
    88

14
  • number2number2-1
  • number3number21

15
How to read a variable
  • includeltiostreamgt
  • using namespace std
  • void main()
  • int number
  • coutltltplease enter a number \n
  • cingtgtnumber
  • numbernumber1
  • coutltltthe number is ltltnumber

16
Integer Types
  • The short, int and long Integer Types
  • A short integer is at least 16 bits
  • A int integer is at least as big as short
  • A long integer is at least 32 bits and at
  • least as big as int .

17
  • E.g. A 16-bit int might run from -32768 to 32767
  • The sizeof operator returns the size (in bytes)

18
  • includeltiostream.hgt
  • int main()
  • int n_int INT_MAX
  • short n_short SHRT_MAX
  • long n_long LONG_MAX
  • cout ltlt int is ltlt sizeof (int) ltlt bytes
    ltlt endl
  • cout ltlt short ltlt n_short ltlt endl
  • cout ltlt long ltlt n_long ltlt endl
  • return 0

19
  • int is 4 bytes
  • Maximum values
  • Short 32767
  • Long 2147483647

20
Characters and small integers
  • includeltiostream.hgt
  • int main()
  • char ch M // assign ASCII code
  • int a ch
  • cout ltlt ASCII code ltlt ch ltlt is ltlt a
    ltlt endl
  • ch ch 1
  • a ch
  • cout ltlt ASCII code ltlt ch ltlt is ltlt a ltlt
    endl
  • return 0

21
Output
  • M is 77
  • N is 78

22
Boolean type
  • bool isReady true
  • int ans true // ans assigned 1
  • int promise false // promise assigned 0
  • bool start -100 // true
  • bool stop 0 // false

23
Floating-point number
  • E.g.
  • 12.34
  • 9300.3
  • 0.02
  • 8.0
  • We have
  • float, double, long double

24
Arithmetic operators
  • Summation
  • Multiplication
  • Division /
  • Subtraction -

25
Operator Precedence
  • int number 3 4 5 // 35 or 23?
  • float logs 120 / 4 5 // 150 or 6??

26
Type Casting
  • Conversion between types
  • (typeName) value // c
  • typeName (value) // c
  • e.g.
  • cout ltlt int(A) ltlt endl // 65
  • float one,
  • int two
  • one 1.9 2.1
  • two (int) 1.9 (int) 2.1

27
Functions
  • Building blocks of programs
  • A function
  • Has an input and an output
  • Contains a set of instructions
  • x sqrt(16) // returns 4

28
Syntax
  • typeName functionName (typeName varName_1, )
  • BODY
  • .
  • .
  • .
  • return value

29
Examples of functions
  • int sum (int firstValue, int secondValue)
  • int final
  • final firstValue secondValue
  • return final
  • void main()
  • int a 1
  • int b 2
  • int total sum(a,b)
  • cout ltlt Total is ltlt total ltlt endl

30
Arrays
  • An array is a data form that holds several values
    of the same type
  • Syntax
  • typeName arrayNamevalue
  • e.g
  • int someArray3
  • Index starts from 0!!!
  • someArray0 1
  • someArray2 2

31
Initializations Rules for Arrays
  • int array4 2,6,4,5
  • int secondArray4
  • secondArray4 5,6,7,8 // error!!!
  • secondArray array //error!!!
  • float Hotel5 1.1, 2.2
  • long total500 0

32
  • short array 1,2,3,4

33
String
  • Series of characters stored in a consecutive
    bytes
  • Create a string as an array but the last element
    must be the null character \0
  • e.g
  • char dog5 b,e,a,u,x //NOT!!
  • char dog6 b,e,a,u,x,\0 //
    STRING!!

34
More examples
  • char dog5 beaux //a better way
  • null character is implicitly included.
  • char namec // compiler counts
  • char boss8Arvind

35
  • includeltiostream.hgt
  • int main()
  • int arSize 20
  • char namearSize
  • char dessertarSize
  • cout ltlt Enter your name ltlt endl
  • cin gtgt name
  • cout ltlt Enter your dessert ltlt endl
  • cin gtgt dessert
  • cout ltlt name ltlt has selected ltlt dessert ltlt
    endl
  • return 0

36
Another way to read a string
  • includeltiostream.hgt
  • int main()
  • int arSize 20
  • char namearSize
  • char dessertarSize
  • cout ltlt Enter your name ltlt endl
  • cin.getline(name, arSize) // reads through
    newline
  • cout ltlt Enter your dessert ltlt endl
  • cin.getline(dessert, arSize)
  • cout ltlt name ltlt has selected ltlt dessert ltlt
    endl
  • return 0

37
The String class
  • To define strings more easily
  • Include the string class
  • includeltstringgt
  • string str_1 jaguar

38
Assignment Concatenation Appending
  • char char_120
  • char char_220 jaguar
  • string str_1
  • string str_2 panther
  • char_1 char_2 // INVALID!!
  • str_1 str_2 // VALID!!

39
Appending
  • string str_3
  • str_3 str_1 str_2 // join str_1 and str_2
  • str_1 str_2 // add str_2 to the end of str_1

40
More string operations
  • Copying include ltcstringgt
  • strcpy(char_1, char_2) //copy char_2 into
    char_1 strcat(char_1, char_2) //append char_2 to
    char_1
  • Size of a string
  • char charOne20 p,i,e
  • string strOne pie
  • strlen(charOne)
  • strOne.size()

41
Increment Decrement
  • Increment
  • int a 20
  • int b 20
  • cout ltlt a ltlt a ltlt b ltlt b ltlt endl
  • cout ltlt a ltlt a ltlt b ltlt b ltlt
    endl
  • cout ltlt a ltlt a ltlt b ltlt b ltlt endl

42
  • a 20 b20
  • a 20 b21
  • a21 b21
  • int x 5
  • int y x

43
Decrement --
  • Same rules as increment ( i.e. )

44
Loops
  • different types
  • for
  • while
  • etc.

45
For Loop
  • for(initialValue test-expression
    update-expression)
  • BODY

46
Example
  • includeltiostream.hgt
  • int main()
  • int a
  • for (a 0 a lt 10 a )
  • cout ltlt a ltlt endl
  • return 0

47
While loop
  • while(test-expression)
  • BODY

48
Example
  • includeltiostream.hgt
  • int main()
  • int arSize 20
  • char namearSize
  • cin gtgt name
  • int a 0
  • while (namea ! \0)
  • cout ltlt namea ltlt endl
  • a
  • return 0

49
Conditional Statements
  • if statements
  • Syntax
  • if (condition)
  • IF_BODY
  • else
  • ELSE_BODY

50
Conditions
  • Relational Expressions
  • Comparisons
  • e.g
  • tests equality
  • gt is greater?

51
Example
  • int number
  • cin gtgt number
  • if ( number lt 0)
  • cout ltlt Negative Value ltlt endl
  • else
  • cout ltlt Non-negative Value ltlt endl

52
Comparing two strings
  • includeltiostream.hgt
  • int main()
  • int arSize 20
  • char name1arSize
  • char name2arSize
  • coutltltString1ltltendl
  • cingtgtname1
  • coutltltString2ltltendl
  • cingtgtname2

53
  • int len1,len2,a
  • len1strlen(name1)
  • len2strlen(name2)
  • bool flagtrue
  • if (len1 ! len2 )
  • coutltltdifferent
  • else
  • for (a0 name1aname2a)
  • if (alen1)
  • coutltltThe same
  • else
  • coutltltdifferent

54
Sort an Array of Integer
  • includeltiostream.hgt
  • int main()
  • int arSize 20
  • int ArrayNumberarSize
  • int indx
  • for(index0 index lt20 index)
  • coutltltenter an integerltltendl
  • cingtgtArrayNumberindex

55
  • int i,j
  • int temp
  • for( i1 ilt 20i)
  • for (j0 jlt ij)
  • if (ArrayNumber j gt ArrayNumber j1)
  • temp ArrayNumber j
  • ArrayNumber j ArrayNumber j1
  • ArrayNumber j1temp
Write a Comment
User Comments (0)
About PowerShow.com