QN1.
Write a program to create two multidimensional arrays of same size. Accept value
from user and store them in first array. Now copy all the elements of first array
are second array and print output.
using System;
class Program
{ static void Main()
{
// Get the size of the multidimensional array from the user
Console.Write("Enter the number of rows: ");
int rows = int.Parse(Console.ReadLine());
Console.Write("Enter the number of columns: ");
int cols = int.Parse(Console.ReadLine());
// Create the first array
int[,] firstArray = new int[rows, cols];
// Accept values from the user and store them in the first array
Console.WriteLine("Enter values for the first array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write("Enter value for element ({i + 1},{j + 1}): ");
firstArray[i, j] = int.Parse(Console.ReadLine());
}
}
// Create the second array with the same size as the first array
int[,] secondArray = new int[rows, cols];
// Copy all elements from the first array to the second array
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
secondArray[i, j] = firstArray[i, j];
}
}
// Print the elements of the second array
Console.WriteLine("\nElements of the second array:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(secondArray[i, j] + " ");
}
Console.WriteLine();
}
}
}
QN4.
Reverse a string using array.
Hint: accept string value and store in a string variable str. Then convert str
into array as follow:
char[] ch=str.ToCharArray;
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string inputString = Console.ReadLine();
// Reverse the string
string reversedString = ReverseString(inputString);
// Convert the reversed string to a char array
char[] charArray = reversedString.ToCharArray();
// Print the reversed string and the char array
Console.WriteLine("\nReversed String: " + reversedString);
Console.WriteLine("Char Array:");
foreach (char ch in charArray)
{
Console.Write(ch + " ");
}
}
static string ReverseString(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
QN2
:Write a program to create three multidimensional arrays of same size. Accept
value from user and store them in first array. Now copy all the elements of first
array are second array and print output.
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of rows and columns: ");
int size = int.Parse(Console.ReadLine());
// Create the first array and accept values from the user
int[,] firstArray = new int[size, size];
Console.WriteLine("Enter values for the first array:");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Console.Write(
"Enter value for element ({i + 1},{j + 1}): ");
firstArray[i, j] = int.Parse(Console.ReadLine());
}
}
// Create the second and third arrays and copy elements from the first array
int[,] secondArray = new int[size, size];
int[,] thirdArray = new int[size, size];
Array.Copy(firstArray, secondArray, firstArray.Length);
Array.Copy(firstArray, thirdArray, firstArray.Length);
// Print the elements of the second array
Console.WriteLine("\nElements of the second array:");
PrintArray(secondArray);
// Print the elements of the third array
Console.WriteLine("\nElements of the third array:");
PrintArray(thirdArray);
}
static void PrintArray(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
}
QN3.
Write a program to copy one array’s elements to another array without using array
function.
using System;
class Program
{
static void Main()
{
Console.Write("Enter the size of the array: ");
int size = int.Parse(Console.ReadLine());
// Create the first array and accept values from the user
int[] firstArray = new int[size];
Console.WriteLine("Enter values for the first array:");
for (int i = 0; i < size; i++)
{
Console.Write("Enter value for element {i + 1}: ");
firstArray[i] = int.Parse(Console.ReadLine());
}
// Create the second array with the same size
int[] secondArray = new int[size];
// Copy elements from the first array to the second array
for (int i = 0; i < size; i++)
{
secondArray[i] = firstArray[i];
}
// Print the elements of the second array
Console.WriteLine("\nElements of the second array:");
for (int i = 0; i < size; i++)
{
Console.Write(secondArray[i] + " ");
}
}
}