0% found this document useful (0 votes)
18 views18 pages

Dsa Lab 1 by Syed

The document describes tasks to work with one- and two-dimensional arrays in C#. It includes tasks to find pairs of elements that sum to 25, count even and odd numbers, calculate averages, and perform operations like addition, subtraction and multiplication on 2D matrices of user-defined size.

Uploaded by

mbinqasim71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views18 pages

Dsa Lab 1 by Syed

The document describes tasks to work with one- and two-dimensional arrays in C#. It includes tasks to find pairs of elements that sum to 25, count even and odd numbers, calculate averages, and perform operations like addition, subtraction and multiplication on 2D matrices of user-defined size.

Uploaded by

mbinqasim71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LAB 1 Data Structure and Algorithms

One- and Two-dimensional Arrays

Bahria University,
Karachi Campus

COURSE: CSL 221


DATA STRUCTURES AND ALGORITHM
TERM: FALL 2023, CLASS: BSE- 3(C)

Submitted By:

__SYED MUHAMMAD BIN QASIM______02-131222-


011___
(Name) (Reg. No.)

Submitted To:

Engr. Majid Kaleem /Engr. Ayesha Khan

Signed: Remarks: Score:

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

INDEX
SN DATE LAB LAB OBJECTIVE SIGN
O NO

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

SN DATE LAB LAB OBJECTIVE SIGN


O NO

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

Bahria University,
Karachi Campus

LAB EXPERIMENT NO.01


LIST OF TASKS
TASK OBJECTIVE
NO
1. Create an array A of length of 10 integers. Values ranging from 1 to 50.
1. Find all pairs of elements whose sum is 25.
2. Find the number of elements of A which are even, and the number of elements of A
which are odd.
3. Write a procedure which finds the average of the value of A.
2. Write a C# program that utilizes a 1D array to implement a simple inventory management system:
1: Inventory Setup
Create an array to store inventory items and initialize the item count.
2: Main Menu Loop
Implement the main menu loop for the inventory management system. This loop will repeatedly display
options to the user until they choose to exit.
3: Add Item
Implement the functionality to add an item to the inventory. Ask the user for the name of the item to add
it to the inventory array.
4: Remove Item
Implement the functionality to remove an item from the inventory. Ask the user for the name of the item
to remove and remove it from the inventory array if found.
5. Search Item
Implement the functionality to search for an item in the inventory. Ask the user for the name of the item
to search for and display whether it's in the inventory or not.
6: Display Inventory
Implement the functionality to display the current items in the inventory.
7: Exit Program
Implement the functionality to exit the inventory management system when the user chooses to exit.
3. Write a program which input 2 matrix of user defined rows and columns and perform
following operation:
a. Display/Print as a Matrix
b. Addition of Matrix
c. Subtraction of Matrix
d. matrix multiplication

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

e. Determinant
f. Inverse

Submitted On:
____10/10/23_ __

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

LAB 1
TASK 1: Find all pair of elements whose sum is 25.
SOLUTION:
namespace ConsoleApp5
{
internal class Program
{
static void Main(string[] args)
{

int[] A = { 10, 15, 12, 13, 2 };


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

for (int j = i + 1; j < A.Length; j++)


{
if (A[i] + A[j] == 25)
{
Console.WriteLine("The sum of pair is matched");
}
else
{
Console.WriteLine("The sum of pair is not matched");
}
}
}

Console.ReadLine();

}
}
}
OUTPUT:

TASK 2: Find the number of elements of A which are even, and the number of
elements of A which are odd.

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

SOLUTION:
namespace ConsoleApp5
{
internal class Program
{
static void Main(string[] args)
{
int[] A = { 46, 13, 6, 1, 56, 8, 2, 76 };
for (int i = 0; i < A.Length; i++)
{
if (A[i] % 2 == 0)
{
Console.WriteLine("Even value");
}
else
{ Console.WriteLine("Odd value"); }
}
}
}
}
OUTPUT:

TASK 3: Write a procedure which finds the average of the value of A.


SOLUTION:
namespace ConsoleApp5
{
internal class Program
{
static void Main(string[] args)
{
int a = 0;
int F = 0;
int[] A = { 99, 92, 96, 97, 94 };
for (int i = 0; i < A.Length; i++)
{
F = F + A[i];
}
a = F / 5;
Console.WriteLine("The Average Value of A: " + a);
}
}

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

}
OUTPUT:

2D ARRAYS
TASK 1: Write a program which input 2 matrix of user defined rows and columns
and perform following operation.
SOLUTION:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
internal class Program
{

static int row1;


static int col1;
static int row2;
static int col2;
static int[,] first = new int[row1, col1];
static int[,] second = new int[row2, col2];
static int[,] resultMul = new int[row1, col1];
static void Main(string[] args)
{
Console.WriteLine("***********************Operations on Matrices*************************");
Console.WriteLine(" ");
Console.WriteLine("Please enter the number of rows of matrix 1: ");
row1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of coloumns of Matrix 1: ");
col1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of rows of Matrix 2: ");
row2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of coloumns of Matrix 2: ");
col2 = Convert.ToInt32(Console.ReadLine());
if (col1 != row2)
{
Console.WriteLine("Please insert valid input!");
return;
}
else
{

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

Console.WriteLine("Please wait a minute, validating...");


}

first = new int[row1, col1];


second = new int[row2, col2];
resultMul = new int[row1, col1];

Console.WriteLine("\nEnter values for Matrix 1:");


for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
Console.Write("({0},{1}) = ", i + 1, j + 1);
first[i, j] = Convert.ToInt32(Console.ReadLine());
}
}

Console.WriteLine("\nEnter values for Matrix 2:");


for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
Console.Write("({0},{1}) = ", i + 1, j + 1);
second[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("");
Console.WriteLine("Please select any of the operation below: ");
Console.WriteLine("Press s to display both matrices.");
Console.WriteLine("Press y to Add both matrices.");
Console.WriteLine("Press e to Subtract both matrices.");
Console.WriteLine("Press d to Multiply both matrices.");
Console.WriteLine("Press m to take Determinant of both matrices.");
Console.WriteLine("Press b to take inverse of both matrices.");
char var = Convert.ToChar(Console.ReadLine());
switch (var)
{
case 's':
{
Display();
break;
}
case 'y':
{
Add();
break;
}
case 'e':
{
Subtract();
break;
}
case 'd':
{
Multiply();
break;

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

}
case 'm':
{
Determinant();
break;
}
case 'b':
{
Inverse();
break;
}
default:
{
Console.WriteLine("Invalid Input!");
break;
}
}
}
static void Display()
{
Console.WriteLine("\nFirst Matrix according to your given values:");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
Console.Write(first[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("\nSecond Matrix according to your given values:");
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
Console.Write(second[i, j] + "\t");
}
Console.WriteLine();
}
}
static void Add()
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
resultMul[i, j] = first[i, j] + second[i, j];
Console.Write(resultMul[i, j] + "\t");
}
Console.WriteLine();
}
}
static void Subtract()
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

{
resultMul[i, j] = first[i, j] - second[i, j];
Console.Write(resultMul[i, j] + "\t");
}
Console.WriteLine();
}
}
static void Multiply()
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
resultMul[i, j] = first[i, j] * second[i, j];
Console.Write(resultMul[i, j] + "\t");
}
Console.WriteLine();
}
}
static void Determinant()
{
Console.WriteLine("Only valid for 2x2 Matrix...");
int determinant1 = (first[0, 0] * first[1, 1]) - (first[0, 1] * first[1, 0]);
int determinant2 = (second[0, 0] * second[1, 1]) - (second[0, 1] * second[1, 0]);
Console.WriteLine("Determinant of first matrix is: " + determinant1);
Console.WriteLine("Determinant of second matrix is: " + determinant2);
}
static void Inverse()
{
double determinant1 = (first[0, 0] * first[1, 1]) - (first[0, 1] * first[1, 0]);
if (determinant1 == 0)
{
Console.WriteLine("Inverse of the matrix 1 is not possible because the determinant is singular");
}
else
{
double[,] inverse1 = new double[2, 2];
inverse1[0, 0] = first[1, 1] / determinant1;
inverse1[0, 1] = -first[0, 1] / determinant1;
inverse1[1, 0] = -first[1, 0] / determinant1;
inverse1[1, 1] = first[0, 0] / determinant1;

Console.WriteLine("Inverse of the first matrix is:");


for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(inverse1[i, j] + "\t");
}
Console.WriteLine();
}
}
double determinant2 = (second[0, 0] * second[1, 1]) - (second[0, 1] * second[1, 0]);
if (determinant2 == 0)
{
Console.WriteLine("Inverse of the matrix 2 is not possible because the determinant is singular");

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

}
else
{
double[,] inverse2 = new double[2, 2];
inverse2[0, 0] = second[1, 1] / determinant2;
inverse2[0, 1] = -second[0, 1] / determinant2;
inverse2[1, 0] = -second[1, 0] / determinant2;
inverse2[1, 1] = second[0, 0] / determinant2;

Console.WriteLine("Inverse of the matrix 2 is:");


for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(inverse2[i, j] + "\t");
}
Console.WriteLine();
}
}
}
}

}
OUTPUT:

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

TASK 2: Write a C# program that utilizes a 1D array to implement a simple


inventory management system
SOLUTION:
class InventoryManagementSystem
{
static string[] inventory = new string[100];
static int[] quantities = new int[100];
static int itemCount = 0;

static void Main()


{
bool exit = false;
while (!exit)
{
Console.WriteLine("Inventory Management System");
Console.WriteLine("1. Add Item");
Console.WriteLine("2. Remove Item");
Console.WriteLine("3. List Inventory");
Console.WriteLine("4. Check Quantity");
Console.WriteLine("5. Exit");
Console.Write("Enter your choice: ");

int choice;
if (int.TryParse(Console.ReadLine(), out choice))

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

{
switch (choice)
{
case 1:
AddItem();
break;
case 2:
RemoveItem();
break;
case 3:
ListInventory();
break;
case 4:
CheckQuantity();
break;
case 5:
exit = true;
break;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}
else
{
Console.WriteLine("Invalid input. Please enter a number.");
}
}
}

static void AddItem()


{
if (itemCount < inventory.Length)
{
Console.Write("Enter the item name: ");
string itemName = Console.ReadLine();
Console.Write("Enter the quantity: ");
if (int.TryParse(Console.ReadLine(), out int quantity))
{
inventory[itemCount] = itemName;
quantities[itemCount] = quantity;
itemCount++;
Console.WriteLine("Item added to inventory.");
}
else
{
Console.WriteLine("Invalid quantity. Please enter a number.");
}
}
else
{
Console.WriteLine("Inventory is full. Cannot add more items.");
}
}

static void RemoveItem()

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

{
Console.Write("Enter the item name to remove: ");
string itemName = Console.ReadLine();
int index = Array.IndexOf(inventory, itemName);
if (index >= 0)
{
for (int i = index; i < itemCount - 1; i++)
{
inventory[i] = inventory[i + 1];
quantities[i] = quantities[i + 1];
}
itemCount--;
Console.WriteLine("Item removed from inventory.");
}
else
{
Console.WriteLine("Item not found in inventory.");
}
}

static void ListInventory()


{
Console.WriteLine("Inventory List:");
for (int i = 0; i < itemCount; i++)
{
Console.WriteLine($"{inventory[i]} - Quantity: {quantities[i]}");
}
}

static void CheckQuantity()


{
Console.Write("Enter the item name to check quantity: ");
string itemName = Console.ReadLine();
int index = Array.IndexOf(inventory, itemName);
if (index >= 0)
{
Console.WriteLine($"Quantity of {itemName}: {quantities[index]}");
}
else
{
Console.WriteLine("Item not found in inventory.");
}
}
}

}
}
OUTPUT:

SYED MUHAMMAD BIN QASIM 02-131222-011


LAB 1 Data Structure and Algorithms
One- and Two-dimensional Arrays

SYED MUHAMMAD BIN QASIM 02-131222-011

You might also like