CHP-2 ARRAYS.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
Introduction to arrays
One Dimensional Arrays
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.
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.
Array.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
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.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Hassan Khosravi / Geoffrey Tien
Data Structure and Algorithms
Today’s Material Arrays Definition Declaration Initialization
EKT120 : Computer Programming
Objectives Identify the built-in data types in C++
Program to search an element of array using linear search.
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Array 9/8/2018.
Array Array Array Dimension 3. One dinensional array
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Visit for more Learning Resources
Module 2 Arrays and strings – example programs.
ARRAYS An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: 1. One-dimensional arrays 2. Multi-Dimensional arrays.
JavaScript: Functions.
Visit for more Learning Resources
Buy book Online -
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
Chapter 5 POINTERs.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
EKT120: Computer Programming
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Data Structures (CS212D) Week # 2: Arrays.
Multidimensional array
Arrays Week 2.
Single-Dimensional Arrays chapter6
CHAPTER 2 Arrays and Vectors.
In C Programming Language
Multi-Dimensional Arrays
CHAPTER 2 Arrays and Vectors.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
C++ Array 1.
Arrays Imran Rashid CTO at ManiWeber Technologies.
Course Outcomes of Programming In C (PIC) (17212, C203):
Dr. Khizar Hayat Associate Prof. of Computer Science
Visit for more Learning Resources
Presentation transcript:

CHP-2 ARRAYS

1.INTRODUCTION An array is a collection of data elements of similar data types. The data elements grouped in an array can be of any basic data types like integer, float or character or user-defined data types.

2.ARRAYS One of the data types, which can be used for storing a list of elements, is an array. Whenever programmers want to store a list of elements under a single variable name but still want to access and manipulate an individual element of the list, arrays are used. Arrays are defined as fixed size sequence of elements of the same data type. These elements are stored at continuous memory locations and can be accessed sequentially or randomly. The program can access a particular element of an array by using one or more indices or subscripts. If only one subscript is used, an array is known as the single- dimensional array. If more than one subscript is used, an array is known as the multi- dimensional array.

3.SINGLE-DIMENSIONAL ARRAYS A single-dimensional array is defined as an array in which only one subscript value is used to access its elements. It is the simplest form of an array. Generally, a single-dimensional array is denoted as array_name [L:U] where, array_name = the name of the array L = the lower bound of the array U = the upper bound of the array Before using an array in a program, it needs to be declared. The syntax of declaring a Single-dimensional array in C is data_type array_name[size]; data_type = data type of elements to be stored in array array_name = name of the array s i z e = the size of the array indicating that the lower bound of the array is 0 and the upper bound is s i z e -1. Hence, the value of the subscript ranges from 0 to s i ze -1. For example, in the statement int abc [ 5 ] , an integer array of five elements is declared and the array elements are indexed from 0 to 4. Once the compiler reads a single-dimensional array declaration, it allocates a specific amount of memory for the array. Memory is allocated to the array at the compile-time before the program is executed.

Initializing and Accessing Single-Dimensional Array An array can be initialized in two ways: by declaring and initializing it simultaneously or by accepting elements for the already declared array from the user. These elements can be accessed by using the combination of the array name and the subscript value. Program 2.1: A program to illustrate initialization of two arrays and display their elements #include<stdio.h> #include<conio.h> void main () { int A[5]={1,2,3,4,5}; int B[5l, i; clrscr() ; printf("Enter the elements of array B:\n"); for (i=O; i<5; i++) printf("Enter the element:"); scanf("%d", &B[il); printf("Elements of array A: \n"); for (i=O;i<5;i++) printf("%d\t", A[il); printf("\nElements of array B: \n"); printf("%d\t", B[il); getch (); }

Initializing and Accessing Single-Dimensional Array The output of the program is Enter the elements of array B: Enter the element: 6 Enter the element: 7 Enter the element: 8 Enter the element: 9 Enter the element: 10 Elements of array A: 1 2 3 4 5 Elements of array B: 6 7 8 9 10 Once an array is declared and initialized, various operations, such as traversing, searching, insertion, deletion, sorting and merging can be performed on an array. To perform any operation on an array, the elements of the array need to be accessed. The process of accessing each element of an array is known as traversal.

Program 2.2: A program to illustrate the traversal of an array #include<stdio.h> #include<conio.h> void main () { int ARR[5]; Int i,sum=0; clrscr(); for (i=O;i<3;i++) printf("Enter the elements of the array:\n"); scanf("%d", &ARR[i]); } printf("%d", ARR[i]); Sum=sum + ARR[i]; Printf(“Sum=%d”,sum); getch();}

The output of the program is Enter the elements of the array: 12 23 34 45 56 The elements of the array are: Sum of elements of an array: 170

4.Two-Dimensional arrays A two-dimensional array is defined as an array in which two subscript values are used to access an array element. Two-dimensional arrays are useful when the elements being arranged in the form of rows and columns (matrix form). Generally, a two-dimensional array is represented as A[Lr : Ur, Lc : Uc] where, Lr and Lc = the lower bounds of the row and the column, respectively Ur and Uc = the upper bounds of the row and the column, respectively The syntax of declaring a two-dimensional array in C is data type array_name [row_size] [column_size]; For example, in the statement int a [3] [3] an integer array of three rows and three columns is declared.

Initializing and Accessing Two-dimensional Arrays Like a single-dimensional array, a two-dimensional array can also be initialized in two ways: by declaring and initializing the array simultaneously and by accepting array elements from the user. Once a two-dimensional array is declared and initialized, the array elements can be accessed anytime. two-dimensional array elements are also accessed by using the combination of the name of the array and subscript values.

Program 2.3: A program to illustrate traversal of a matrix (two-dimensional array) and finding sum of its elements #include<stdio.h> #include<conio.h> void main () { int ARR[3] [3], i, j; clrscr(); printf("Enter the elements of matrix A: \n"); for(i=O;i<3;i++) for(j=O;j<3;j++) Printf(“Enter elements\n”); scanf ("%d", &ARR[i] [j]); } printf ("%d", &ARR[i] [j]); Sum=sum+ARR[i][j]; Printf(“sum=%d”,sum); getch ();

The output of the program is Enter the elements of matrix A: 1 2 3 4 5 6 7 8 9 Sum of elements of a matrix is: 45

5.Three-Dimensional Arrays A three-dimensional array is defined as an array in which three subscript values are used to access an individual array element. The three-dimensional array can be declared as shown here. int A[3] [3] [3];

Program 2.4: A program to illustrate the traversal of a three-dimensional array #include<stdio.h> #include<conio.h> void main () { int ARR[3] [3] [3], i, j, k; clrscr(); for(i=O;i<3;i++) for (j=O;j<3;i++) for (k=O;k<3;k++) printf("Enter the elements of an array A(3x3x3) :\n"); scanf("%d", &ARR[i] [j] [k]); }