Array in C# Array in C# RIHS Arshad Khan

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Chapter 8 Arrays and Strings
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Pointers *, &, array similarities, functions, sizeof.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Low level collections.
Computer Programming BCT 1113
Lecture-5 Arrays.
Module 2 Arrays and strings – example programs.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Arrays, For loop While loop Do while loop
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Chapter 15 Pointers, Dynamic Data, and Reference Types
7 Arrays.
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Review for Final Exam.
Chapter 15 Pointers, Dynamic Data, and Reference Types
INC 161 , CPE 100 Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Arrays .
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Topics discussed in this section:
Arrays ICS2O.
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Review for Final Exam.
7 Arrays.
Chapter 2 Programming Basics.
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
CS1001 Lecture 14.
Question 1a) What is printed by the following Java program? int s;
Arrays, Part 1 of 2 Topics Definition of a Data Structure
How do you do the following?
Pointers, Dynamic Data, and Reference Types
4.1 Introduction Arrays A few types Structures of related data items
Week 7 - Monday CS 121.
Presentation transcript:

Array in C# Array in C# RIHS Arshad Khan

12-2 Objectives: Learn about arrays and when to use them Learn the syntax for declaring and initializing arrays and how to access array’s size and elements Learn simple array algorithms Understand two-dimensional arrays

12-3 What is an Array An array is a block of consecutive memory locations that hold values of the same data type. Individual locations are called array’s elements. When we say “element” we often mean the value stored in that element An array of floats

12-4 What is an Array (cont’d) Rather than treating each element as a separate named variable, the whole array gets one name. Specific array elements are referred to by using array’s name and the element’s number, called index or subscript. c[0]c[1]c[2]c[3] c is array’s name

12-5 Indices (Subscripts) In C# an index is written within square brackets following data types (for example, int [ ]a). Indices start from 0; the first element of an array a is referred to as a[0] and the n-th element as a[n - 1]. An index can have any int value from 0 to array’s length - 1.

12-6 Indices (cont’d) We can use as an index an int variable or any expression that evaluates to an int value. For example: a [3] a [k] a [k - 2] a [ (int) (6 * Math.random()) ]

12-7 Indices (cont’d) In C#, an array is declared with fixed length that cannot be changed. C# editor checks the values of indices at run time and throws ArrayIndexOutOfBoundsException if an index is negative or if it is greater than the length of the array - 1.

12-8 Why Do We Need Arrays? The power of arrays comes from the fact that the value of a subscript can be computed and updated at run time. int sum = 0; sum += x1; sum += x2; … sum += x1000; No arrays: int n = 1000; int sum = 0, i; for (i = 0; i < n; i++) sum += x[i]; With arrays: 1000 times!

12-9 Declaration and Initialization When an array is created, space is allocated to hold its elements. If a list of values is not given, the elements get the default values. For example: int [ ] x= new int[10] ; char [ ] word=new char[1000]; length 10, all values set to 0 length 10000, all values set to null

12-10 Initialization (cont’d) An array can be declared an initialized in one statement. For example: int [ ] x = { 3,1, 7, 5, 9 };

12-11 Array’s Length The length of an array is determined when that array is created. The length is either given explicitly or comes from the length of the {…} initialization list. int [5] x= { 3,1, 7, 5, 9 }; int [ ] x= { 3,1, 7, 5, 9 }; // here in both cases array length is 5

C# Program usingarray Public static void main() { //Declares the Array int [ ] arr =new int [10] ; int i; String str = “ ” ; //Enters the values in the array arr[0]=5; arr[1]=10; 12-12

arr[2]=15; arr[3]=20; arr[4]=25; arr[5]=30; arr[6]=35; arr[7]=40; arr[8]=45; arr[9]=50; //Print the values of the Array 12-13

Message(“The Entered Array Element Are :“); for( i=0; i<=9; i++) { str += x[ i] + “\ n ”; } Message(str); } 12-14

THE OUTPUT IS: The Entered Array Element Are : 12-15