SlideShare a Scribd company logo
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

More Related Content

What's hot

Lecture 5
Lecture 5Lecture 5
Lecture 5
Soran University
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
Terry Yoast
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
ahmed22dg
 
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 ch12
Terry Yoast
 
Chap02
Chap02Chap02
Chap02
Terry Yoast
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
Terry Yoast
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
Terry Yoast
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
swatisinghal
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
Mahmoud Ouf
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
Terry Yoast
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
Terry Yoast
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
Kopi Maheswaran
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
Terry Yoast
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
Prashant odhavani
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
Terry Yoast
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformations
miso_uam
 
Chapter2
Chapter2Chapter2
Chapter2
Anees999
 

What's hot (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
 

Viewers also liked

Arrays In General
Arrays In GeneralArrays In General
Arrays In General
martha leon
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
Hock Leng PUAH
 
Arrays C#
Arrays C#Arrays C#
C
CC
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
Gopal Ji Singh
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
Shehrevar Davierwala
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
Jm Ramos
 
Sql ppt
Sql pptSql ppt
Sql ppt
Anuja Lad
 
5 arrays
5   arrays5   arrays
5 arrays
Tuan Ngo
 
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
eVideoTuition
 
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
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
Mandy Suzanne
 

Viewers also liked (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 to Csc153 chapter 06

Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
ahmed hmed
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.ppt
Farhan646135
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
Sandeep Singh
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
JulioRecaldeLara1
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
tangadhurai
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdf
ssuser598883
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
Techbuddy Consulting Pvt. Ltd.
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
Savitribai Phule Pune University
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
FarHanWasif1
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
PankajKumar497975
 
Net framework
Net frameworkNet framework
Net framework
Abhishek Mukherjee
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.pdf
Arvind Pathak
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
Terry Yoast
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)
Dr. Ahmed Al Zaidy
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
Fisnik Doko
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
ssusere19c741
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Soran University
 

Similar to 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
 

Recently uploaded

GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 

Recently uploaded (20)

GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 

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