PROGRAMMING IN C#
Module 3: Statements and Operators
Module 4: Programming Constructs
Module 5: Array
Lab Guide for Lab2
Session Objectives
At the end of this session, you will able to understand:
Part 1 – Getting started (30 minutes)
Exercise 1: foreach statement
Step 1: Add a console based project ‘ForEach’ to the solution
Step 2: Right click on project ForEach -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘ForEach.cs’
Step 4: Replace the code in ‘ForEach.cs’ with the given code
using System;
class ForEach
{
static void Main(string[] args)
{
DateTime now = DateTime.Now;
Random rand = new Random((int)now.Millisecond);
int[] Arr = new int[10];
for (int x = 0; x < Arr.Length; ++x)
{
Arr[x] = rand.Next() % 100;
}
int Total = 0;
Console.Write("Array values are ");
foreach (int val in Arr)
{
Total += val;
Console.Write(val + ", ");
}
Console.WriteLine("\nAnd the average is {0,0:F1}",
(double)Total / (double)Arr.Length);
Console.ReadLine();
}
}
Step 5: Select menu File -> Save to save the file
Step 6: Select Build -> Build ‘ForEach’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following
Exercise 2: Array of Object
Step 1: Add a console based project ‘ObjectArray’ to the solution
Step 2: Right click on project ObjectArray -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘ObjectArray.cs’
using System;
class Employee
{
private int empID;
// constructor
public Employee(int empID)
{
this.empID = empID;
}
public override string ToString()
{
return empID.ToString();
}
}
class ObjectArray
{
public void Run()
{
int[] intArray;
Employee[] empArray;
intArray = new int[5];
empArray = new Employee[3];
// populate the array
for (int i = 0; i < empArray.Length; i++)
{
empArray[i] = new Employee(i + 5);
}
Console.WriteLine("The int array...");
for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine(intArray[i].ToString());
}
Console.WriteLine("\nThe employee array...");
for (int i = 0; i < empArray.Length; i++)
{
Console.WriteLine(empArray[i].ToString());
}
}
static void Main(string[] args)
{
ObjectArray arr = new ObjectArray();
arr.Run();
Console.ReadLine();
}
}
Step 5: Select menu File -> Save to save the file
Step 6: Select Build -> Build ‘ObjectArray’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following
Exercise 2: Array Class
Step 1: Add a console based project ‘ArrayClass’ to the solution
Step 2: Right click on project ArrayClass -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘ArrayClass.cs’
Step 4: Replace the code in ‘ArrayClass.cs’ with the given code
using System;
class ArrayClass
{
static void Main(string[] args)
{
int[] Arr = new int[12] { 29, 82, 42, 46, 54, 65, 50, 42, 5, 94, 19, 34 };
Console.WriteLine("The first occurrence of 42 is at index "
+ Array.IndexOf(Arr, 42));
Console.WriteLine("The last occurrence of 42 is at index "
+ Array.LastIndexOf(Arr, 42));
int x = 0;
while ((x = Array.IndexOf(Arr, 42, x)) >= 0)
{
Console.WriteLine("42 found at index " + x);
++x;
}
x = Arr.Length - 1;
while ((x = Array.LastIndexOf(Arr, 42, x)) >= 0)
{
Console.WriteLine("42 found at index " + x);
--x;
}
Console.WriteLine("Array that befor sorted");
for (int i = 0; i < Arr.Length; i++)
{
Console.WriteLine("{0} : {1}", i + 1, Arr[i]);
}
Array.Sort(Arr);
Console.WriteLine("Array that after sorted");
for (int i = 0; i < Arr.Length; i++)
{
Console.WriteLine("{0} : {1}", i + 1, Arr[i]);
}
Array.Reverse(Arr);
Console.WriteLine("Array that after reserse");
for (int i = 0; i < Arr.Length; i++)
{
Console.WriteLine("{0} : {1}", i + 1, Arr[i]);
}
Console.ReadLine();
}
}
Step 5: Select menu File -> Save to save the file
Step 6: Select Build -> Build ‘ArrayClass’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following
إرسال تعليق