Skip to main content

Class 12 Array(Remaining Parts)

Jagged Array in C#


What is a Jagged Array?

A Jagged Array is an array of arrays. Each element of the

main array is another array.

A jagged array is an array whose elements are arrays.

Unlike a multidimensional array,

each row can have a different number of elements.

Simple Diagram

Each row contains a different number of elements.

Features of Jagged Array

It is an array of arrays.
Each row can have a different length.
It is useful for storing uneven or irregular data.
It uses memory efficiently because each row is created only
with the required size.

Declaration

A jagged array is declared using two square brackets ([][]).

int[][] jaggedArr = new int[3][];

Explanation

int[][] → Jagged array of integers.
new int[3][] → Creates a main array with 3 rows.
The rows are created, but their sizes are not assigned yet.

Initializing a Jagged Array

Method 1: Initialize Row by Row

int[][] jaggedArr = new int[3][];

jaggedArr[0] = new int[2];
jaggedArr[1] = new int[4];
jaggedArr[2] = new int[3];

Structure

Row

        Number of Elements

Row 0    

            2

Row 1

            4

Row 2

            3

Each row has its own size.

Accessing Elements

Elements are accessed using two indexes.

Syntax

jaggedArr[row][column]

Examples

jaggedArr[0][0];   // 1
jaggedArr[1][2];   // 5
jaggedArr[2][1];   // 8

Meaning

First index (row) selects the row.
Second index (column) selects the element inside that row.

Traversing a Jagged Array

A jagged array is traversed using nested loops.

The outer loop visits each row.
The inner loop visits each element in the selected row.

complete Example Program

using System;

class JaggedArray
{
    static void Main()
    {
        int[][] jaggedArr = new int[4][];

        jaggedArr[0] = new int[] {1, 2, 3, 4};
        jaggedArr[1] = new int[] {11, 34, 67};
        jaggedArr[2] = new int[] {89, 23};
        jaggedArr[3] = new int[] {0, 45, 78, 53, 99};

        for (int i = 0; i < jaggedArr.Length; i++)
        {
            Console.Write("Row " + i + ": ");

            for (int j = 0; j < jaggedArr[i].Length; j++)
            {
                Console.Write(jaggedArr[i][j] + " ");
            }

            Console.WriteLine();
        }
    }
}

Output

Row 0: 1 2 3 4
Row 1: 11 34 67
Row 2: 89 23
Row 3: 0 45 78 53 99

Jagged Array vs Multidimensional Array

Jagged Array

Multidimensional Array

Array of arrays.

Single array with rows and 

   columns.

Rows can have different lengths.

 All rows have the same 

    number of columns.

Declared as int[][].

  Declared as int[,].

Flexible for irregular data.

  Suitable for regular 

table-like data.

 

Param Array in C# 

A Param Array is a method parameter declared using the params keyword. It allows a method to accept a variable number of arguments of the same data type.

Definition: A param array is a special parameter that lets a method receive zero, one, or many values without creating an array separately.

Syntax

returnType MethodName(params dataType[] parameterName)
{
    // Method body
}

Explanation

  • params → Keyword used for variable arguments.

  • dataType[] → Must be a single-dimensional array.

  • parameterName → Name of the parameter array.

How Param Array Works

When a method is called, all the values passed are automatically stored in an array.

Visual Diagram

Method Call

Sum(10, 20, 30);

Inside the Method

arr[] = {10, 20, 30}

Example Program

using System;

class Program
{
    static int Sum(params int[] numbers)
    {
        int total = 0;

        foreach (int num in numbers)
        {
            total += num;
        }

        return total;
    }

    static void Main()
    {
        Console.WriteLine(Sum(10, 20, 30));
        Console.WriteLine(Sum(5, 15));
        Console.WriteLine(Sum());
    }
}

Comments

Popular posts from this blog

Unit 2 Operating System

  Unit- 2  Process and Process Scheduling 2.1 Introduce Process, Program and Process Life Cycle  Process Process is something that is currently under execution. So, an active program can be called a Process. Examples: ●        Opening a web browser to search something on the internet — the browser becomes a process. ●        Launching a music player to enjoy your favorite tunes — the music player is also a process. In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. Modern operating systems support multithreading , meaning a process can have multiple threads running concurrently .   A Process has various attributes associated with it. Some of the attributes of a Process are: ●          Process Id: Every process will be given a unique id that identifies the process...

Unit-1 Introduction to C#.NET (Class 12)

  What is .NET Framework? The .NET Framework is a software development platform developed by Microsoft. It provides tools and libraries to build and run Windows applications, web services, and web apps. It gives tools and libraries that make it easier to write programs. It also helps the computer run those programs safely and efficiently. Microsoft started working on .NET Framework in the late 1990s . The first version, .NET Framework 1.0 , was released in 2002 .   The .NET Framework is made up of several important components: 1.       Common Language Runtime (CLR) is the core engine that runs your program. It converts your code into machine code that the computer can understand, manages memory, handles errors, and ensures that your program runs safely. 2.       The Class Library is a large collection of ready-made code that helps you perform common tasks like working with files, databases, graphics, the i...

Introduction to Software Engineering (Class 12)

 Introduction to Software Engineering Software engineering is the branch of computer science that deals with the design, development, testing, and maintenance of software applications. Software engineers apply engineering principles and knowledge of programming languages to build software solutions for end users. IEEE, in its standard 610.12-1990, defines software engineering as the application of a systematic, disciplined, which is a computable approach for the development, operation, and maintenance of software. Boehm defines software engineering, which involves, ‘the practical application of scientific knowledge to the creative design and building of computer programs. It also includes associated documentation needed for developing, operating, and maintaining them.’ Importance of Software Engineering Reduces complexity Large software systems are divided into smaller, manageable modules. Each module is developed and solved independently, ...