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

1st Level | struct. Prog.

| Lab 1
Lab#1: Arrays

Program#1: Write a program to get the maximum & minimum number of an array, and then
print its elements inversely.

using System;

namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*************Array Program************");
Console.Write("Enter the size of an array : ");
int size = int.Parse(Console.ReadLine());
int[] arry = new int[size];
for (int i = 0; i < arry.Length; i++)
{
Console.Write("Enter element {0} : ", i + 1);
arry[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("** Array Elements **");
for (int i = 0; i < arry.Length; i++)
{
Console.Write(arry[i]+ " ");
}
int max = arry[0];
int min = arry[0];
for (int i = 1; i < arry.Length; i++)
{
if (arry[i] > max)
{
max = arry[i];
}
if (arry[i] < min)
{
min = arry[i];
}
}
Console.WriteLine("\nMax element is : {0}", max);
Console.WriteLine("Min element is : {0}", min);
for (int i = arry.Length - 1; i >= 0; i--)
{
Console.WriteLine("Element {0}", (arry.Length - i) + " : " + arry[i]);

}
}
}

Page | 1
1st Level | struct. Prog. | Lab 1

Output:

Program#2: Write a C# Code to get the number of even and odd elements in an array.
using System;

namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*************Array Prog************");
Console.Write("Enter the size of an array : ");
int size = int.Parse(Console.ReadLine());
int[] arry = new int[size];
for (int i = 0; i < arry.Length; i++)
{
Console.Write("Enter element {0} : ", i + 1);
arry[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("** Array Elements **");
for (int i = 0; i < arry.Length; i++)
{
Console.Write(arry[i] + " ");
}
int count_odd = 0, count_even = 0;
for (int i = 0; i < arry.Length; i++)
{
if (arry[i] % 2 == 0)
{
count_even++;

Page | 2
1st Level | struct. Prog. | Lab 1
}
else
{
count_odd++;
}
}
Console.WriteLine("\nnumber of evens : {0}\nnumber of odds : {1}", count_even,
count_odd);

}
}
}

Output:

Program#3: Using 2D array4*5 write a C# program to get


the following output:

Page | 3
1st Level | struct. Prog. | Lab 1

namespace TwoDimensionalArayDemo
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[4, 5];
int a = 0;
//printing the values of 2d array using foreach loop
//It will print the default values as we are not assigning
//any values to the array
foreach (int i in arr)
{
Console.Write(i + " ");
}
Console.WriteLine("\n");
//assigning values to the array by using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
a += 5;
arr[i, j] = a;
}
}
//printing the values of array by using nested for loop
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ");
}
}
Console.ReadKey();
}
}
}

Program#4: Write a C# program to find the product of two matrices a[n][k] and b[k][m].

using System;
namespace MatrixMultiplicationDemo
{
class Example
{
static void Main(string[] args)
{
int m = 2, n = 3, p = 3, q = 3, i, j;
int[,] a = { { 1, 4, 2 }, { 2, 5, 1 } };
int[,] b = { { 3, 4, 2 }, { 3, 5, 7 }, { 1, 2, 1 } };
Console.WriteLine("Matrix a:");
for (i = 0; i < m; i++)
{

Page | 4
1st Level | struct. Prog. | Lab 1
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Matrix b:");
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{
Console.Write(b[i, j] + " ");
}
Console.WriteLine();
}
if (n != p)
{
Console.WriteLine("Matrix multiplication not possible");
}
else
{
int[,] c = new int[m, q];
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
c[i, j] = 0;
for (int k = 0; k < n; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
Console.WriteLine("The product of the two matrices is :");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
}
}
}
}

Page | 5
1st Level | struct. Prog. | Lab 1
Output:

Assignment #1:

Write a program reads a matrix A[4][4] of real numbers, and then generates an array of four
elements, the first element is the sum of the diagonal elements of the matrix, the second is the sum
of the upper triangular elements, the third is the sum of the lower triangular elements, and the forth
is the sum of the secondary diagonal elements.

**************************

Page | 6

You might also like