Title: CS 112 Introduction to Programming
1CS 112 Introduction to Programming
- Lecture 19
- Arrays Examples and Parameter Passing
- Http//zoo.cs.yale.edu/classes/cs112/
2Outline
- Review
- Examples
- command line array
- strings as arrays
- using array elements as counters
- Parameter passing
3Review Arrays
- An array stores multiple elements of the same
type - Refer to particular element in the array by
position number - In C, an array behaves very much like an object
- declaration and instantiation are like objects
- declare an array variable
- create an array using new
- make a variable a reference of an array
- parameter passing is similar to objects
- an array has the Length property
- You can use initializer to create and initialize
an array
4Example 1 Command-Line Arguments
- The signature of the Main method indicates that
it takes an array of string as parameter - These values come from command-line arguments
that are provided when the program is invoked - For example, the following invocation of the
interpreter passes an array of three string
objects into Main - C\gt Calculator 2 3
- These strings are stored at positions 0-2 of the
parameter - See Calculator.cs
- To set command line if use command window, see
above if use VS, Project-gtCalculator
Properties-gtConfiguration Properties-gtDebugging-gtC
ommand Line Arguements
5Example 2 strings as Arrays
- You can use a string as an array
- You can access the length of a string using the
Length property - You can access each character of a string using
, e.g.,string resp Console.ReadLine().ToUpper
()for (int i 0 i lt resp.Length i)
Console.Write( respi )
See StringArray.cs
6Examples Using the Elements of an Array as
Counters
- Use array elements to keep track of number of
occurrences of different values - Create an array with size of the number of
possible values - Each element of the array keeps track of the
number of occurrences of one value - When a possibility occurs, increase the array
element by one - May need to map from value to index
- For example, read a sequence of integers between
1 to 10 until user inputs 0 keep track of number
of occurrences of 1 to 10int counters new
int10int numwhile ( (numInt32.Parse(
Console.ReadLine() )) ! 0) countersnum-1
7StudentPoll.cs
- 1 // Fig. 7.7 StudentPoll.cs
- 2 // A student poll program.
- 3
- 4 using System
- 5 using System.Windows.Forms
- 6
- 7 class StudentPoll
- 8
- 9 // main entry point for application
- 10 static void Main( string args )
- 11
- 12 int responses 1, 2, 6, 4, 8, 5,
9, 7, 8, 10, 1, - 13 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5,
7, 6, 8, 6, 7, - 14 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,
10 - 15
- 16 int frequency new int 11
- 17 string output ""
- 18
- 19 // increment the frequency for each
response
Declare and initialize integer array responses
Declare and allocate integer array frequency
For every element in responses, increment the
frequency element that corresponds to the answer
Output the number of times each response appeared
8Example Die Rolling Program
- Roll 12 dice after a user clicks on Roll
- Use an array to keep track of the number of
occurrences of each face value - Increment the corresponding array element when a
face value is rolled
9RollDie.cs
- 1 // Fig. 7.6 RollDie.cs
- 2 // Rolling 12 dice.
- 3
- 4 using System
- 5 using System.Drawing
- 6 using System.Collections
- 7 using System.ComponentModel
- 8 using System.Windows.Forms
- 9 using System.Data
- 10 using System.IO
- 11
- 12 public class RollDie System.Windows.Forms.F
orm - 13
- 14 private System.Windows.Forms.Button
rollButton - 15
- 16 private System.Windows.Forms.RichTextBox
displayTextBox - 17
- 18 private System.Windows.Forms.Label die1
- 19 private System.Windows.Forms.Label die2
Create a Random object
Declare an integer array frequency and allocate
it enough memory to hold 7 integers
10RollDie.cs
- 36 public RollDie()
- 37
- 38 InitializeComponent()
- 39
- 40
- 41 // Visual Studio .NET generated code
- 42
- 43 STAThread
- 44 static void Main()
- 45
- 46 Application.Run( new RollDie() )
- 47
- 48
- 49 private void rollButton_Click(
- 50 object sender, System.EventArgs e )
- 51
- 52 // pass the labels to a method that
will - 53 // randomly assign a face to each die
- 54 RollADie( die1 )
11RollDie.cs
- 71
- 72 displayTextBox.Text
"Face\tFrequency\tPercent\n" - 73
- 74 // output frequency values
- 75 for ( int x 1 x lt frequency.Length
x ) - 76
- 77 displayTextBox.Text x "\t"
- 78 frequency x "\t\t"
String.Format( "0N", - 79 frequency x / total 100 )
"\n" - 80
- 81
- 82 // end Main
- 83
- 84 // simulates roll, display proper
- 85 // image and increment frequency
- 86 public void RollADie( Label die )
- 87
- 88 int face randomNumber.Next( 1, 7 )
- 89
Get a random number from 1 to 6
Display die image corresponding to the number
rolls
12Outline
- Admin. and review
- Examples
- command line array
- strings as arrays
- using array elements as counters
- Parameter passing
13Recall Two Types of Variables
- A variable represents a cell in memory
- Value type
- int, char, byte, float, double, stringA value
type variable stores a value of the type of the
variable in the memoryint x 45double y
45.12 - Reference type
- A variable that stores object or array
actually stores a reference to an object or
array, e.g., - A reference is a location in computers memory
where the object or array itself is storedTime3
t1t1 new Time3(11, 45, 59)
x
45
y
45.12
t1
14Implications of the Two Types of Variables
Assignment
- An assignment of one value variable toanother
value variable copies the value, e.g.,int x
45double y 45.12int zz x - An assignment of one reference variable
toanother reference variable copies the
reference, e.g.,Time3 t1t1 new Time3(11, 45,
59)Time3 t2t2 t1
x
45
y
45.12
t1
15Implications of the Two Types of Variables Change
- Change the value of one value variablewill not
change the otherint x 45double y
45.12int zz xx 23 - Change the content (state) by one reference may
affect another reference variableTime3 t1t1
new Time3(11, 45, 59)Time3 t2t2
t1t2.SetTime(22, 22, 22)
x
45
23
y
45.12
t1
16Passing Arguments by Value and by Reference
- Passing a value type argument to methods
- The value of an actual argument is copied to the
formal argument - The formal argument has its own memory location
- Any changes to the formal argument in the method
do not affect the actual argument - Passing a reference type argument to methods
- A copy of the reference to the object/array is
made to the formal argument - Any changes to the contents of the object/array
in the method, do affect the object/array outside
the method - Because both the formal argument and the actual
argument are reference to the same object/array
17Calling a Method
- Each time a method is called, the actual
arguments in the invocation are copied into the
formal arguments - If a value type, it is the value that is copied
- If a reference type, it is the reference that is
copied
int num SquareSum (2, 3)
2
num1
5
num2
3
18Calling a Method Value Type
2
n1
int n1 2
n2
int n2 3
3
int num SquareSum (n1, n2)
2
num1
5
num2
3
19Calling a Method Value Type
- Even if formal arguments and actual arguments
have the same name, modifications to formal
arguments in a method will not affect actual
arguments
2
num1
int num1 2
num2
int num2 3
3
int num SquareSum (num1, num2)
2
num1
5
num2
3
20Calling a Method Reference
- Each time a method is called, the actual
arguments in the invocation are copied into the
formal arguments - If a value type, it is the value
- If a reference type, it is the reference
int array 1, 2, 3
array
int num SquareSum (array)
array
21Calling a Method Reference
- Each time a method is called, the actual
arguments in the invocation are copied into the
formal arguments - If a value type, it is the value
- If a reference type, it is the reference
int array 1, 2, 3
array
DoubleArray (array)
array
22Arrays as Parameters Summary
- A reference to a array is passed, making the
formal and actual parameters reference of the
same array - Changing an array element in the method changes
the original - An array element can be passed to a method as
well, and follow the parameter passing rules of
that element's type