Arrays - C Sharp - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of C sharp which includes Fraction Class, Encapsulated Features in Fraction, Fraction Arithmetic, Non-Static Method, Overloaded Method, Public Static Fraction Operator, Sides of Inequality etc. Key important points are: Arrays, Memory Locations, Integer-Valued Expression, Reference Type, Defining Array, Specified Type, Array of Characters, Array of Integers, Array of Doubles, Accessing Individual Elements

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dheeraj
dheeraj 🇮🇳

5

(4)

101 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Arrays - C Sharp - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Arrays

Definition An array is a group of consecutive

memory locations (contiguous) allconsisting of the same type. Individual memory elements within the

array can be accessed via a commonidentifier (the name of the array) andan integer-valued expression.

We will

see how this is done as we developand use arrays.

Defining an Array in C# There are two steps in establishing an array.First:

Declare an identifier as an array type:

Second:

Allocate the actual array object.

Example:

int[ ] Grades;

//Grades will reference an //array of integers:

Grades = new int[50];

//Allocate space for//50 integers

Same Declaration int[ ] Grades = new int[50];

Yet another way to declare anarray. int[ ] List = {1,2,10,20,30,11,12};This declares and initializes the array.Here, we are not specifically calling the

new to allocate memory at runtime.List will consist of 7 integer elementscontaining the numbers 1, 2, 10, 20,30, 11, & 12 respectively.

Accessing Individual Elements If an array has N elements (N>0), then

the elements in that array can beaccessed by the array’s name and anindex that ranges from 0 to N-1inclusive. Consider the example on the next slide.

Terminology In the example on the previous page:List is the “name” of the array. Like a variable

identifier it can be any legal user-definedidentifier. List[i]

Placing [ ] after the name “dereferences” the array and gives theindividual element. The quantity inside the[ ] is called the index.

It must be an

integer valued expression whose value isfrom 0 to N-1 where N is the number ofelements in the array.

In general, the name of an array is a reference

type.

When it is dereferenced as

indicated before

( name[expression])

the result might be a value type or areference type, depending on thetype of the array. Example:

List[i] from our previous

slides is a value type, since List is anarray of integers.

Look at a problem: Declare an array of size 1000 and fill it

with random integers in the range 1to 20.

Problem (cont.) Now add the code that will count the

number of times each number in therange 1 to 20 was written into thearray.

Problem 2. Write a program that will ask the user to input

the number of random birthdays (numbersin the range 1 to 365) that she would liketo generate.

Then generate that many

random birthdays, and report the ones thatwere hit more than once. This simulates the experiment we ran in class

in which we looked at 40 random birthdays.

Problem 3 An experiment consists of choosing N

people at random and seeing if twohave the same birthday. Write a program that will ask the user

to input N, and then simulate 10000experiments.

Report the number of

experiments in which two people hadthe same birthday.

Questions about the method: 

What is its return type? 

How many parameters are there andwhat are their types? 

What is the method name? 

What is the method doing? 

Given an array B of integers, howwould you call this method and passB to it?

For Example: int[] B

= {4,4,2,3,7,3,2,4,3,8,7,5};

Console.WriteLine(FindMax(B)); We will discuss what is going on with

this call to the method, and describehow B is being passed to the method.