SlideShare una empresa de Scribd logo
1 de 40
Microsoft Visual C# 2010
       Fourth Edition



        Chapter 6
       Using Arrays
Objectives
• Declare an array and assign values to array
  elements
• Access array elements
• Search an array using a loop
• Use the BinarySearch(), Sort(), and
  Reverse() methods
• Use multidimensional arrays
• Learn about array issues in GUI programs



Microsoft Visual C# 2010, Fourth Edition         2
Declaring an Array and Assigning
         Values to Array Elements
• Array
    – List of data items that all have the same data type and
      the same name
    – Each item is distinguished from the others by an index
• Declaring and creating an array
        double[] sales;
        sales = new double[20];
• new operator
    – Used to create objects
• You can change the size of an array

Microsoft Visual C# 2010, Fourth Edition                   3
Declaring an Array and Assigning
      Values to Array Elements (cont'd.)
• Array element
    – Each object in an array
• Subscript (or index)
    – Integer contained within square brackets that indicates
      the position of one of an array’s elements
    – Array’s elements are numbered beginning with 0
• “Off by one” error
    – Occurs when you forget that the first element in an
      array is element 0


Microsoft Visual C# 2010, Fourth Edition                    4
Declaring an Array and Assigning
      Values to Array Elements (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   5
Declaring an Array and Assigning
      Values to Array Elements (cont'd.)
• Assigning a value to an array element
          sales[0] = 2100.00;
• Printing an element value
          Console.WriteLine(sales[19]);




Microsoft Visual C# 2010, Fourth Edition   6
Initializing an Array

• In C#, arrays are objects
    – Arrays are instances of a class named
      System.Array
• Initializing objects
    – Numeric fields: 0
    – Character fields: ‘u0000’ or null
    – bool fields: false
• Initializer list
    – List of values provided for an array


Microsoft Visual C# 2010, Fourth Edition         7
Initializing an Array (cont'd.)

• Initializer list examples
    int[] myScores = new int[5] {100, 76, 88,
                     100, 90};
    int[] myScores = new int[] {100, 76, 88,
                     100, 90};
    int[] myScores = {100, 76, 88, 100, 90};




Microsoft Visual C# 2010, Fourth Edition        8
Accessing Array Elements

• The power of arrays becomes apparent when you
  use subscripts
    – Can be variables rather than constant values
• Using a loop to perform arithmetic on each element
        for (int sub = 0; sub < 5; ++sub)
          myScores[sub] += 3;




Microsoft Visual C# 2010, Fourth Edition               9
Using the Length Property

• Length property
    – Member of the System.Array class
    – Automatically holds an array’s length
• Examples
        int[] myScores = {100, 76, 88, 100, 90};
        Console.WriteLine(“Array size is {0}”,
                          myScores.Length);
        for (int x = 0; x < myScores.Length; ++x)
        Console.WriteLine(myScores[x]);



Microsoft Visual C# 2010, Fourth Edition            10
Using foreach

• foreach statement
    – Cycles through every array element without using a
      subscript
    – Uses a temporary iteration variable
          • Automatically holds each array value in turn
• Example
    double[] payRate = {6.00, 7.35, 8.12, 12.45, 22.22};
    foreach(double money in payRate)
    Console.WriteLine(“{0}”, money.ToString(“C”));



Microsoft Visual C# 2010, Fourth Edition                   11
Using foreach (cont'd.)

• Used under the following circumstances
    – When you want to access every array element
    – Since the iteration variable is read-only
          • You cannot assign a value to it




Microsoft Visual C# 2010, Fourth Edition            12
Using foreach with Enumerations




Microsoft Visual C# 2010, Fourth Edition   13
Searching an Array Using a Loop

• Searching options
    – Using a for loop
    – Using a while loop




Microsoft Visual C# 2010, Fourth Edition   14
Using a for Loop to Search an Array

• Use a for statement to loop through the array
    – Set a Boolean variable to true when a match is found
• Solution is valid even with parallel arrays




Microsoft Visual C# 2010, Fourth Edition               15
Using a for Loop to Search an Array
              (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   16
Using a while Loop to Search an
                  Array
• Use a while loop to search for a match




Microsoft Visual C# 2010, Fourth Edition   17
Microsoft Visual C# 2010, Fourth Edition   18
Searching an Array for a Range Match
• Range match
    – Determines the pair of limiting values between which a
      value falls




Microsoft Visual C# 2010, Fourth Edition                 19
Searching an Array for a Range Match
              (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   20
Using the BinarySearch(),
     Sort(),and Reverse() Methods
• System.Array class contains a variety of useful,
  built-in methods that can search, sort, and
  manipulate array elements




Microsoft Visual C# 2010, Fourth Edition             21
Using the BinarySearch() Method

• BinarySearch() method
    – Finds a requested value in a sorted array
    – Member of the System.Array class
• Do not use BinarySearch() under these
  circumstances
    – If your array items are not arranged in ascending order
    – If your array holds duplicate values and you want to
      find all of them
    – If you want to find a range match rather than an exact
      match

Microsoft Visual C# 2010, Fourth Edition                  22
Using the BinarySearch() Method
               (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   23
Using the Sort() Method

• Sort() method
    – Arranges array items in ascending order
    – Use it by passing the array name to Array.Sort()




Microsoft Visual C# 2010, Fourth Edition             24
Using the Sort() Method (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   25
Using the Reverse() Method

• Reverse() method
    – Reverses the order of items in an array
    – Element that starts in position 0 is relocated to position
      Length – 1
    – Use it by passing the array name to the method




Microsoft Visual C# 2010, Fourth Edition                     26
Using the Reverse() Method
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   27
Using Multidimensional Arrays

• One-dimensional or single-dimensional array
    – Picture as a column of values
    – Elements can be accessed using a single subscript
• Multidimensional arrays
    – Require multiple subscripts to access the array
      elements
    – Two-dimensional arrays
          • Have two or more columns of values for each row
          • Also called rectangular array, matrix, or a table


Microsoft Visual C# 2010, Fourth Edition                        28
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   29
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   30
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   31
Using Multidimensional Arrays
                     (cont'd.)
• Three-dimensional array




Microsoft Visual C# 2010, Fourth Edition   32
Using Multidimensional Arrays
                     (cont'd.)
• Jagged array
    – One-dimensional array in which each element is
      another one-dimensional array
    – Each row can be a different length




Microsoft Visual C# 2010, Fourth Edition               33
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   34
Array Issues in GUI Programs

• If array values change based on user input
   – Array must be stored outside any method that reacts to
     the user’s event




 Microsoft Visual C# 2010, Fourth Edition                35
Array Issues in GUI Programs (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   36
You Do It

• Activities to explore
   – Creating and Using an Array
   – Using the Sort() and Reverse() Methods




 Microsoft Visual C# 2010, Fourth Edition         37
Summary
• An array is a list of data items
     – All of which have the same type and the same name
     – Items are distinguished using a subscript or index
• In C#, arrays are objects of a class named
  System.Array
• The power of arrays becomes apparent when you
  begin to use subscripts
• Subscript you use remains in the range of 0
  through length -1


Microsoft Visual C# 2010, Fourth Edition                38
Summary (cont'd.)

• foreach statement cycles through every array
  element without using subscripts
• You can compare a variable to a list of values in an
  array
• You can create parallel arrays to more easily
  perform a range match
• The BinarySearch() method finds a requested
  value in a sorted array
• The Sort() method arranges array items in
  ascending order

Microsoft Visual C# 2010, Fourth Edition             39
Summary (cont'd.)

• The Reverse() method reverses the order of
  items in an array
• Multidimensional arrays require multiple subscripts
  to access the array elements
• Types of multidimensional arrays
     – Two-dimensional arrays (rectangular arrays)
     – Three-dimensional arrays
     – Jagged arrays



Microsoft Visual C# 2010, Fourth Edition             40

Más contenido relacionado

La actualidad más candente

9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#Prasanna Kumar SM
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02Terry Yoast
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- Cswatisinghal
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04Terry Yoast
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003Prashant odhavani
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04Terry Yoast
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformationsmiso_uam
 

La actualidad más candente (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Chap02
Chap02Chap02
Chap02
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformations
 
Chapter2
Chapter2Chapter2
Chapter2
 

Destacado (12)

Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
C
CC
C
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
5 arrays
5   arrays5   arrays
5 arrays
 
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick LearningOracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar a Csc153 chapter 06

Similar a Csc153 chapter 06 (20)

Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.ppt
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdf
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Net framework
Net frameworkNet framework
Net framework
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.pdf
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Csc153 chapter 06

  • 1. Microsoft Visual C# 2010 Fourth Edition Chapter 6 Using Arrays
  • 2. Objectives • Declare an array and assign values to array elements • Access array elements • Search an array using a loop • Use the BinarySearch(), Sort(), and Reverse() methods • Use multidimensional arrays • Learn about array issues in GUI programs Microsoft Visual C# 2010, Fourth Edition 2
  • 3. Declaring an Array and Assigning Values to Array Elements • Array – List of data items that all have the same data type and the same name – Each item is distinguished from the others by an index • Declaring and creating an array double[] sales; sales = new double[20]; • new operator – Used to create objects • You can change the size of an array Microsoft Visual C# 2010, Fourth Edition 3
  • 4. Declaring an Array and Assigning Values to Array Elements (cont'd.) • Array element – Each object in an array • Subscript (or index) – Integer contained within square brackets that indicates the position of one of an array’s elements – Array’s elements are numbered beginning with 0 • “Off by one” error – Occurs when you forget that the first element in an array is element 0 Microsoft Visual C# 2010, Fourth Edition 4
  • 5. Declaring an Array and Assigning Values to Array Elements (cont'd.) Microsoft Visual C# 2010, Fourth Edition 5
  • 6. Declaring an Array and Assigning Values to Array Elements (cont'd.) • Assigning a value to an array element sales[0] = 2100.00; • Printing an element value Console.WriteLine(sales[19]); Microsoft Visual C# 2010, Fourth Edition 6
  • 7. Initializing an Array • In C#, arrays are objects – Arrays are instances of a class named System.Array • Initializing objects – Numeric fields: 0 – Character fields: ‘u0000’ or null – bool fields: false • Initializer list – List of values provided for an array Microsoft Visual C# 2010, Fourth Edition 7
  • 8. Initializing an Array (cont'd.) • Initializer list examples int[] myScores = new int[5] {100, 76, 88, 100, 90}; int[] myScores = new int[] {100, 76, 88, 100, 90}; int[] myScores = {100, 76, 88, 100, 90}; Microsoft Visual C# 2010, Fourth Edition 8
  • 9. Accessing Array Elements • The power of arrays becomes apparent when you use subscripts – Can be variables rather than constant values • Using a loop to perform arithmetic on each element for (int sub = 0; sub < 5; ++sub) myScores[sub] += 3; Microsoft Visual C# 2010, Fourth Edition 9
  • 10. Using the Length Property • Length property – Member of the System.Array class – Automatically holds an array’s length • Examples int[] myScores = {100, 76, 88, 100, 90}; Console.WriteLine(“Array size is {0}”, myScores.Length); for (int x = 0; x < myScores.Length; ++x) Console.WriteLine(myScores[x]); Microsoft Visual C# 2010, Fourth Edition 10
  • 11. Using foreach • foreach statement – Cycles through every array element without using a subscript – Uses a temporary iteration variable • Automatically holds each array value in turn • Example double[] payRate = {6.00, 7.35, 8.12, 12.45, 22.22}; foreach(double money in payRate) Console.WriteLine(“{0}”, money.ToString(“C”)); Microsoft Visual C# 2010, Fourth Edition 11
  • 12. Using foreach (cont'd.) • Used under the following circumstances – When you want to access every array element – Since the iteration variable is read-only • You cannot assign a value to it Microsoft Visual C# 2010, Fourth Edition 12
  • 13. Using foreach with Enumerations Microsoft Visual C# 2010, Fourth Edition 13
  • 14. Searching an Array Using a Loop • Searching options – Using a for loop – Using a while loop Microsoft Visual C# 2010, Fourth Edition 14
  • 15. Using a for Loop to Search an Array • Use a for statement to loop through the array – Set a Boolean variable to true when a match is found • Solution is valid even with parallel arrays Microsoft Visual C# 2010, Fourth Edition 15
  • 16. Using a for Loop to Search an Array (cont'd.) Microsoft Visual C# 2010, Fourth Edition 16
  • 17. Using a while Loop to Search an Array • Use a while loop to search for a match Microsoft Visual C# 2010, Fourth Edition 17
  • 18. Microsoft Visual C# 2010, Fourth Edition 18
  • 19. Searching an Array for a Range Match • Range match – Determines the pair of limiting values between which a value falls Microsoft Visual C# 2010, Fourth Edition 19
  • 20. Searching an Array for a Range Match (cont'd.) Microsoft Visual C# 2010, Fourth Edition 20
  • 21. Using the BinarySearch(), Sort(),and Reverse() Methods • System.Array class contains a variety of useful, built-in methods that can search, sort, and manipulate array elements Microsoft Visual C# 2010, Fourth Edition 21
  • 22. Using the BinarySearch() Method • BinarySearch() method – Finds a requested value in a sorted array – Member of the System.Array class • Do not use BinarySearch() under these circumstances – If your array items are not arranged in ascending order – If your array holds duplicate values and you want to find all of them – If you want to find a range match rather than an exact match Microsoft Visual C# 2010, Fourth Edition 22
  • 23. Using the BinarySearch() Method (cont'd.) Microsoft Visual C# 2010, Fourth Edition 23
  • 24. Using the Sort() Method • Sort() method – Arranges array items in ascending order – Use it by passing the array name to Array.Sort() Microsoft Visual C# 2010, Fourth Edition 24
  • 25. Using the Sort() Method (cont'd.) Microsoft Visual C# 2010, Fourth Edition 25
  • 26. Using the Reverse() Method • Reverse() method – Reverses the order of items in an array – Element that starts in position 0 is relocated to position Length – 1 – Use it by passing the array name to the method Microsoft Visual C# 2010, Fourth Edition 26
  • 27. Using the Reverse() Method (cont'd.) Microsoft Visual C# 2010, Fourth Edition 27
  • 28. Using Multidimensional Arrays • One-dimensional or single-dimensional array – Picture as a column of values – Elements can be accessed using a single subscript • Multidimensional arrays – Require multiple subscripts to access the array elements – Two-dimensional arrays • Have two or more columns of values for each row • Also called rectangular array, matrix, or a table Microsoft Visual C# 2010, Fourth Edition 28
  • 29. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 29
  • 30. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 30
  • 31. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 31
  • 32. Using Multidimensional Arrays (cont'd.) • Three-dimensional array Microsoft Visual C# 2010, Fourth Edition 32
  • 33. Using Multidimensional Arrays (cont'd.) • Jagged array – One-dimensional array in which each element is another one-dimensional array – Each row can be a different length Microsoft Visual C# 2010, Fourth Edition 33
  • 34. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 34
  • 35. Array Issues in GUI Programs • If array values change based on user input – Array must be stored outside any method that reacts to the user’s event Microsoft Visual C# 2010, Fourth Edition 35
  • 36. Array Issues in GUI Programs (cont'd.) Microsoft Visual C# 2010, Fourth Edition 36
  • 37. You Do It • Activities to explore – Creating and Using an Array – Using the Sort() and Reverse() Methods Microsoft Visual C# 2010, Fourth Edition 37
  • 38. Summary • An array is a list of data items – All of which have the same type and the same name – Items are distinguished using a subscript or index • In C#, arrays are objects of a class named System.Array • The power of arrays becomes apparent when you begin to use subscripts • Subscript you use remains in the range of 0 through length -1 Microsoft Visual C# 2010, Fourth Edition 38
  • 39. Summary (cont'd.) • foreach statement cycles through every array element without using subscripts • You can compare a variable to a list of values in an array • You can create parallel arrays to more easily perform a range match • The BinarySearch() method finds a requested value in a sorted array • The Sort() method arranges array items in ascending order Microsoft Visual C# 2010, Fourth Edition 39
  • 40. Summary (cont'd.) • The Reverse() method reverses the order of items in an array • Multidimensional arrays require multiple subscripts to access the array elements • Types of multidimensional arrays – Two-dimensional arrays (rectangular arrays) – Three-dimensional arrays – Jagged arrays Microsoft Visual C# 2010, Fourth Edition 40