0% found this document useful (0 votes)
70 views

Arrays and Strings Module

The document discusses arrays, including declaring single- and multi-dimensional arrays, initializing array elements, invoking array elements, and using foreach statements to iterate through arrays. It also covers the Array class and methods, as well as strings, including string literals, escape sequences, and string methods. Examples are provided for array and string programs involving sorting scores into arrays, computing percentages, and reversing a string.

Uploaded by

Ichi Paradiang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Arrays and Strings Module

The document discusses arrays, including declaring single- and multi-dimensional arrays, initializing array elements, invoking array elements, and using foreach statements to iterate through arrays. It also covers the Array class and methods, as well as strings, including string literals, escape sequences, and string methods. Examples are provided for array and string programs involving sorting scores into arrays, computing percentages, and reversing a string.

Uploaded by

Ichi Paradiang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Arrays and Strings

OBJECTIVES:
1. To learn and apply the basics principles of arrays, declare single- and multidimensional arrays and
perform compile-time initialization of array elements
2. To invoke elements of an array and be familiar with some methods of the Array class.
3. To understand the use of foreach statements.
4. To use array as user-defined objects and return type in methods.
5. To create methods that use strings class and some methods.

DISCUSSION:

Arrays

An array is a data structure that contains a number of variables that are accessed
through their assigned index. The variables contained in an array are all of the same type, and this type
is called the element type of the array.

An array has a rank that determines the number of indices associated with each array
element and is also referred to as the dimensions of the array.

An array with a rank of one is called a single-dimensional array. An array with a rank
greater than one is called a multi-dimensional array.

Dimension of an array has an associated length that is an integral number greater than
or equal to zero. The dimension lengths are not part of the type of the array but they are
established when an instance of the array type is created at run-time.

The length of a dimension determines the valid range of indices for that dimension:
For a dimension of length N, indices can range from 0 to N – 1 inclusive.
The total number of elements in an array is the product of the lengths of each dimension in the array. If
one or more of the dimensions of an array have a length of zero, the array is said to be empty. The
element type of an array can be any type, including an array type.

Array Creation, Initialization and Instantiation

DataType[ ] ArrayName = new DataType[ArraySize] {ArrayInitialContents};


(Optional)
Foreach
foreach statement is new feature in programming language such as C++/Java. It is used
particularly for arrays because it moves through the collection until it expires. The general form
of the foreach statement is:

Array Class
The class defined can be used as an array. It is possible to create a record using the userdefined
class as an array. Instead of using a data type, the array name is used. Aside from
this feature in C#, an array could be used also as return type of a method. Example of the
method heading below,

Strings
The string type is a sealed class type that inherits directly from object. Instances of the
string class represent Unicode character strings. Values of the string type can be written as
string literals. The keyword string is simply an alias for the predefined class System.String.
C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as
in "hello, world", and can include both simple escape sequences and hexadecimal and
Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote


character, zero or more characters, and a closing double-quote character.
String type is a sequence of Unicode code units.
Example :
string s = "hello CpE";
string b = @"Happy birthday, John";
string c = "hello \t world"; // hello world
string d = @"hello \t world"; // hello \t world
The keyword string is simply an alias for the predefined class System.String.
APPLICATION PROGRAMS: Use Filename: “CPE 111_idnumber_Practice1”
1. Create a program that will accept 10 quiz raw scores, store and sort them in an array.
Create another array that will store the percentage of each quiz. Compute and display
the average, highest and lowest percentage.

2. Create a program that will accept a string and display that string backwards.
Example: “Computer Engineering”
Displays: “gnireenignE retupmoC”

You might also like