0% found this document useful (0 votes)
41 views33 pages

Name:Kiran Naz Student I.D:11422: LAB 9 Examples & Exercises

The document contains 11 examples and 4 questions related to arrays in C#. The examples demonstrate how to declare, initialize, access, modify, iterate through, and sort integer and string arrays. The questions involve prompting the user for input to populate arrays, calculating statistics like highest/lowest values from random numbers in an array, counting product codes in an array, and tracking salsa jar sales data in arrays.

Uploaded by

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

Name:Kiran Naz Student I.D:11422: LAB 9 Examples & Exercises

The document contains 11 examples and 4 questions related to arrays in C#. The examples demonstrate how to declare, initialize, access, modify, iterate through, and sort integer and string arrays. The questions involve prompting the user for input to populate arrays, calculating statistics like highest/lowest values from random numbers in an array, counting product codes in an array, and tracking salsa jar sales data in arrays.

Uploaded by

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

Name:Kiran Naz

Student I.D:11422
LAB 9 Examples & Exercises:
Example 9.1
int[] FirstArray;

FirstArray = new int[3];

Console.Write(FirstArray[0] + " ");

Console.Write(FirstArray[1] + " ");

Console.Write(FirstArray[2] + " ");


Example 9.2
int[] FirstArray;

FirstArray = new int[3] {1,2,3};

Console.Write(FirstArray[0] + " ");

Console.Write(FirstArray[1] + " ");

Console.Write(FirstArray[2] + " ");


Example 9.3
int[] FirstArray = { 1, 2, 3, 4, 5 };

FirstArray[2] = 33;

Console.Write(FirstArray[0] + " ");

Console.Write(FirstArray[1] + " ");

Console.Write(FirstArray[2] + " ");

Console.Write(FirstArray[3] + " ");

Console.Write(FirstArray[4] + " ");


Example 9.4
string[] username = { "Kiran", "Mehriin", "Asma" };

Console.WriteLine(username[0] + "\t" + username[1] + "\t" +


username[2]);

username[2] = "Sana";

Console.Write(username[0] + "\t" + username[1] + "\t" +


username[2]);
Example 9.5
int[] FirstArray = new int[5];

int length = FirstArray.Length;

for (int i = 0; i < length; i++)

FirstArray[i] = i + 1;

for (int j = 0; j < FirstArray.Length; j++)

Console.WriteLine(FirstArray[j]);

}
Example 9.6
Console.WriteLine("How many players name you want to enter");

int N = Convert.ToInt32(Console.ReadLine());

string[] playersName = new string[N];

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

Console.Write("Enter player " + (i+1) + " Name: ");

playersName[i] = Console.ReadLine();

Console.WriteLine("Players Name");

Console.WriteLine("------------");

for (int j = 0; j < playersName.Length; j++)

Console.WriteLine(playersName[j]);

}
Example 9.7
int[] Numbers = { 11, 22, 33, 44, 55 };

foreach (var item in Numbers)

Console.Write(item + " ");

}
Example 9.8
Console.WriteLine("How many players name you want to enter");

int N = Convert.ToInt32(Console.ReadLine());

string[] playersName = new string[N];

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

Console.Write("Enter player " + (i + 1) + " Name: ");

playersName[i] = Console.ReadLine();

Console.WriteLine("Players Name");

Console.WriteLine("------------");

foreach (var item in playersName)

Console.WriteLine(item);

}
Example 9.9
int[] numbers = { 1, 2, 3, 4, 9, 2 };

foreach (int item in numbers)

Console.Write(item + " ");

Console.WriteLine("Enter value which you want to remove");

int value = Convert.ToInt32(Console.ReadLine());

bool Flag = false;

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

if (numbers[i] == value)

Flag = true;

break;

if (Flag)

int[] Temp = new int[numbers.Length - 1];

for (int i = 0, j = 0; i < numbers.Length; i++)

if (numbers[i] != value)

Temp[j] = numbers[i];
j++;

numbers = Temp;

else

Console.WriteLine("Value not found");

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

Console.Write(numbers[i] + " ");

}
Example 9.10
string[] UserName = { "Furqan Abbasi", "Umer Aslam", "Zeeshan Hanif" };

Console.WriteLine("All users names");

foreach (var item in UserName)

Console.WriteLine(item);

Console.Write("Enter user name which you want to update: ");

string Name = Console.ReadLine();

bool Flag = false;

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

if (UserName[i] == Name)

Console.Write("Edit/Update Name: ");

UserName[i] = Console.ReadLine();

Flag = true;

break;

if (Flag)

Console.WriteLine("Successfully update");

foreach (var item in UserName)


{

Console.WriteLine(item);

else

Console.WriteLine("Name not found");

}
Example 9.11
int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };

int Temp = 0;

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

for (int sort = 0; sort < arr.Length - 1; sort++)

if (arr[sort] > arr[sort + 1])

Temp = arr[sort + 1];

arr[sort + 1] = arr[sort];

arr[sort] = Temp;

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

Console.Write(arr[i] + " ");

Console.ReadKey();
Question No:1
Console.WriteLine("How many players name you want to enter");

int N = Convert.ToInt32(Console.ReadLine());

string[] playersName = new string[N];

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

Console.Write("Enter player " + (i + 1) + " Name: ");

playersName[i] = Console.ReadLine();

Console.WriteLine("Players Name");

Console.WriteLine("------------");

foreach (var item in playersName)

Console.WriteLine(item);

}
Question No:2
int[] numbers = new int[20];
Random ran = new Random();

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


{
int randomNumbers = ran.Next(1, 100);
numbers[i] = randomNumbers;
Console.Write(randomNumbers + " ");

}
int highest = 0;
for (int i = 0; i < numbers.Length; i++)
{
if(highest < numbers[i])
{
highest = numbers[i];
}

}
Console.WriteLine("Highest is :" + highest);

int low = numbers[0];


for (int i = 1; i < numbers.Length; i++)
{
if (low > numbers[i])
{
low = numbers[i];
}

}
Console.WriteLine("Lowest is :" + low);

int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
sum = sum / numbers.Length;
Console.WriteLine("Average is :" + sum);
Question No:3
int[] productCode = new int[4];

for (int i = 0; i < 280; i++)


{
Console.Write("Enter code :");
int pCode = Convert.ToInt32(Console.ReadLine());

int digit = pCode / 100;


switch (digit)
{
case 0:
productCode[0]++;
Console.WriteLine("Cards");
break;

case 1:
productCode[1]++;
Console.WriteLine("Sweets");
break;

case 2:
productCode[2]++;
Console.WriteLine("Stationary");
break;

case 3:
productCode[3]++;
Console.WriteLine("Toys");
break;

default:
Console.WriteLine("Enter Valid Code");
break;
Question No:4
string[] salsa = new string[] { "Mild", "Medium", "Sweet", "Hot", "Zesty" };

            int[] jarSold = new int[5];

            int sum = 0;

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

      {

                Console.Write("Enter how many number of jars " + salsa[i] +" " + "sold : ");

                jarSold[i] = Convert.ToInt32(Console.ReadLine());

      }

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

      {

                Console.WriteLine(salsa[i] + " Salsa jars sold :" + jarSold[i]);

                sum += jarSold[i];

      }

            Console.WriteLine("Total Jars sold :"+ " " + sum);

            int highest = 0;

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

      {

                if(highest < jarSold[i])

        {

                    highest = jarSold[i];
        }

      }

            Console.WriteLine("Highest is :" + highest);

            int low = jarSold[0];

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

      {

                if (low > jarSold[i])

        {

                    low = jarSold[i];

        }

      }

            Console.WriteLine("Lowest is :" + low);

You might also like