IST211 – Lecture Notes: Arrays – Chapter 7
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceLearningArrays
{
classProgram
{
staticvoid Main(string[] args)
{
//Experiment01(); //create and initialize array
//Experiment02(); //show array content
//Experiment03(); //find position of key value in array
//Experiment04(); //position of max value inside array
////example of an array returning method
//double[] values = Experiment05(4);
//ShowArray(values);
//double average = CalculateAverage(values);
//Console.WriteLine("\nAverage is {0:F2}\n", average);
////passing simple argument by value
//int n = 7;
//Console.WriteLine("n before calling is {0}", n);
//Experiment06(n);
//Console.WriteLine("n after calling is {0}", n);
////passing array argument by reference
//int[] list8 = { 100, 200, 300 };
//ShowArray(list8);
//Experiment07(list8);
//ShowArray(list8);
//Experiment10(); // using the foreach iterator
//Experiment11(); // using the Array.Clear class
//Experiment12(); // shallow copy vs. cloning
//Experiment13(); // indexOf
//Experiment14(); // using Sort
//Experiment15(); // reversing an array
//Experiment16(); // create array of UDT (User-Defined-Type)
int[] list17 = { 77, 22, 66, 11, 55, 44, 33 };
Console.WriteLine("BEFORE SORTING\n");
ShowArray(list17);
//Experiment17(list17); // sorting
int[] list18 = Experiment18(list17);
Console.WriteLine("\nAFTER SORTING");
ShowArray(list17);
ShowArray(list18);
}
privatestaticint[] Experiment18(int[] list17)
{
int[] clonedArray = (int[])list17.Clone();
int n = clonedArray.Length - 1;
for (inti = 0; i <= n; i++)
{
intpos = FindPositionOfMaxValue(clonedArray, n - i);
SwapCells(clonedArray, pos, n - i);
}
returnclonedArray;
}
privatestaticvoid Experiment17(int[] list17)
{
int n = list17.Length - 1;
for (inti = 0; i <= n; i++)
{
intpos = FindPositionOfMaxValue(list17, n-i);
SwapCells(list17, pos, n-i);
}
}
privatestaticvoidSwapCells(int[] list, int left, int right)
{
int temp = list[right];
list[right] = list[left];
list[left] = temp;
}
privatestaticintFindPositionOfMaxValue(int[] list, int last )
{
intposMax = 0;
intvalMax = list[0];
for (inti = 1; i <= last; i++)
{
if ( valMax < list[i])
{
posMax = i;
valMax = list[i];
}
}
Console.WriteLine("Pos Max is {0} max value is {1}", posMax, valMax);
returnposMax;
}
privatestaticvoid Experiment16()
{
Console.WriteLine("\nExperiment16");
Person p1 = newPerson();
Person p2 = newPerson("Arya Stark", "555-1111");
Person p3 = newPerson("Daenerys Targeryan", "555-2222");
Person p4 = newPerson("Tyrion Lannister", "555-3333");
Person[] westerosPeople = newPerson[7];
westerosPeople[0] = p1;
westerosPeople[1] = p2;
westerosPeople[2] = p3;
westerosPeople[3] = p4;
foreach (PersonheroeinwesterosPeople)
{
Console.WriteLine(heroe);
}
}
privatestaticvoid Experiment15()
{
Console.WriteLine("\nExperiment15");
int[] list1 = newint[] { 11, 22, 33, 66, 44, 77, 55 };
Console.WriteLine("\nBefore reversing\n");
ShowArray(list1);
Array.Reverse(list1);
Console.WriteLine("\nAfter reversing\n");
ShowArray(list1);
}
privatestaticvoid Experiment14()
{
Console.WriteLine("\nExperiment14");
int[] list1 = newint[] { 11, 22, 33, 66, 44, 77, 55 };
Console.WriteLine("\nBefore sorting\n");
ShowArray(list1);
Array.Sort(list1);
Console.WriteLine("\nAfter sorting\n");
ShowArray(list1);
}
privatestaticvoid Experiment13()
{
Console.WriteLine("\nExperiment13");
int[] list1 = newint[] { 11, 22, 33, 66, 66, 44, 77, 55 };
Console.WriteLine("\nlist1\n");
ShowArray(list1);
intpos = Array.LastIndexOf(list1, 66);
Console.WriteLine("Position is " + pos);
}
privatestaticvoid Experiment12()
{
Console.WriteLine("\nExperiment12");
int[] list1 = newint[] { 11, 22, 33, 66, 44, 77, 55 };
Console.WriteLine("\nlist1\n");
ShowArray(list1);
//Console.WriteLine("\nlist2\n");
//int[] list2 = list1;
//ShowArray(list2);
//list2[3] = 6000;
//Console.WriteLine("\nlist1\n");
//ShowArray(list1);
int[] list2 = (int[])list1.Clone();
ShowArray(list2);
list2[3] = 6000;
Console.WriteLine("\nlist1\n");
ShowArray(list1);
Console.WriteLine("\nlist2\n");
ShowArray(list2);
}
privatestaticvoid Experiment11()
{
Console.WriteLine("\nExperiment11");
int[] list1 = newint[] { 11, 22, 33, 66, 44, 77, 55 };
Console.WriteLine("\nBefore\n");
ShowArray(list1);
Console.WriteLine("\nAfter\n");
Array.Clear(list1,2, 3);
ShowArray(list1);
}
privatestaticvoid Experiment10()
{
Console.WriteLine("\nExperiment10");
int[] list1 = newint[]{ 11, 33, 55, 77};
foreach ( int n in list1)
{
Console.WriteLine( n );
}
stringstr = "now is the time for all good citizens to come in the aid of their country";
string[] tokens = str.Split();
foreach (string t in tokens)
{
Console.WriteLine( t );
}
Console.WriteLine("\nShowing array in reverse order");
for (inti = list1.Length - 1; i >= 0; i--)
{
Console.WriteLine( list1[i]);
}
}
privatestaticvoid Experiment07(int[] list)
{
Console.WriteLine("\nExperiment07");
//accepting argument by reference, changes will affect caller
for (inti = 0; ilist.Length; i++)
{
list[i] = 2 * list[i];
}
Console.WriteLine("Inside Experiment07");
ShowArray(list);
}
privatestaticvoid Experiment06(int n)
{
Console.WriteLine("\nExperiment06");
//accepting argument by value, changes are local
n = 2 * n;
Console.WriteLine("Inside Experiment06 n is {0}", n);
}
privatestaticdouble[] Experiment05(intarraySize)
{
Console.WriteLine("\nExperiment05");
//data acquisition
double[] grades = newdouble[arraySize];
int index = 0;
//read fombeyboard, transfer data to array
string data = "";
Console.WriteLine("Please enter next grade [## to end]");
data = Console.ReadLine();
while ( (data != "##") & (index < grades.Length) )
{
doublesingleGrade = double.Parse(data);
grades[index] = singleGrade;
index++;
if (index >= grades.Length) break;
Console.WriteLine("Please enter next grade [## to end]");
data = Console.ReadLine();
}
//ShowArray(grades);
return grades;
}
privatestaticvoid Experiment04()
{
Console.WriteLine("\nExperiment04");
//find max value, position of max, min value
int[] list5 = newint[]{ 88, 66, 22, 44, 11, 99, 77, 55};
ShowArray(list5);
intmaxValue = FindMaxValue(list5);
Console.WriteLine("Maximum value is {0}", maxValue);
intposMax = FindPosOfMax(list5);
Console.WriteLine("Position of max value is {0}", posMax);
intposMin = FindPosOfMin(list5);
Console.WriteLine("Position of min value is {0}", posMin);
}
privatestaticintFindPosOfMin(int[] list)
{
//find position of the minimum value stored in an array
intcurrentMinVal = list[0];
intcurrentMinPos = 0;
for (inti = 1; ilist.Length; i++)
{
if( list[i] < currentMinVal)
{
currentMinVal = list[i];
currentMinPos = i;
}
}
returncurrentMinPos;
}
// ------
privatestaticintFindPosOfMax(int[] list)
{
//find position of maximum value in array
intcurrentMaxVal = list[0];
intcurrentMaxPos = 0;
for (inti = 1; ilist.Length; i++)
{
if (list[i] > currentMaxVal)
{
currentMaxVal = list[i];
currentMaxPos = i;
}
}
returncurrentMaxPos;
}
privatestaticintFindMaxValue(int[] list)
{
//find maximum value in array
intcurrentMax = list[0];
for (inti = 1; ilist.Length; i++)
{
if(list[i] > currentMax)
{
currentMax = list[i];
}
}
returncurrentMax;
}
privatestaticvoid Experiment03()
{
Console.WriteLine("\nExperiment03");
//find a key in given array, calculate its average value
int[] list3 = newint[] { 22, 33, 11, 44, 7 };
ShowArray(list3);
int key = 77;
intpos = FindPosition(key, list3);
Console.WriteLine("pos is {0} key is {1}", pos, key);
int[] list4 = { };
double average = CalculateAverage(list4);
Console.WriteLine("Average values is {0}", average);
}
privatestaticdoubleCalculateAverage(int[] list)
{
//calculate average of int-array cells
double result = 0;
if (list.Length == 0)
return 0;
for (inti = 0; ilist.Length; i++)
{
result += list[i];
}
return (result / list.Length);
}
privatestaticdoubleCalculateAverage(double[] list)
{
//calculate average of double-array cells
double result = 0;
if (list.Length == 0)
return 0;
for (inti = 0; ilist.Length; i++)
{
result += list[i];
}
return (result / list.Length);
}
privatestaticintFindPosition(int key, int[] list)
{
// find first occureence of a key inside array
for (inti = 0; ilist.Length; i++)
{
if (key == list[i])
returni;
}
return -1;
}
privatestaticvoid Experiment02()
{
Console.WriteLine("\nExperiment02");
//define array immwdiate data - show its content
int[] list2 = newint[] { 11, 33, 55, 77 };
ShowArray(list2);
}
privatestaticvoidShowArray(double[] array)
{
// show double-array content
for (inti = 0; iarray.Length; i++)
{
Console.Write("[{0}]= {1}, ", i, array[i]);
}
Console.WriteLine();
}
privatestaticvoidShowArray(int[] array)
{
// show int-array content
for (inti = 0; iarray.Length; i++)
{
Console.Write("[{0}]= {1}, ", i, array[i]);
}
Console.WriteLine();
}
privatestaticvoid Experiment01()
{
Console.WriteLine("\nExperiment01");
// create aand initialize an array
int[] list1 = newint[5];
list1[0] = 100;
list1[1] = 200;
list1[2] = 300;
list1[3] = 400;
list1[4] = 500;
for (inti = 0; i < list1.Length; i++)
{
Console.WriteLine("at {0} data is {1} ", i, list1[i]);
}
int data = list1[4];
Console.WriteLine(data);
}
}
}
CONSOLE
Experiment01
at 0 data is 100
at 1 data is 200
at 2 data is 300
at 3 data is 400
at 4 data is 500
500
Experiment02
[0]= 11, [1]= 33, [2]= 55, [3]= 77,
Experiment03
[0]= 22, [1]= 33, [2]= 11, [3]= 44, [4]= 7,
pos is -1 key is 77
Average values is 0
Experiment04
[0]= 88, [1]= 66, [2]= 22, [3]= 44, [4]= 11, [5]= 99, [6]= 77, [7]= 55,
Maximum value is 99
Position of max value is 5
Position of min value is 4
Experiment05
Please enter next grade [## to end]
100
Please enter next grade [## to end]
60
Please enter next grade [## to end]
##
[0]= 100, [1]= 60, [2]= 0, [3]= 0,
Average is 40.00
n before calling is 7
Experiment06
Inside Experiment06 n is 14
n after calling is 7
[0]= 100, [1]= 200, [2]= 300,
Experiment07
Inside Experiment07
[0]= 200, [1]= 400, [2]= 600,
[0]= 200, [1]= 400, [2]= 600,
Experiment10
11
33
55
77
now
is
the
time
for
all
good
citizens
to
come
in
the
aid
of
their
country
Showing array in reverse order
77
55
33
11
Experiment11
Before
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 44, [5]= 77, [6]= 55,
After
[0]= 11, [1]= 22, [2]= 0, [3]= 0, [4]= 0, [5]= 77, [6]= 55,
Experiment12
list1
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 44, [5]= 77, [6]= 55,
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 44, [5]= 77, [6]= 55,
list1
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 44, [5]= 77, [6]= 55,
list2
[0]= 11, [1]= 22, [2]= 33, [3]= 6000, [4]= 44, [5]= 77, [6]= 55,
Experiment13
list1
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 66, [5]= 44, [6]= 77, [7]= 55,
Position is 4
Experiment14
Before sorting
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 44, [5]= 77, [6]= 55,
After sorting
[0]= 11, [1]= 22, [2]= 33, [3]= 44, [4]= 55, [5]= 66, [6]= 77,
Experiment15
Before reversing
[0]= 11, [1]= 22, [2]= 33, [3]= 66, [4]= 44, [5]= 77, [6]= 55,
After reversing
[0]= 55, [1]= 77, [2]= 44, [3]= 66, [4]= 33, [5]= 22, [6]= 11,
Experiment16
Person [ Name: no name yet, Phone: no phone yet ]
Person [ Name: Arya Stark, Phone: 555-1111 ]
Person [ Name: Daenerys Targeryan, Phone: 555-2222 ]
Person [ Name: Tyrion Lannister, Phone: 555-3333 ]
Press any key to continue . . .