Vous êtes sur la page 1sur 40

ARRAYS

Contents

Introducing Arrays Classification of Arrays Declaring Array variables, Creating arrays & Initializing arrays. Copying Arrays. Arrays limitations. Searching & Sorting Arrays Multidimensional Arrays examples.

Introducing Arrays

Array is a datas tructure that represents collection of similar type of data with a single name. An Array of 10 elements of type int as follows:

Classification of Arrays
Classification of arrays: one-dimensional array: If there is one subscript, then it is called one-dimensional array Ex: int array[10]; Multi-dimensional array: 1.Two-If there are two subscripts, then it is called two-dimensional array. E.g.: int row[3]col[3]; 2.If there are three subscripts, then it is called three-dimensional array. E.g.: int p[5]r[4]c[4]; and so on.

Declaring Array variables


Syntax: data_ type array name [size]; Ex: int a[10]; float values[15]; ->float array stores 15 float
values.

char name[10];->char array stores10 characters

Declaring Array variables

Here a is called name of the array which stores 10 integers. [ ]->subscript, [10]->dimension or size of the array. The size specifies the max no. of elements the array can store. Total size= size*(size of (data _type)); Ex: int a[10];Total size=10*2=20bytes(for int memory allocation is 2 bytes) The individual elements of the array are a[0],a[1],..a[9] where 0,1,.9 are called the indexes of the array. Array index always starts from 0 and the last index=size of array-1.

Creating Arrays
data_

type array name [size]; Ex: int a[10]; Here a[0] references the first element of the array. a[9] references the last element of the array.

Array Initialization
Ex1: int array[5]={2,4,6,8,9}; Ex2: float f1[4]={134.8,125.6,150.0,166.2}; Ex3: int a[ ]={5,6,3,2,1}; Ex4: int marks[5]={35,78,65}; Ex5: int age[3]; a[0]=12; a[1]=5; a[2]=9;

Array Initialization

If an array is initialized, specifying size is optional. In Ex3 size is not specified, array automatically starts storing from a[0]th location onwards. In Ex4 a[0]=34,a[1]=78,a[2]=65,a[4]&a[5] is not initialized, so they will automatically set to zero. once an array is created ,its size is fixed. It cannot be changed. In Ex5 any number cannot be inserted at a[4],because it is not initialized.

Declaring, Creating& initializing using Shorthand notation


float list[4]={1.9, 2.9, 3.4, 3.5}; This Shorthand notation is equivalent to the following statements: list[0]=1.9; list[1]=2.9; list[2]=3.4; list[3]=3.5;

Allocating memory to arrays


Static arrays: Array created at compile time. Array has a fixed size. Cannot be modified at run time. The process of allocating memory at compile time is known as static memory allocation. Such arrays are static arrays. Works fine as long as we know exactly what our data requirement are.

Limitations of an Array
Limitations of an array: Boundary checking is not there in array. So the limitations are as follows: 1.Array overflow: If the no. of array elements read is greater than the maximum size specified in type declaration instruction, array overflow will occur. 2.Wastage of memory: If the no. of array elements read is less than the maximum size specified in type declaration instruction, Memory will be wasted.

Dynamic Memory Allocation


In order to overcome the limitations of static array, Dynamic memory allocation is used

Dynamic arrays

Allocating memory to arrays at run time is known as dynamic allocation. Such arrays are called dynamic arrays. They are created using pointer variables and memory management functions like malloc , calloc and realloc. These functions are included in header file <stdlib.h>. dynamic arrays is used in creating and manipulating data structures such as linked list , stacks and queues.

Write a program to accept n numbers & display the same using arrays.
#include <stdio.h> void main() { int a[20],i,n; printf (Enter the value of n \n); scanf (%d, &n); printf (\n Enter the array Elements); for (i=0; i<n; i++) scanf (%d, &a[i]); printf(\n Displayed Array Elements are :); for (i=0; i<n; i++) printf (%d \n, a[i]); }
Output: Enter the value of n 5 Enter the array Elements 12 3 4 6 8 Displayed Array Elements are: 12 3 4 6 8

Write a program to calculate the sum of n elements in an array& display the sum.
#include <stdio.h> void main() { int a[20],i,n,sum=0; printf (Enter the value of n \n); scanf (%d, &n); printf (\n Enter the array Elements); for (i=0; i<n; i++) { scanf (%d, &a[i]); sum= sum +a[i]; } printf(\n sum of Array Elements is :); printf (sum=%d \n, sum); }
Output: Enter the value of n 5 Enter the array Elements 12 3 4 6 8 sum of Array Elements is: 33

Write a program to accept two integer arrays &find the sum of the corresponding elements of these arrays.Output: #include <stdio.h>
void main() { int a[20],b[20],sum[20],i,n; printf (Enter the size of arrays A & B \n); scanf (%d, &n);

Enter the size of arrays A & B

4 2 3 4 5 8 3 4 9

Enter the array Elements of A

printf (\n Enter the array Elements of A \n); for (i=0; i<n; i++) scanf (%d, &a[i]); printf (\n Enter the array Elements of B \n); for (i=0; i<n; i++) scanf (%d, & b[i]); for (i=0; i<n; i++) sum [i]= a[i] + b[i]; printf(\n sum of elements of A&B \n ); for (i=0; i<n; i++) printf (%d \n, a[i]); }

Enter the array Elements of B

sum of elements of A&B

10 6 8 14

Write a program to print the largest element of an array.


#include <stdio.h> void main() { int a[20],i,n,large; printf (Enter the value of n \n); scanf (%d, &n); printf (\n Enter the array Elements); for (i=0; i<n; i++) scanf (%d, &a[i]); large=a[0];//assume a[0]is large for (i=1; i<n; i++) { if (a[i]> large) large= a[i]; } printf( Largest element is=%d \n, large); }
Output: Enter the value of n 5 Enter the array Elements 1 37 42 90 86 Largest element is=90

Write a program to generate Fibonacci series of n elements


#include <stdio.h> void main() { int a[10],i,n,; printf (Enter the series for how many numbers \n); scanf (%d, &n); //printf (\n Enter the array Elements); //for (i=0; i<n; i++) //scanf (%d, &a[i]); a[0]=0; a[1]=1; for (i=2;i<n; i++) { a[i]= a[i-1]+ a[i-2]; } printf (Fibonacci series is \n); for (i=0; i<n ; i++) printf (%d \n, a[i]); }

output: Enter the series for how many numbers 8 Fibonacci series is o 1 1 2 3 5 8 13

Searching of element in an Array


Searching: finding the position of searching element in an array. Types of searching 1.Linear searching: In this type, key (searching) element is compared with each of
the element in an array, this is time taking, if there are more no. of elements

2.Binary searching: In this

All array elements should be sorted finding the middle element & comparing whether that is only the key element if not, checking whether key element is less than or greater than middle element if the element is less than middle element, then no need of searching in greater than middle elements & vice versa. It reduces time for searching.

Linear search
#include <stdio.h> void main () { int array[10],i,n,key,found=0; printf (Enter the value of n \n); scanf (%d, &n); printf (Enter the elements one by one \n); for (i=0; i<n; i++) scanf (%d, &a[i]); printf (Input array is \n); printf (%d, a[i]); printf (enter the element to be searched \n); scanf (%d, &key);

//linear search begins for (i=0; i<n; i++) { if (key== array[i]) { found=1; break; } } if (found==1) printf (SUCCESSFUL SEARCH \n); else printf (search failed \n); }

Output: Enter the value of n 5 Enter the elements one by one 12 3 23 44 6 Input array is 12 3 23 44 6 Enter the element to be searched 56 Search failed

Binary Search
#include<stdio.h> void main () { int array[10], i, j, n, key, low, mid, high, temp; printf (Enter the value of n \n); scanf (%d, &n); printf (Enter the elements one by one \n); for (i=0; i<n; i++) scanf (%d, & a[i]); printf (Input array is \n); printf (%d, a[i]); printf (enter the element to be searched \n); scanf (%d, &key);

Binary search
//bubble sort begins for (i=0; i<n; i++) { for (j=o; j<(n-i-1); j++) { if (a [j]>a[j+1] { temp= a [j]; a [j]=a[j+1]; a[j+1]=temp; } } } printf (sorted array is \n); for (i=0; i<n; i++) printf (%d \n, a[i]);
/Binary search begins Low=1; High=n; Do { Mid=(low + high) /2; If (key > a [mid]) High=mid-1; Else if (key< a [mid]) Low=mid+1; }while (key!= a [mid] && low<=high) If (key==[mid]) printf (SUCCESFUL SEARCH \N); Else printf (search failed \n); }

Binary search
Output: Enter the value of n 5 Enter the elements one by one 12 3 23 44 6 Input array is 12 3 23 44 6 Enter the element to be searched 3 SUCCESSFUL SEARCH

Sorting
Sorting:

Arranging elements in an order either ascending or descending. Types of sorting: 1.Bubble sort 2.selection sort

Bubble sort
In each pass, we compare adjacent elements and swap them if they are out of order until the end of the list. By doing so, the 1st pass ends up bubbling up the largest element to the last position on the list 2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1 passes, the list is sorted. Example: Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17 45 89 | 68 90 29 34 17 45 68 | 89 29 34 17 68 89 | 90 29 34 17 68 89 | 29 34 17 89 90 | 29 34 17 29 89 | 34 17 29 90 | 34 17 34 89 | 17 34 90 | 17 17 89 17 90 45 68 89 29 34 17 90 45 68 29 34 17 89 largest 2nd largest
1.

Selection Sort
Selection Sort
1. 2.

3.

Start with the 1st element, scan the entire list to find its smallest element and exchange it with the 1st element Start with the 2nd element, scan the remaining list to find the the smallest among the last (N-1) elements and exchange it with the 2nd element 89 45 68 90 29 34 17 17 | 45 68 90 29 34 89 29 | 68 90 45 34 89 34 | 90 45 68 89 45 | 90 68 89 68 | 90 89 89 | 90 90

Example:

//logic for selection sort for (i=0;i<5;i++) { for (j=i+1;j<5;j++) { if (a [i]>a [j]) temp=a [i]; a [i]=a [j]; a [j]=temp; } } for (i=0;i<5;i++) printf ("%d", a[i]);

Two dimensional array

int table[2][3]={0,0,0,1,1,1}; int table[2][3]={{0,0,0},{1,1,1}};

Matrix addition
Two matrices can be added if and only if they have the same dimensions. Just add corresponding entries. for (i=0;i< row ; i++) for (j=0;j<col ;j++) A [i] [j]= a [i] [j] +b [i] [j];

Matrix subtraction
for (i=0;i< row ;i++) for (j=0;j< col ;j++) A [i] [j]= a [i] [j] b [i] [j];

for (i=0;i<2;i++) { for (j=0;j<2;j++) { printf ("%d ",arr1[i][j]); } } printf ("transpose\n"); for (i=0;i<2;i++) { for (j=0;j<2;j++) { printf ("%d ",arr1[j][i]); } }

Transpose

Matrix multiplication
[ 3 -2] * 4 5 -2*5]=[2] = [3 *4 +

#define R1 1 #define C1 4 #define R2 4 #define C2 3 int a[R1][C1] ,b[R2][C2],A[R1][C2]; If(C1!=R2) puts (multiplication not possible); else { for (i=0;i<R1;i++) for (j=0;j<C2;j++) A [i][j]=0; for (k=0;k<C1;k++)

[j];
}

A [i] [j]=A [i] [j] +a [i] [k]* b [k]

Questions?

Vous aimerez peut-être aussi