methodological_instructions (4)

  • doc
  • 02.05.2020
Публикация на сайте для учителей

Публикация педагогических разработок

Бесплатное участие. Свидетельство автора сразу.
Мгновенные 10 документов в портфолио.

Иконка файла материала methodological_instructions (4).doc

Methodological Instructions

Theme: Use of strings

Aim: Use of procedures and functions for processing strings

Assessment criteria

Knowledge

·     Know how to find Length of string

 

Understanding

·     How to output the 2D string array elements

 

Application

·     Write code for processing the strings using functions

·     Write code for processing the strings using procedures

 

Language objectives

Student is able to describe 2D array of string.

For instance, for loop operator is used to work with 2D string array elements.

Also, student can justify the purpose of function or procedure used for processing string.

Vocabulary and terminology specific to the subject:

String element, string array, for loop

Useful expressions for dialogs and letters:

Well, to work with 2D array of strings … .

For example, each element of 2D string array … .

As a matter of fact, the stings are used in the wide variety of cases to  … .

 

Theory

 

2D array. A glowing platform appears at the water's edge. The platform is solid. You step onto it. Suddenly an entire dimension is illuminated—a new world is revealed.

In this 2D world, an element is addressed with X and Y. Consider now C#. 2D arrays can store any element type. Jagged arrays can also represent our 2D space.Jagged Arrays

Let us begin. Here we show a two-dimensional string array. We initialize it. Then we use the indexing syntax to access all elements and display their values.

Tip:To access a two-dimensional array element, please use the syntax array[0, 0]. Each dimension is indexed starting at zero.

Result:The program creates a 2x2 string array and then prints out all 4 elements (with no loops).

Based on: .NET (2019)
 
C# program that creates 2D array
 
using System;
 
class Program
{
    static void Main()
    {
        // ... Create 2D array of strings.
        string[,] array = new string[,]
        {
            {"cat", "dog"},
            {"bird", "fish"},
        };
        // ... Print out values.
        Console.WriteLine(array[0, 0]);
        Console.WriteLine(array[0, 1]);
        Console.WriteLine(array[1, 0]);
        Console.WriteLine(array[1, 1]);
    }
}
 
Output
 
cat
dog
bird
fish

Debugger. Above we declare a 2D array. The syntax is somewhat confusing, with curly brackets and commas. We can examine the code in the Visual Studio debugger.

Info:The compiler sees the string[,] array as a string[2, 2] array. It inferred the size (good job, compiler).

GetUpperBound. This method receives the highest index of the specified rank (passed as an argument). It returns an int. This is probably not best for a simple 2D array.

C# program that uses GetUpperBound
 
using System;
 
class Program
{
    static void Main()
    {
        string[,] codes = new string[,]
        {
            {"AA", "BB"},
            {"CC", "DD"}
        };
 
        // Get the upper bound.
        // ... Use for-loop over rows.
        for (int i = 0; i <= codes.GetUpperBound(0); i++)
        {
            string s1 = codes[i, 0];
            string s2 = codes[i, 1];
            Console.WriteLine("{0}, {1}", s1, s2);
        }
    }
}
 
Output
 
AA, BB
CC, DD

Length-based loop. The fastest method for a 2D array is to do some arithmetic. In this example, there are five rows. GetUpperBound(0) will return 4.

And:If we take Length, which is 10, and divide by 2, we get 5. We can iterate until we reach 5.

C# program that uses length-based loop
 
using System;
 
class Program
{
    static void Main()
    {
        string[,] words = new string[,]
        {
            {"ONE", "TWO"},
            {"THREE", "FOUR"},
            {"FIVE", "SIX"}
        };
 
        // Loop based on length.
        // ... Assumes each subarray is two elements long.
        for (int i = 0; i < words.Length / 2; i++)
        {
            string s1 = words[i, 0];
            string s2 = words[i, 1];
            Console.WriteLine("{0}, {1}", s1, s2);
        }
    }
}
 
Output
 
ONE, TWO
THREE, FOUR
FIVE, SIX

GetUpperBound, int example. We cache array bounds in local variables for better performance and clarity. Here we get the two dimensions of the array and iterate through them.

C# program that uses int array, GetUpperBound twice
 
using System;
 
class Program
{
    static void Main()
    {
        int[,] codes = new int[,]
        {
            {200, 400},
            {2000, 4176},
            {20000, 40000}
        };
 
        // Get all bounds before looping.
        int bound0 = codes.GetUpperBound(0);
        int bound1 = codes.GetUpperBound(1);
        // ... Loop over bounds.
        for (int i = 0; i <= bound0; i++)
        {
            for (int x = 0; x <= bound1; x++)
            {
                // Display the element at these indexes.
                Console.WriteLine(codes[i, x]);
            }
            Console.WriteLine();
        }
    }
}
 
Output
 
200
400
 
2000
4176
 
20000
40000

 

П. Questions for self-assessment.

What is the 2D string array in C#?

How to declare 2D string array?

How to initialize 2D string array elements?

 

Visual Aids and Materials links.

1.      Slides

2.      https://www.dotnetperls.com/2d

 

Students' Practical Activities:

Students must be able:

·     Write code for processing the strings using functions

·     Write code for processing the strings using procedures


Скачано с www.znanio.ru

Посмотрите также