0% found this document useful (0 votes)
38 views28 pages

Lecture 5

Uploaded by

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

Lecture 5

Uploaded by

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

Department of Computer Science & Engineering

UNIVERSITY OF MINES AND TECHNOLOGY

COMPUTER PROGRAMMING

BY: DR MILLICENT AGANGIBA

1
ARRAYS
An array stores a fixed-size sequential collection of
elements of the same type. An array is used to store
a collection of data, but it is often more useful to
think of an array as a collection of variables of the
same type stored at contiguous memory locations.

2
ARRAYS
All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest
address to the last element.

3
TYPES OF ARRAYS
There are two categories:

Single –dimensional Multi-dimensional

4
MODES OF IMPLEMENTATION
There are two modes of implementing arrays :

Static (Fixed size) Dynamic (Size not fixed)

5
DECLARATION OF SINGLE ARRAYS
To declare an array in C#, you can use the following
syntax
datatype[] arrayName;
where,
datatype is used to specify the type of elements in the array.
[ ] specifies the rank of the array. The rank specifies the size of the array.
arrayName specifies the name of the array.
Example: double[] salary

6
DECLARATION OF SINGLE ARRAYS

7
Initializing an Array
Declaring an array does not initialize the array in the
memory. When the array variable is initialized, you can
assign values to the array.
Array is a reference type, so you need to use the new
keyword to create an instance of the array.
For example,
double[] salary = new double[5];

8
Assigning Values to an Array
Value can be assigned using the index:
Example
double[] salary = new double[5];

9
Assigning Values to an Array
Value can be assigned using the entire array:
Example
double[] salary = { 2340.0, 523.69, 1452.0 3421.0, 100.5}
OR
double[] salary = new double[5] { 2340.0, 523.69, 1452.0 3421.0, 100.5};
OR
double[] salary = new double[] { 2340.0, 523.69, 1452.0 3421.0, 100.5};

10
Accessing an Array
To access any array element, write the name of the array followed by the
index of the element in square brackets.

Example:

A={5,7,8,10,11}

A[1]= 7

A[3]= 10

11
Traversing
To traverse, use loop and access elements one by one using the loop
variable. Hence an array of n elements is traverse as follows:

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

Arrayname[i];

12
Example 1
To declare an array of integer data type with a variable
name “intArray”

int[] intArray = new int[3]{10, 20, 30 };


// //print out the elements
for(int i = 0; i < intArray.Length; i++)
Console.WriteLine(intArray[i]);

13
Example 2
To declare an array of string data type with a variable name
“names” and assign the array to 2 elements

string[] names = new string[2];

14
Example 2
//To input data into the arrays or populating an array
names[0] = “Millicent Agangiba";
names[1] = "Jane Bawa";

//print out data in the arrays


for (int i = 0; i < names.Length; i++)
Console.WriteLine("Item number " + i + ": " + names[i]);
15
Example 3
To declare array of integer data type with a variable name
“numbers” and assign 5 elements

int[] numbers = { 4, 3, 8, 0, 5 }

16
Example 3
//To sort numbers

Array.Sort(numbers);
//print out the elements

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


Console.WriteLine("Item number " + i + ": " + numbers [i]);

17
Example 4
To declare an array of integer data type with a variable name “intArr”

int[] intArr = new int[5]{ 2, 4, 1, 3, 5};


To sort numbers and reverse arrays
Array.Sort(intArr);

Array.Reverse(intArr);
//print out the elements
for (int i = 0; i < intArr.length; i++)
Console.WriteLine("Item number " + i + ": " + intArr[i]);
18
Example 1: program to allow users enter 10
elements in an array
//declaration and initialization of array
int[] number_arr = new int[10];
int i;
//entering of 10-element array
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i++) {
Console.Write("element - {0} : ", i);
number_arr[i] = Convert.ToInt32(Console.ReadLine());
}
19
Example 1: program to allow users enter 10
elements in an array
// entering of 10-element array
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", number_arr[i]);
}
Console.Write("\n");

20
Example 2: program to allow users indicate how many
elements the array should have and print out in reverse
order
//declaration and initialization of array
int i, n;
int[] a = new int[100];
//indicate number of elements and elements
Console.Write("Input the number of elements to store in the
array :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} number of elements in the array
:\n", n);
21
Example 2: program to allow users indicate how many
elements the array should have and print out in reverse
order
//enter arrays with elements n
Console.Write("Input {0} number of elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
a[i] = Convert.ToInt32(Console.ReadLine());
}

22
Example 2: program to allow users indicate how
many elements the array should have and print out
in reverse order
//print out arrays with elements n
Console.Write("\nThe values store into the array are : \n");
for (i = 0; i < n; i++) {
Console.Write("{0} ", a[i]);
}
Console.Write("\n\nThe values store into the array in reverse are :\n");
// reverse the order of array elements
for (i = n - 1; i >= 0; i--) {
Console.Write("{0} ", a[i]);
}
Console.Write("\n\n");
23
Example 3: program to calculate the sum of
elements in an array
//declaration and initialization of array

int[] add = new int[100];


int i, n, sum = 0;
// preamble before entry
Console.Write("\n\nFind sum of all elements of array:\n");
Console.Write("******************************************\n");
//enter number of elements
Console.Write(“enter the number of elements in the array :");
n = Convert.ToInt32(Console.ReadLine());

24
Example 3: program to calculate the sum of
elements in an array
//print out array elements

Console.Write("Input {0} elements in the array :\n", n);


for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
add[i] = Convert.ToInt32(Console.ReadLine()); }
// sum of array elements
for (i = 0; i < n; i++) {
sum += add[i];
}
Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);
25
MORE EXERCISES TO TRY
1. Write a program in C# Sharp to count a total
number of duplicate elements in an array
2. Write a program in C# Sharp to find maximum and
minimum element in an array
3. Write a program in C# Sharp to sort elements of
array in ascending order

26
MORE EXERCISES TO TRY
4. Write a program in C# Sharp to sort elements of
the array in descending order
5. Write a program in C# Sharp to delete an element
at desired position from an array

27
28

You might also like