VISUAL C - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

VISUAL C

Description:

Jagged arrays are arrays whose elements are also arrays. A jagged array (also referred to as an array of arrays) is an array (single ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 49
Provided by: birgl
Category:
Tags: visual | jagged

less

Transcript and Presenter's Notes

Title: VISUAL C


1
VISUAL C
  • Arrays

2
Arrays
  • So far, we have needed to store only a single
    value in a variable. This has applied to both
    strings and integers. Suppose we need to store
    and process the names of all students enrolled in
    a programming course. We could declare a new
    variable to store each name. To keep track of our
    name variables, we could call all of them
    StudentName and append an incremental number to
    each to differentiate them.
  • String StudentName1 "Aneesha"
  • String StudentName2 "Celine"
  • String StudentName3 "Zaeem"
  • String StudentName4 "Tess"

3
Arrays
  • This does not provide a solution to the problem.
    We can't write code to iterate over the list and
    batch-process data. This is where the Array data
    structure comes in handy.
  • An array is able to store several values of the
    same type without requiring you to declare a
    separate name for each variable. Here is our
    student names example implemented as an array of
    strings
  • string StudentNames new string3

4
Arrays
  • This creates an array that is able to store 3
    values, where index value starts from 0. We can
    now assign the values to the array elements.
    Square braces are used to reference the elements
    within the array.
  • StudentNames0 "Aneesha"
  • StudentNames1 "Celine"
  • StudentNames2 "Zaeem"

5
Arrays
  • This is how an array is defined
  • dataType variableName
  • The data type of an array could be an int, char,
    or a string. Square braces are placed immediately
    after the data type.
  • Arrays must be initialized before a value can be
    assigned to individual elements.

6
Arrays
  • byte byteValues
  • int counts
  • string words
  • Object objList
  • Customer customers

7
Arrays
  • The following code will cause a syntax error
    because the 10th element in the TestScores array
    is not initialized before a value is assigned
  • int TestScores
  • TestScores9 4
  • We fix this problem by using the keyword new and
    initializing the 10 elements within the array
  • int TestScores new int 10
  • We could initialize and assign values at the same
    time
  • int TestScores 1,2,3,4,5

8
Arrays
  • Initializing the array is what provides C with
    the boundary information on the array. Because
    arrays are essentially just specialized classes,
    you initialize them with the new keyword, as
    shown in the following samples
  • byte byteArray new byte21
  • int counters new int99
  • string wordList new string21

9
Arrays
  • If you know the data that will be in the arrays
    at design time, you can pre-initialize the arrays
    with data as shown in the following samples
  • byte byteArray new byte 1, 2, 3
  • byte byteArray2 1, 2, 3
  • string wordList "The", "Quick", "Fox"

10
Arrays
  • Property Description
  • IsFixedSize Indicates whether the array is fixed
    size
  • IsReadOnly Indicates whether the array is
    read-only
  • Length Returns the total of elements in the
    array
  • Rank Returns the number of dimensions of the
    array

11
Arrays
  • Method Description
  • Clear Clears out all array elements and sets
    them to the default value for the data type
    (for example, 0 for integers, null for object
    types, and so on).
  • ConvertAll Converts all elements within the array
    from one type to another.
  • Exists Determines if an element exists in the
    array based on a Boolean test function.

12
Arrays
  • Method Description
  • Find Searches the array for an element that is
    matched based on a Boolean test function.
    Static.
  • FindAll Returns a list of all matches where the
    Boolean test function returns true. Static.

13
Arrays
  • Method Description
  • FindLast Returns the last occurrence of an array
    element where the Boolean test function
    returns true. Static.
  • Initialize Invokes the default constructor of the
    type of the array to initialize each
    element.
  • Resize Static method used to resize a given
    array.

14
Arrays
  • Now that we have the names stored in an array, we
    can use a for loop to iterate over the array and
    do some data processing, as in the following
    example. The Length property of the array returns
    the number of elements stored in the array.
  • for (int i 0 ilt StudentNames.Length i)
  • textBox1.Text textBox1.Text
    (StudentNamesi)

15
Arrays
  • We can also use a foreach loop to iterate over
    the array, as follows. The advantage of using a
    foreach loop is that we don't need to explicitly
    define the length of the array. The foreach loop
    simply iterates over all elements in the array.
  • foreach (string i in StudentNames)
  • textBox1.Text textBox1.Text i

16
Arrays
  • using System
  • using System.Collections.Generic
  • using System.Text
  • namespace Array1D
  • class Program
  • static void Main(string args)
  • string daysOfWeek "Mon", "Tue", "Wed",
    "Thu", "Fri", "Sat"
  • string productNames "Blue Pen", "Red Pen",
    "Blue Eraser", "Red Eraser"

17
Arrays
  • for (int x 0 x lt daysOfWeek.Length x)
  • textBox1.Text textBox1.Text daysOfWeekx
    " "
  • string blueProducts Array.FindAll(productNames
    , IsBlueProduct)
  • foreach (string product in blueProducts)
  • textBox2.Text textBox2.Text product " "
  • FindAll Returns a list of all matches where the
    Boolean test function (IsBlueProduct in this
    case) returns true.

18
Arrays
  • static bool IsBlueProduct(string productName)
  • if (productName.ToUpper().Contains("BLUE"))
  • return true
  • else
  • return false

19
Sorting Arrays
  • The Sort() method arranges the elements in an
    array in either ascending or alphabetical order
  • String names "Aneesha", "Zaeem",
    "Celine")
  • //Sorting the contents of an array
  • // in alphabetical order of a to z
  • Array.Sort(StudentNames)
  • We could also reverse the contents of an array by
    using the Reverse() method
  • Array.Reverse(StudentNames)
  • // If used after sort result will be z to a
    order

20
Multidimensional Arrays
  • This is how a 2-dimensional array is defined
  • dataType variableName new
    stringrow,column
  • To create an array of 3 rows with 2 columns where
    rows will specify students and columns will
    specify name and lastname we can use
  • String StudentInfo new string3,2
  • StudentInfo0,0 Ali
  • StudentInfo0,1 Veli
  • StudentInfo1,0 49
  • StudentInfo1,1 Geçer
  • StudentInfo2,0 Kalir
  • StudentInfo2,1 50

21
Multidimensional Arrays
22
Multidimensional Arrays
  • You declare a multidimensional array by using a
    comma to indicate the presence of multiple
    dimensions in the array, as shown in the
    following example
  • byte, twoDByteArray
  • // two-dimensional array of bytes
  • SpaceShip,, objectsInSpace
  • // stores ships that exist in various
    coordinates
  • PlayerPiece, checkerBoard
  • // stores pieces on a checkerboard

23
Character Matrix Ex.
  • This is how a two-dimensional character array is
    declared and initialized
  • char, WordfinderMatrix new char4,4
  • We can reference individual elements by their
    index. The following code sets the element in row
    1 and column 1 to the letter 'C'
  • WordfinderMatrix 0, 0 'c'
  • textBox1.Text WordfinderMatrix0,
    0.ToString()

24
Character Matrix Ex.
  • We will have to use two for loops to populate the
    matrix. The first for loop will use a counter
    variable called i to loop through the 4 rows of a
    matrix. The second loop will be placed within the
    first loop and use the counter variable j to loop
    through the 4 columns in the matrix. Placing a
    loop within another loop is known as a nested
    loop.

25
Character Matrix Ex.
  • The following code fills all of the elements
    within the matrix with the letter 'A'
  • for (int i 0 i lt 4 i)
  • for (int j 0 j lt 4 j)
  • WordfinderMatrix i, jl 'A'

26
Random Numbers
  • Random numbers are easily generated in C. The
    Random class contains a method called Next(),
    which can generate a random number between 0 and
    a maximum number that is specified by you. The
    upper limit of the randomly generated number is
    passed to the Next() method. In most cases,
    you'll need to randomly select an item from a
    set. Examples include displaying random images
    and quotes.
  • Let's use the Random class to generate a number
    between 0 and 10. We'll need to create a Random
    object, call the Next() method, and pass 10 to
    it
  • Random randomObj new Random()
  • int randomNo randomObj.Next(10)

27
Character Matrix Random Ex.
  • We can populate the matrix with random characters
    by generating a random number between 1 and 26,
    adding 65 to it, and then casting the number to
    the char data type, as shown in the following
    example. We add 65 to the random number to
    convert it to an uppercase ASCII character.

28
Character Matrix Random Ex.
  • Random randomNo new Random()
  • for (int i 0 i lt 4 i)
  • for (int j 0 j lt 4 j)
  • WordfinderMatrix i, j (char)(
    randomNo.Next(26) 65)

29
3X4 Array Ex.
  • int, productCounts new int4,3
  • // set the product counts for each product
    for each month
  • for (int x 0 x lt 3 x)
  • for (int y 0 y lt4 y)
  • productCountsx, y x y // some
    arbitrary number

30
3X4 Array Ex.
  • Textbox placement
  • textBox1 textBox2 textBox3 textBox4
    textBox5 textBox6 textBox7
    textBox8 textBox9 textBox10 textBox11
    textBox12

31
4X3 Array Ex.
  • textBox1.Text productCounts0,
    0.ToString()
  • textBox2.Text productCounts0,
    1.ToString ()
  • textBox3.Text productCounts0,
    2.ToString ()
  • textBox4.Text productCounts0,
    3.ToString ()
  • textBox5.Text productCounts1,
    0.ToString()
  • textBox6.Text productCounts1,
    1.ToString()
  • textBox7.Text productCounts1,
    2.ToString()
  • textBox8.Text productCounts1,
    3.ToString()
  • textBox9.Text productCounts2,
    0.ToString()
  • textBox10.Text productCounts2,
    1.ToString()
  • textBox11.Text productCounts2,
    2.ToString()
  • textBox12.Text productCounts2,
    3.ToString()

32
4X3 Array Ex.
  • Result
  • 0 1 2 3
  • 1 2 3 4
  • 2 3 4 5

33
3X4 Array Ex. (TextBox Array)
  • TextBox MyArray new TextBox12
  • MyArray0 textBox1
  • MyArray1 textBox2
  • MyArray2 textBox3
  • MyArray3 textBox4
  • MyArray4 textBox5
  • MyArray5 textBox6
  • MyArray6 textBox7
  • MyArray7 textBox8
  • MyArray8 textBox9
  • MyArray9 textBox10
  • MyArray10 textBox11
  • MyArray11 textBox12
  • int, productCounts new int3,4
  • for (int x 0 x lt 3 x)
  • for (int y 0 y lt4 y)
  • productCountsx, y x y
  • for (int i 0 i lt 12 i)
  • MyArrayi.Text productCountsi/4,
    i4.ToString()

34
3X4 Array Ex. (TextBox Control)
  • int, productCounts new int3,4
  • int index 0
  • int sum 0
  • // set the product counts for each product
    for each month
  • for (int x 0 x lt 3 x)
  • for (int y 0 y lt4 y)
  • index 4 x y 1
  • sum x y
  • this.Controls"textBox"
    index.ToString().Text sum.ToString()
  • productCountsx, y sum

35
TextBox Array Creation
  • public Form1()
  • InitializeComponent()
  • textboxolustur()
  • public void textboxolustur()
  • TextBox txtbx new TextBox()
  • for (int i 0 i lt 12 i)
  • txtbx new TextBox ()
  • txtbx.Name "txtbx" i.ToString()
  • Point p new System.Drawing.Point(10,150
    25 i)
  • txtbx.Location p
  • this.Controls.Add(txtbx)
  • private void button2_Click(object sender,
    EventArgs e)
  • foreach (Control c in this.Controls)
  • for (int i 0 i lt 12 i)
  • if (c.Name "txtbx" i.ToString())
  • c.Text i.ToString()
  • break

36
4X3 Array Ex. (Full TextBox Array Create)
  • private void Form1_Load(object sender, EventArgs
    e)
  • textboxolustur()
  • public void textboxolustur()
  • TextBox txtbx new TextBox()
  • int sayac 1
  • for (int i 0 i lt 4 i, sayac)
  • for (int j 0 j lt 3 j)
  • txtbx new TextBox()
  • txtbx.Name "txtbx"
    sayac.ToString()
  • Point p new
    System.Drawing.Point(10 j 120 ,10 25 i)
  • txtbx.Location p
  • this.Controls.Add(txtbx)
  • txtbx.Text (i
    j).ToString()

37
4X3 Array Ex. (Full TextBox Array Create)
38
Jagged Arrays
  • The preceding section dealt with rectangular
    arrays, in which the number of columns (elements)
    would be the same for each row, giving you data
    that is shaped like a matrix. Jagged arrays are
    arrays whose elements are also arrays.
  • A jagged array (also referred to as an array of
    arrays) is an array (single-dimension or
    multidimensional) in which the elements are
    themselves arrays. This allows each element of
    the array to be an array with a different size.

39
Jagged Arrays
  • There are many ways to declare jagged arrays,
    some of which take advantage of the same
    initializer shortcuts that are available for
    other array types.
  • The following few lines of code declare a jagged
    array without doing any initialization
  • string jaggedStrings
  • int jaggedInts
  • Customer jaggedCustomers

40
Jagged Arrays
  • int jaggedInts new int2
  • jaggedInts0 new int10
  • jaggedInts1 new int20
  • int ji2 new int2
  • ji20 new int 1, 3, 5, 7, 9
  • ji21 new int 2, 4, 6

41
Jagged Array Example
  • int ji2 new int2
  • ji20 new int 1, 3, 5, 7, 9
  • ji21 new int 2, 4, 6

42
Jagged Array Example
  • for (int i 0 i lt ji2.Length i)
  • for (int j 0 j lt ji2i.Length j)
  • textBox1.Text textBox1.Text
  • " / " ji2ij.ToString()
  • textBox1.Text textBox1.Text " - "

43
Jagged Array Example
  • / 1 / 3 / 5 / 7 / 9 - / 2 / 4 / 6 -

44
Jagged Arrays
  • // doesn't need the initial 'new int' ,
  • // compiler can infer it
  • int ji3
  • new int 1, 3, 5, 7, 9 ,
  • new int 2, 4, 6

45
Jagged Arrays
  • // 'new int ' used explicitly, though it
    isn't
  • // needed.
  • int ji4 new int
  • new int 1, 3, 5, 7, 9 ,
  • new int 2, 4, 6

46
Jagged Arrays
  • As confusing as it might seem, you can mix jagged
    and rectangular arrays to create some powerful
    (and often difficult to decipher) structures.
  • Take a look at the following code, which creates
    a jagged array in which each element is a
    different-size two-dimensional array

47
Jagged Arrays
  • int , mixedJagged new int ,
  • new int, 0,1, 2,3, 4,5 ,
  • new int, 9,8, 7,6, 5,4, 3,2, 1,0
  • textBox1.Text mixedJagged11, 1.ToString()
  • What would this print? ......

48
Jagged Arrays
  • 6
  • mixedJagged11, 1
  • new int, 0,1, 2,3, 4,5 ,
  • new int, 9,8, 7,6, 5,4, 3,2, 1,0
Write a Comment
User Comments (0)
About PowerShow.com