Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays

Description:

Department of Computer Science & Engineering. University of Nevada, Reno. Spring 2006 ... reads in a single character at a time. Will read any character. ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • Character Arrays and Strings

Alina Solovyova-Vincent Department of Computer
Science Engineering University of Nevada,
Reno Spring 2006
2
Worksheet
  • char my_word5 c, l, a, s, s
  • Is it a character array or a string? Why?
  • How can you display it to the screen?

for(i0 ilt5 i) coutltlt my_wordiltlt
3
Worksheet
  • char my_word6 c, l, a, s, s, \0
  • Is it a character array or a string? Why?
  • How can you display it to the screen?

coutltlt my_word
4
Extraction Operator gtgt (char array)
  • char word3
  • coutltlt Please enter a 3-letter word // cat
  • for ( int i0 i lt 3 i)
  • cin gtgt wordi
  • coutltltYou have entered
  • for ( i0 i lt 3 i)
  • cout ltlt wordi // cat

5
Extraction Operator gtgt (string)
  • char word4
  • coutltlt Please enter a 3-letter word // cat
  • cin gtgt word
  • coutltltYou have entered
  • cout ltlt word // cat

6
Extraction Operator gtgt (string)
  • char word4
  • coutltlt Please enter a 3-letter word // cat
  • for ( int i0 i lt 3 i)
  • cin gtgt wordi
  • word3\0 //convert character array into
    string
  • coutltltYou have entered
  • cout ltlt word // cat

7
Extraction Operator gtgt (char array)
  • char phrase10
  • coutltlt Please enter a sentence// This is
    hard
  • for ( int i0 i lt 10 i)
  • cin gtgt phrasei
  • coutltltYou have entered
  • for ( i0 i lt 10 i)
  • cout ltlt phrasei

// Thisishard
8
Extraction Operator gtgt (string)
  • char phrase10
  • coutltlt Please enter a sentence// This is
    hard
  • cin gtgt phrase
  • coutltltYou have entered
  • cout ltlt phrase

// This
9
getline( )
  • char my_string10
  • cin.getline(my_string, 10) //user enters hi
  • my_string0h
  • my_string1i
  • my_string2\0
  • my_string3 garbage
  • my_string9 garbage

coutltltmy_string Displayed hi
What is the length of this string???
10
getline( )
  • char my_string10
  • cin.getline(my_string, 10) //user enters I am
    happy
  • my_string0I
  • my_string1
  • my_string2a
  • my_string3m
  • my_string8p
  • my_string9\0

coutltltmy_string Displayed I am happ
11
Function get( ) - page 104
  • cin.get( )
  • reads in a single character at a time. Will read
    any character.
  • if you need to read the ENTER key, a space, or
    the tab key, you cannot use cin. You must use
    cin.get because cin.get will read any character.
  • Example
  • char c1
  • cin.get(c1)

12
Function get( )
  • char phrase12
  • coutltlt Please enter a sentence// This is
    hard
  • for ( int i0 i lt 12 i)
  • cin.get(phrasei)
  • coutltltYou have entered
  • for ( i0 i lt 12 i)
  • cout ltlt phrasei

// This is hard
13
Initialization of Strings
  • char my_word6 c, l, a, s, s, \0
  • char my_word6class
  • char my_word very interesting class

14
Worksheet
  • Write a code segment that will prompt the
    user for his first name and his last name and
    display to the screen
  • Hello, first_name last_name.
  • I.e. If user enters John Smith, the program
    should display
  • Hello, John Smith

15
  • char first_name20, last_name20
  • coutltlt\nEnter your first and last name
  • cingtgtfirst_name gtgt last_name
  • coutltlt\nHello, ltlt first_name ltlt ltltlast_name
  • OR
  • char name50
  • coutltlt\nEnter your first and last name
  • cin.getline(name, 50)
  • coutltlt\nHello, ltlt name

16
Worksheet
  • Write a code segment that will prompt the
    user for his first name and his last name and
    display to the screen
  • Hello, last_name first_name.
  • I.e. If user enters John Smith, the program
    should display
  • Hello, Smith John.

17
Worksheet
  • Write a function that will accept two
    strings and return true if they are equal in
    length and false if they are not.

18
Worksheet
  • Write a function that will accept an integer
    array, its size and a code integer. The
    function should find the location of that code
    integer in the array and return its position
    (index) in the array or 999 if the code was not
    found in the array.
Write a Comment
User Comments (0)
About PowerShow.com