Arrays and Strings - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Arrays and Strings

Description:

Arrays (holds several values of the same type) Strings (special array which can hold series of characters) ... E.g. Rottweiler, Rhodesian Ridgeback, Ori Pei ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 19
Provided by: amandaso
Category:

less

Transcript and Presenter's Notes

Title: Arrays and Strings


1
Arrays and Strings
2
Derived Data Types
  • Built from basic integer and floating- point
    data types.
  • E.g.
  • Arrays (holds several values of the same type)
  • Strings (special array which can hold series of
    characters)
  • Structures (holds several values of different
    types)
  • Pointers (holds the address of an another
    variable)
  • Classes (Sorry ?, we dont discuss about
    classes)

3
Arrays
  • An array is a data form that can hold several
    values, all of one type.
  • E.g. 1 An array of size 12 can be hold monthly
    sales figures.
  • These sales figures are of the same type
    floating-point.
  • Each value is stored in a separate array
    element.
  • Computer stores all the elements consecutively
    in memory.

2nd element
1st element
12th element
4
Arrays
  • An array declaration statement should indicate
    three things
  • Type of value to be stored in each element.
  • Name of the array.
  • Number of elements in the array.
  • E.g. float sales12

Data Type
Array name
Size of the array

11
1
2
10
0
sales

Sales0
Sales11
Sales1
Sales10
5
Arrays
  • General Format
  • data_type_name array_name array_size
  • The size of the array must be a
  • constant (E.g. 10)
  • constant value (E.g. const int size 15)
  • constant expression (E.g. sizeof(int) 5)
  • Note Array size cannot be a variable.
  • E.g.
  • int size 5
  • float birdsize //Invalid

6
Arrays
  • To access each array element individually we
    use an index to number the array element.
  • This numbering starts from 0 and cannot be
    changed.
  • C uses array_name and bracket with the index
    to specify an array element.
  • sales0 - 1st element , sales1 - 2nd
    element
  • Each element can be treated like a simple
    variable.
  • C also lets you initialize array elements
    within the declaration statement.
  • E.g. int marks3 77, 60, 55

7
Arrays
  • Rules on Array initialization
  • You can use the initialization form only when
    defining the array.
  • E.g. int x4 3, 6, 8, 10) // okay
  • You cannot initialize it later.
  • E.g. x4 3, 6, 8, 10 // not allowed
  • You cannot assign one array wholesale to another.
  • E.g. x y // not allowed
  • You can use the initialization form only when
    defining the array.
  • E.g. int x4 3,6

8
Arrays
  • Rules on Array initialization
  • int x 3, 6, 8, 10 //okay
  • During initialization array size is optional.
  • If you dont put the size, the compiler will do
    it for you.

9
Strings
  • A string is a series of characters stored in
    consecutive bytes of memory.
  • E.g. Rottweiler, Rhodesian Ridgeback, Ori Pei
  • Can store in an array of char, with
    each character kept in its own array element.
  • char dogBreed7 O, r, i, , P, e,
    i
  • // not stored as a string
  • But C needs one additional step to store this
    as a string.
  • The null character \0, with ASCII code 0,
    should be added to the end of the string.
  • This serves to mark the end of the string.

10
Strings
  • E.g.
  • char dogBreed8 O, r, i, , P ,
    e, i, \0
  • //stored as a string
  • This null character plays a fundamental role in
    C strings.
  • Why do we need to add null character at the
    end of the string?
  • C has many built -in functions that handle
    strings, including those used by cout.
  • They all work by processing a string, character
    by character until they reach the null character.
  • E.g. cout
  • E.g. cout

11
Strings
  • Earlier method to initialize a string variable
    looks tedious.
  • Second method - another method for
    initializing strings.
  • E.g. 1 char fish6 shark //reserve one
    character //for the null character
  • The null character is automatically added to the
    string.
  • E.g. 2 char fish shark
  • The null character is automatically added to the
    string.
  • Compiler counts the number of characters (plus
    null character) for you.

Note Do not interchange a string constant
(double quotes) with a character constant (single
quotes).
12
Strings Functions
(Defined in string.h header file.)
  • strcat( s1,s2 ) - appends a copy of s2 to the end
    of string s1.
  • strlen( s ) - counts all the visible characters
    in the string s and return this count.
  • strcpy( s1, s2 ) copies string s2 to s1.
  • strcmp( s1, s2 ) returns
  • 0 if the strings s1 and s2 are the same
  • 0 if s1 is lexicographically greater than s2
  • Example

13
Cin and Strings
  • To read a string from the keyboard we must
    enter the null character. But it is impossible.
  • cin adds the null character to the string
    when it sees a white space character (space,
    tabs, carriage return).
  • Thus, cin reads just one word.

14
Line Oriented Input
  • getline( name, size ) takes to arguments
  • name name of the array which hold the
    string
  • size limit on the number of characters to be
    read
  • E.g. cin.getline ( text, 20 )
  • reads up to 19 characters, leaving room one
    element to hold the null character.
  • stops reading input when it reaches this
    numeric limit or when it reads carriage
    return, whichever comes first.

15
Line Oriented Input
  • cin.ignore (20, \n) takes to arguments
  • causes up to 20 characters in the input stream
    to be skipped
  • stops if the new line character is encountered.

16
2-D Arrays
  • Data that is in rows and columns is usually
    stored in 2-dimensional arrays
  • Declaring of 2-dimensional arrays
  • Need to specify no of rows and columns
  • int a3010 // int array of 30 rows and
    // 10 columns
  • char ticTacToeBoard33 // three rows
    //and three columns of chars

17
2-D Arrays
  • Unless specified, all initial values of arrays
    are garbage
  • specify initial values by enclosing each row in
    curly braces

char ticTacToeBoard 3 3 'x', 'x',
'o',
'o', 'o', 'x',
'x', 'o', ' '

18
Subscripting 2-D arrays
  • int a3010

?
a00
a01
a02
a09
?
a11
a10
a12
a19
?
a22
a29
a20
a21
?
?
a290
a291
a292
a299
Write a Comment
User Comments (0)
About PowerShow.com