Sonzaschool
Rudi

Sekondari ya Kawaida · Kidato cha Tatu · Amali · Electronics and Computer

Computer Programming

Implementing single-dimensional and multi-dimensional arrays

takriban dakika 4 kusoma

Majadiliano
Mada za sehemu hiiDeveloping programs using functions and arraysMada 3
  1. Constructing reusable functions
  2. Implementing single-dimensional and multi-dimensional arrays
  3. Using control structures to traverse and process arrays

An array is a collection of elements of the same data type stored under a single variable name. Instead of creating separate variables for each value (like score1, score2, score3...), you can use an array to store all scores together and access them using an index.

Declaring an Array

To declare an array in C#, use the following syntax:

datatype[] arrayName = new datatype[size];

For example, to create an array that holds 5 integers:

int[] marks = new int[5];

Initialising an Array

There are two ways to initialise an array:

Method 1: Declare first, then assign values

int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

Method 2: Declare and initialise at the same time

string[] fruits = { "Mango", "Banana", "Papaya", "Orange" };

When you use this method, you do not need to specify the size—the compiler automatically determines it from the number of items.

Accessing Array Elements

Array elements are accessed using an index. C# uses zero-based indexing, meaning the first element is at index 0, the second at index 1, and so on.

string[] vegetables = { "Kisamvu", "Mchicha", "Chinese", "Mchunga" };
Console.WriteLine(vegetables[1]);  // Output: Mchicha
Console.WriteLine(vegetables[3]);  // Output: Mchunga

If you try to access an index that does not exist, the program will throw an IndexOutOfRangeException error.

Getting the Size of an Array

Use the Length property to find how many elements an array contains:

int[] numbers = { 10, 20, 30 };
Console.WriteLine(numbers.Length);  // Output: 3

Looping Through an Array

You can use a for loop to iterate through all elements of an array:

int[] numbers = { 10, 20, 30, 40, 50 };

for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine("Element at index " + i + ": " + numbers[i]);
}

This prints each element by using the loop counter i as the index. The loop automatically stops when i equals the array's length.

Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa

Tathmini ya mchakato

The student should be able to:

  • Declare and initialize arrays using correct syntax.
  • Access, modify, and process elements within single-dimensional and multidimensional arrays.

Tathmini ya matokeo

Arrays are implemented accurately and meet the task requirements.

Tathmini ya maarifa

Methods used: The trainee should explain how to:

  • Declare and initialize single-dimensional and multi-dimensional arrays.
  • Access and manipulate array elements using loops and conditional statements.

Principles: The student should explain the principle of:

  • Data storage and organization using arrays.
  • Indexing in single-dimensional and multidimensional arrays for efficient data access.
  • Iterative processes for array traversal and manipulation.

Theories: The trainee should explain: The trainee should explain:

  • How arrays represent structured data in programming.
  • The role of arrays in efficient data handling and storage.

Circumstantial knowledge:

Detailed knowledge about:

  • Common errors in array operations, such as offby-one errors or out-of-bound accesses, and their solutions.
  • Practical applications of arrays in event-driven programming and real-world scenarios.

Swali

What is the correct syntax to declare an integer array named marks that can hold 5 elements in C#?

Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.

Ingia ili kufanya mazoezi

Mwalimu

Umekwama? Niulize chochote kuhusu mada hii.

Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.

Ingia ili kuuliza

Majadiliano

Hakuna maswali bado

Ingia ili kuuliza
Inapakia majadiliano…