0% found this document useful (0 votes)
30 views5 pages

(Lab No: 09) (Computer Programming) (Arrays)

The document contains 4 tasks demonstrating the use of arrays in C#. The first task prints the days of the week using a string array. The second task initializes an integer array with elements multiplied by 5. The third task checks if two arrays entered by the user are equal. The fourth task calculates the sum and average of numbers entered by the user and stored in a double array. Each task includes sample code and is attributed to student Abdul Qudoos.

Uploaded by

Malik Qudoos
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)
30 views5 pages

(Lab No: 09) (Computer Programming) (Arrays)

The document contains 4 tasks demonstrating the use of arrays in C#. The first task prints the days of the week using a string array. The second task initializes an integer array with elements multiplied by 5. The third task checks if two arrays entered by the user are equal. The fourth task calculates the sum and average of numbers entered by the user and stored in a double array. Each task includes sample code and is attributed to student Abdul Qudoos.

Uploaded by

Malik Qudoos
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/ 5

[Lab No: 09] [Computer Programming]

[Arrays]

Task No.1:
Make a program in C# to print name of Days Using Dense Array.

CODE:
string[] Days_Of_Week = { "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
for (int i = 0; i <= 6; i++)
{
Console.WriteLine(Days_Of_Week[i]);
}

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Task No.2:
Write a program, which creates an array of 20 elements of type integer and
initializes each of the elements with a value equals to the index of the element
multiplied by 5. Print the elements to the console.

CODE:
int[] arr = new int[20];

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


{
arr[i] = i * 7;
Console.WriteLine(arr[i]);
}

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Task No.3:
Write a program, which reads two arrays from the console and checks whether
they are equal (two arrays are equal when they are of equal length and all of
their elements, which have the same index, are equal).

CODE:
bool arraysEqual = true;

Console.Write("Enter lenght of first array: ");


int length = Int32.Parse(Console.ReadLine());

int[] arrA = new int[length];

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


{
Console.Write("Enter element {0}: ", i);
arrA[i] = Int32.Parse(Console.ReadLine());
}

Console.Write("\nEnter lenght of second array: ");

if (length != Int32.Parse(Console.ReadLine())) Console.WriteLine("\nArrays


have different lengths.");
else
{
int[] arrB = new int[length];

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


{
Console.Write("Enter element {0}: ", i);
arrB[i] = Int32.Parse(Console.ReadLine());
}

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


{
if (arrA[i] != arrB[i])
{
Console.WriteLine("\nArrays are different.");
arraysEqual = false;
break;
}
}

if (arraysEqual) Console.WriteLine("\nArrays are the same.");


}

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Task No.4:
Make a program in C# in which take 5 numbers from user and then give sum
and avg. of them. Using arrays.

CODE:
Console.WriteLine("For how many numbers you want to calculate average? ");
int numbersForAverage = int.Parse(Console.ReadLine());
double[] myArrayOfNumbers = new double[numbersForAverage];
Console.WriteLine("Input your numbers");
for (int i = 0; i < myArrayOfNumbers.Length; i++)
{
myArrayOfNumbers[i] = int.Parse(Console.ReadLine());
}
double sum = 0;
for (int j = 0; j < myArrayOfNumbers.Length; j++)
{
sum += myArrayOfNumbers[j];
}
double averageOfNumbers = sum / numbersForAverage;
Console.WriteLine("The average of your numbers is: {0}",
averageOfNumbers);
Console.ReadLine();

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086

You might also like