0% found this document useful (0 votes)
9 views12 pages

Wk10a Arrays

The document provides an overview of arrays in programming, covering their declaration, initialization, and methods for accessing and manipulating elements. It explains the use of built-in methods, shortcuts for creating arrays, and the importance of arrays for code readability and efficiency. Additionally, it highlights the limitations of arrays, such as the requirement for uniform data types.

Uploaded by

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

Wk10a Arrays

The document provides an overview of arrays in programming, covering their declaration, initialization, and methods for accessing and manipulating elements. It explains the use of built-in methods, shortcuts for creating arrays, and the importance of arrays for code readability and efficiency. Additionally, it highlights the limitations of arrays, such as the requirement for uniform data types.

Uploaded by

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

Arrays (Part 1 of 2)

Programming I
Narendra Pershad
Centennial College
Topics

 Declare an array and assign values to array elements


 Access array elements
 Search an array using a loop
 Use the BinarySearch(), Sort(), and Reverse() methods
 Use multidimensional arrays
 Shortcuts in creating arrays
What is an array?

 Multi-value variable
 A set of values that shares the same name
 Midterm marks of all the students in COMP100
 Each value is called an item or element
 The elements are referenced by using a subscript
 Subscripts are integer and starts at 0
 Used extensively in legacy code
 The size of Array can change, but it is a potentially expensive
operation
Using arrays
Declaration Declaration
int mark; int[] marks;
char vowel; char[] vowels;

Allocating storage Allocating storage


marks = new int[38];
vowels = new char[5];

Setting a value Setting a value


mark = 78; marks[0] = 78;
vowel = 'E'; vowels[1] = 'E';
Accessing elements
 Setting values

marks[0] = 75; //assigns 75 to the first item


marks[1] = 91; //assigns 91 to the second item
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
 Getting value

Console.WriteLine(marks[0]); //outputs the first


item
Console.WriteLine(marks); //gibberish
Shortcuts
 Declaration/allocation/initialization
//declares a char array that contains the chars a,e,i,o,u
char[] vowels = {'a', 'e', 'i', 'o', 'u'};

//declares a int array that contains the integers 2, 3, 5, 7, 11, 17, 19


int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};

//declares a string array that containing the first line of the famous nursery
rhyme
string[] poem = {"Mary", "had", "a", "little", "lamb"};

//declares a string to hold the names of the president


string[] obama = {"Barak", "Hussein", "Obama"};
The .Length property of all arrays
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
Console.WriteLine(vowels.Length);

int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};


Console.WriteLine(primes.Length);

string[] poem = {"Mary", "had", "a", "little", "lamb"};


Console.WriteLine(poem.Length);

string[] obama = {"Barak", "Hussein", "Obama"};


Console.WriteLine(obama.Length);
Display all the items in an array

string[] poem = {"Mary", "had", "a", "little", "lamb"};


Console.WriteLine(poem);//will print gibberish

int counter = 0;
do
{
Console.WriteLine(poem[counter]);
counter++;
}while (counter < poem.Length);
Updating all the items in an array
double[] marks = {50, 67, 73, 55, 89, 68, 66, 76, 49};
int counter = 0;
while (counter < marks.Length){
marks[counter] += 3;
counter++;
}
//now we can print the updates array
counter = 0;
while (counter < marks.Length){
Console.WriteLine(marks[counter]);
counter++;
}
Summing all the items in an array
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 37,
41, 47};

int sum = 0;
for (int counter = 0; counter < primes.Length; counter++)
{
sum += primes[counter];
}
//now we can display the sum
Console.WriteLine($"the sum is {sum}");
Built-in Methods to return arrays

//The Split() method of the object string explodes the individual


words to produce a string array
string[] poem = "Mary had a little lamb".Split();
string[] obama = "Barak Hussein Obama".Split()
string[] sillyWords = "foo bar baz quux".Split()

//The ToCharArray () method of the object string "explodes" the


individual letter to produce a char array
char[] vowels = "aeiou".ToCharArray();
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
char[] letters = "Narendra".ToCharArray();
Why Arrays?

 One identifier for the array


 Add readability to your code
 Identifier for lots of variables
 Easy to process the all the values
 First index starts at 0 and goes to one less than the size
 The items are stored sequentially in memory
 Very efficient in storage and accessing items
 Problems:
 All the items must be on the same type

You might also like