Вы находитесь на странице: 1из 19

Introduction to Arrays

Arrays Structures of related data items Static entity same size throughout program Group of consecutive memory locations Same name and type First element at position 0

Name that this same

of array (Note all elements of array have the name, c)

c c [0] [1] c c [2] [3] c [4] c c [5] [6] c [7] c c [8] c[9] [10] c [11]

-4 5 6 0 72 154 3-8 9 0 62 -3 1 645 3 78

Position number of the element within array c

Array Declaration
When declaring arrays, specify Name Type of array Number of elements arrayType arrayName[ numberOfElements ]; Examples: int c[ 10 ]; float myArray[ 3284 ]; Declaring multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ];

Array Initialization
int n[ 5 ] = { 1, 2, 3, 4, 5 }; int n[ 5 ] = { 0 } //All elements 0 C arrays have no bounds checking

If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };

5 initializers, therefore 5 element array Enter the elements of array //&a for(int i=0;i<=n-1;i++) { scanf(%d,&a[i]); &a[0]; } scanf(%d,&a[0]); scanf(%d,&a[1]); scanf(%d,&a[2]); scanf(%d,&a[3]);

Array Printing/accessing
Array elements are like normal variables c[ 0 ] = 3; printf( "%d", c[ 0 ] ); Elements are for(int i=0;i<=3;i++) { printf(%d,a[i]); }

printf(%d,a[0]) printf(%d,a[1]) printf(%d,a[2]) printf(%d,a[3])

; ; ; ;

programs
a.1 Accept elements from user n display a.2 Avg and sum, a.3 Count of even and odd nos. a.4 Finding largest/minimum of user entered no. a.9 Displaying fibonacci series using array a.5 searching an element a.6 Sorting using bubble sort a.7 searching using function a.8 bubble sort using function

Dynamic array size not allowed in turbo c


#include <stdio.h> int main(void) { int n,i; printf("\n enter the value of n: "); scanf("%d", &n); int a[n]; // declaration not allowed here, needs constant printf("\n enter the values one by one\n"); for(i=0;i<n; ++i) scanf("%d", &a[i]); printf("\n entered numbers are.....\n"); for(i=0;i<n;++i) printf("\n %d",a[i]); return 0; }

Searching an element in array and display index if element found

Function Call and Prototype


function2(c, 5)

Address of the first element passed to pointer variable indicated with brackets

Number of array elements passed as a simple variable

void function2 (double b[ ], int num_elem);

Sorting (bubble sort) n=6 pass=5


Pass1 42 60 26 55 34 28 42 26 55 34 28 60 Pass 2 26 42 34 28 55 60 Pass3 26 34 28 42 55 60 Pass 4 26 28 34 42 55 60 Pass5 26 28 34 42 55 60

Introduction to Two-Dimensional Arrays


Two-dimensional arrays are understood as rows and columns Declaration Statement Storage Allocation Array Initialization

How to declare a multidimensional array?

int b[2][3];
declares the name of the array to be b the type of the array elements to be int the dimension to be 2 (two pairs of brackets []) the number of elements or size to be 2*3 = 6

for(i=0;i<3;i++) { for(j=0;j<4;j++) { scanf(%d,&a[i][j]); //printf(%d,a[i][j]); } }

Write a program to accept m*n matrix and display it in natural form

Addition of two matrices

Вам также может понравиться