MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.

Slides:



Advertisements
Similar presentations
One Dimensional Arrays
Advertisements

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.
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
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
More Questions 1) Write a C program to read a matrix A of size nXn and two arrays X and Y of size n and calculate XA-Y?
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Array.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
1 Arrays 1090CS, Computer Programming 1 Manesh T
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
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.
Rray Programs. Insert and element in the given position of the Array rray.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
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.
Computer Programming for Engineers
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
UNIT - 3 ARRAYS AND STRINGS. Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional.
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.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
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.
Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
CHP-2 ARRAYS.
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Array 9/8/2018.
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.
Computer Graphics Matrix
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
CNG 140 C Programming (Lecture set 8)
Chapter 2 Array and String Visit to more Learning Resources.
UNIT - 3 Noornilo Nafees.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Arrays Imran Rashid CTO at ManiWeber Technologies.
Visit for more Learning Resources
Presentation transcript:

MAHENDRAN

Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays

Array is defined as a set of homogeneous data items. An Array is a group of elements that share a common name that are differentiated from one another by their positions within the array Datatype arrayname[subscript]; 1)Arrayname should be a valid “C” variable 2)Arrayname should be unique 3)The elements in the array should be of same type 4)Subscript (array size) cannot be negative 5)Subscript must always be an integer

Strings Arrays A string can be defined as a character type array, which is terminated by a null character. Output -

Single or One Dimensional Arrays Arrays whose elements are specified by one subscript are called One dimensional array or linear array. Syntax : datatype arrayname[size]; For Example : Note : By default array index should starts with zero (0)

Write a program for entering data into an array & Reading data from an array #include void main() { int arr[10],I,n; printf("\n ENter N Elements"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter arr[%d]=",i); scanf("%d",&arr[i]); } for(i=0;i<n;i++) { printf("%d\n",arr[i]); } Enter N Elements : 3 Enter arr[0] : 2 Enter arr[1] : 5 Enter arr[2] : Array Initialization #include void main() { int a[5]={10,20,30,40,50}; int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n",a[i]); } getch(); }

Write a “C” program to sort the given number is in ascending order using one dimensional array #include void main() { int i,j,n, a[10],temp; printf("\n size of vector="); scanf("%d",&n); printf("vector elements:"); for (i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("\n\nElements in asending order is=\n"); for(i=0;i<n;i++) printf("%8.2d",a[i]); printf("\n\nElements in descending order is=\n"); for(i=n-1;i>=0;i--) printf("%8.2d",a[i]); getch(); }

Two Dimensional Arrays A Arrays whose elements are specified by two subscript such as row and column are called One dimensional array or linear array. Row  means horizontally Column  means vertically A two - dimensional array looks like a school time-table consisting of rows and columns. A two – dimensional array is declared as -

Two Dimensional Array Initialization The result of the above assignment will be as follows :

Write a “C” program to perform the addition of two matrices #include void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Input A - Matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Input B - Matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("Sum of A and B Matrix=\n"); for(i=0;i<3;++i) { for(j=0;j<3;++j) printf("%5d",c[i][j]); printf("\n"); } Write a “C” program to perform the subtraction of two matrices #include void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Input A - Matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Input B - Matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]-b[i][j]; printf("Sum of A and B Matrix=\n"); for(i=0;i<3;++i) { for(j=0;j<3;++j) printf("%5d",c[i][j]); printf("\n"); }

Write a “C” program to sort the given names in Alphabetical order using One dimensional array #include void main() { int i,j,n; char a[10][10],temp[10]; printf("\n Enter the N Values"); scanf("%d",&n); printf("Enter the Names one by one :\n"); for(i=0;i<n;i++) { scanf("%s",&a[i]); } for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if((strcmp(a[i],a[j]))>0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } printf("The Names in Alphabetical Order is =\n"); for(i=0;i<n;i++) printf("\n%s",a[i]); } Write a “C” program to perform matrix multiplication using two dimensional array #include void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k; printf("Input row and column of A matrix \n"); scanf("%d %d",&n,&m); printf(" Input row and column of B matrix \n"); scanf("%d %d",&p,&q); if(n==q){ printf(" Matrices can be Multiplied: \n"); printf(" Input A-matrix \n"); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf("%d",&a[i][j]); printf(" Input B-matrix \n"); for(i=0;i<p;++i) for(j=0;j<q;++j) scanf("%d",&b[i][j]); printf("The resultant matrix is\t:\n"); for(i=0;i<n;++i){ for(j=0;j<m;++j){ c[i][j]=0; for(k=0;k<m;++k) c[i][j]=c[i][j]+a[i][k]*b[k][j]; printf("%5d",c[i][j]);} printf("\n");}} else printf("Matrices cannot be multiplied \n"); }

Write a “C” program to find the largest and smallest numbers given in the array #include void main() { int i,n; float a[20],large,small; printf("\nEnter the N values="); scanf("%d",&n); printf("Enter the values one by one :\n"); for(i=0;i<n;i++){ scanf("%f",&a[i]);} large=a[0]; for(i=1;i<n;i++){ if(a[i]>large) large=a[i];} small=a[0]; for(i=1;i<n;i++){ if(a[i]<small) small=a[i];} printf("Largest element is = %6.2f\n",large); printf("Smallest element = %6.2f\n",small); }

Session Summary  Arrayname should be a unique and valid “C” Variable name  The number of elements in a multi dimensional array is the product of its subscripts  Arrays can be initialized to the same type in which they are declared  The character array receives the terminating ‘\0’ in the string constant  The individual values in the array are called as elements  It is not necessary to specify the length of an array, explicitly in case if initializers are provided for the array during declaration itself

EXERCISES 1. Write a program to search an element and to find how many times it is present in the array? 2.Write a program to find the sum of diagonal elements in a matrix 3. Write a program to find the second largest number in an array? 4. Write a program to remove the duplicate elements of the array? 5. Write a program to merge two arrays and print the merged array in ascending order? 6.Write a program to insert an element into an sorted array of integers? 7.Write a program to display only the negative elements of the array?