Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
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.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Chapter 8 Arrays and Strings
Write a C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Programming for Engineers
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Arrays. Arrays are objects that help us organize large amounts of information.
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)
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays.
MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Array in C# Array in C# RIHS Arshad Khan
Two-Dimensional Arrays
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
CHP-2 ARRAYS.
Module 2 Arrays and strings – example programs.
EKT150 : Computer Programming
Declaration, assignment & accessing
Review of Arrays and Pointers
Introduction To Programming Information Technology , 1’st Semester
Multidimensional array
7 Arrays.
Visit for more Learning Resources
Presentation transcript:

Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming for different purposes such as sorting, searching and etc.  Arrays allow you to store a group of data of a single type.  Arrays are efficient and useful for performing operations. You can use them to store a set of high scores in a video game, a 2 dimensional map layout, or store the coordinates of a multi-dimensional matrix for linear algebra calculations.

Arrays  Arrays are of two types single dimension array and multi-dimension array.  Each of these array type can be of either static array or dynamic array.  Static arrays have their sizes declared from the start and the size cannot be changed after declaration.  Dynamic arrays that allow you to dynamically change their size at runtime, but they require more advanced techniques such as pointers and memory allocation

Arrays A single dimension array is represented be a single column. A multiple dimensional array would span out n columns by n rows. Single Dimension Arrays Declaring Single Dimension Arrays Arrays can be declared using any of the data types available in C. Array size must be declared using constant value before initialization. A single dimensional array will be useful for simple grouping of data that is relatively small in size. You can declare a single dimensional array as follows:

Arrays Array - a collection of a fixed number of components wherein all of the components have the same data type One-dimensional array - an array in which the components are arranged in a list form The general form of declaring a one-dimensional array is: dataType arrayName[intExp]; where intExp is any expression(width) that evaluates to a positive integer

Declaring an array The statement int num[5]; declares an array num of 5 components of the type int The components are num[0], num[1], num[2], num[3], and num[4]

Accessing Array Components The general form (syntax) of accessing an array component is: arrayName[indexExp] where indexExp, called index, is any expression whose value is a nonnegative integer Index value specifies the position of the component in the array The [] operator is called the array subscripting operator The array index always starts at 0

Arrays In C language the end of string is marked by the null character ''. Hence to store a group of 3 possible string data. We declare the array as char game_map[4]; This applies for char type array. One-dimensional string array containing 3 elements. Array Element game_map[0] game_map[1] game_map[2 ] game_map[3] DataSR D One-dimensional integer array containing 3 elements.

Initializing Single Dimension Arrays Array can be initialized in two ways, initializing on declaration or initialized by assignment. Initializing on Declaration If you know the values you want in the array at declaration time, you can initialize an array as follows: Syntax: Sample Code array_name[size_of_array] = {element 1, element 2,...}; Example: Sample Code char game_map[3] = {'S', 'R', 'D'}; This line of code creates an array of 3 chars containing the values 'S', 'R ', and 'D'

Initialized by Assignment Sample Code char game_map[3]; game_map[0] = 'S'; game_map[1] = 'R'; game_map[2] = 'D'; Accessing Single Dimension Array Elements Arrays are 0-indexed, so the first array element is at index = 0, and the highest is size_of_array – 1. To access an array element at a given index you would use the following syntax: array_name[index_of_element];

Example: #include void main(void) { int a[5]; int i; for(i = 0;i<5;i++) { a[i]=i; } for(i = 0;i<5;i++) { printf(“ value of I is = %d\n",i,a[i]); } } Output 1 value of I is = 0 2 value of I is = 1 3 value of I is = 2 4 value of I is = 3 5 value of I is = 4

Program to calculate sum of given 10 numbers Include Main() { int i, sum, m ; int s[100]; printf(“how many numbers”); scanf(“%d”,&m); printf(“enter the numbers\n”); for(i=0; i<=m; ++i) { scanf(“%d”, &s[i]); } Sum=0; for(i=0; i<=m; ++i) { sum = sum+s[i]; } printf(“the sum of the number is %d”, sum); getch(); }

Now we are going to learn how we can use two dimensional arrays (2D arrays) to store values. Because it is a 2D array so its structure will be different from one dimension array. The 2D array is also known as Matrix or Table, it is an array of array. See the below image, here each row is an array. Declaration of 2D array: Syntax: data_type array_name[row_size][column_size]; Example: int arr[3][3]; So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).

2D Array Memory Map of 2D Array

Program prints the elements of 2*2 matrix Main() { int a[20] [20],i,j; a[1] [1]=2;a[1] [2]=3; a[2] [1] =4; a[2] [2]=6; for (i=1;i<=2; ++i) { for(j=1;j<=2;++i) { Printf(“%d”,a[i][j]); } Printf(“\n”); }

Sum Main() { int a[20] [20],sum int i,j,m,n; printf(“enter the order m*n of the matrix\n”); scanf (“%d %d”, &m, &n); printf (“enter the elements of the matrix\n”); for(i=1;i<=m;++i) { for( j=1; j<=n; ++j) scanf(“%d”, &a[1][j]); } Sum = 0; For(i=1;i<=m; ++i) { for(j=1;j<=n,++j) sum += a [i] [j]; } Printf(“ sum of all the elements is %d”, sum); Getch(); }