Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Question – Create a array list in c# in which you can perform

different function of array list


Co-1 Bl-6

using System.Collections;
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
var arrlist1 = new ArrayList();
arrlist1.Add (1);
arrlist1.Add (2.5);
arrlist1.Add ("Anshul Pundir");
foreach(var item in arrlist1)
{
Console.WriteLine(item);

}
Console.ReadKey();

}
}
}
Question – write a program for jagged array in c# ?
CO-1 BL-6
using System;

class JaggedArrayExample
{
static void Main()
{

int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[] { 1, 2, 3 };


jaggedArray[1] = new int[] { 4, 5, 6, 7 };
jaggedArray[2] = new int[] { 8, 9 };

Console.WriteLine("Jagged Array Elements:");

for (int i = 0; i < jaggedArray.Length; i++)


{
Console.Write($"Row {i + 1}: ");

for (int j = 0; j < jaggedArray[i].Length; j++)


{
Console.Write(jaggedArray[i][j] + " ");
}

Console.WriteLine();
}

Console.WriteLine("\nAccessing Individual Elements:");


Console.WriteLine("jaggedArray[1][2]: " + jaggedArray[1][2]);
Console.WriteLine("name:-Anshul Pundir\nclass MCA II(B) \nroll no 13");

Console.ReadLine();
}
}
Question – write a program in c# for 3x3 matrix and its transpose ?
CO-1 BL-6
using System;

class MatrixTranspose
{
static void Main()
{
// Declare and initialize a 3x3 matrix
int[,] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

Console.WriteLine("Original Matrix:");
PrintMatrix(matrix);

// Transpose the matrix


int[,] transposedMatrix = TransposeMatrix(matrix);

Console.WriteLine("\nTransposed Matrix:");
PrintMatrix(transposedMatrix);

// Pause to see the output


Console.ReadLine();
}

// Function to transpose a 3x3 matrix


static int[,] TransposeMatrix(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);

int[,] result = new int[cols, rows];

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
result[j, i] = matrix[i, j];
}
}

return result;
}

// Function to print a matrix


static void PrintMatrix(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("name – Anshul Pundir \nclass = MCA II \nRoll no - 13");
}
}

You might also like