Lab9 - Arrays C#
Lab9 - Arrays C#
Lab9 - Arrays C#
Topics
Array of data types
Accessing array elements
Initializing array elements
Actually, here we have not declared an array yet. Technically, we are declaring a variable (myIntArray) that
will hold a reference to an array of integers.
The square brackets ([]) tell the C# compiler that we are declaring and array, and the type specified the type of
the elements it will contain. In the above example, myIntArray is an array of integers.
We can instantiate an array using the new keyword. For example:
myIntArray = new int[5];
This declaration creates and initializes an array of five integers, all of which are initialize to the value 0.
It is important to distinguish between the array itself (which is the collection of elements) and the elements of
the array. myIntArray is the array (or, more accurately, the variable that hold the reference to the array); its
elements are five integers it hold.
OneDimArray () {
myIntArray = new int[dim];
}
Console.ReadLine();
}
}
}
Reader should type and debug all the example codes found in this document to examine the output.
Computer & Programming: Arrays 2/9 Powered by MIKETM
Exercise 1
1: using System;
2: namespace Exercise1
3: {
4: class Fibonacii
5: {
6: static void Main (string[] args)
7: {
8: int[] fib;
9: int num = 0;
10:
11: Console.Write ("Enter the number of terms: ");
12: num = Int32.Parse (Console.ReadLine ());
13:
14: fib = new int[num];
15: for (int i=0; i<fib.Length; i++) {
16: fib[i] = fib[i-1] + fib[i-2];
17: }
18:
19: for (int i=0; i<fib.Length; i++)
20: Console.Write ("{0} ", fib[i]);
21:
22: Console.ReadLine();
23: }
24: }
25: }
1.1 Run the above program with input 1. What happens and why?
1.2 Replace the statement in line 16 with the following statements. Try running the program again with the
inputs 1, 2, 5, 10 and 20, respectively, for each run, and then re-examine the results.
if ((i==0) || (i==1))
fib[i] = 1;
else
fib[i] = fib[i-1] + fib[i-2];
Input Output
1
2
5
10
20
1.3 According to the outputs from 1.2, what does the above program do?
2.1 Run the program with input string “hello world 555” What is the output of the program?
2.3 Try another input “Hello WORLD 555”, how does the output differ from the output in 2.1?
2.4 Rewrite the above program to make the program count an uppercase letter as a lowercase one. For
example, one ‘A’ and one ‘a’ will be counted as two ‘a’. Write the part of the changed code below.
3.1 Run the program with the input 234, what is the output of the program?
3.2 Modify the above program to convert decimal numbers into octal (base 8) ones.
4.1 Run the program with five inputs: 1, 2, 3, 4, and 5. What is the output of the program?
Then try running the program again with same inputs: 1, 2, 3, 4, and 5. What is the output of the
program?
Computer & Programming: Arrays 7/9 Powered by MIKETM
4.3 Why does the output in 4.1 differ from the one in 4.2?
Exercise 5
5.1 The following program takes five numbers as user input. It then finds the minimum value, maximum
value, as well as computes the average and standard deviation.
Complete all the blank areas of this program. (You must not change other contents of the program) Each
method is defined to operate as follows:
getInput gets a list ( l ) of input from user.
findMin returns the minimum value in the list l .
findMax returns the maximum value in the list l .
n
l[i]
CalAvg returns the average value of the list l as x i 1
.
n
n
(l[i] x ) 2