Array One dimensional array 1 Introduction Arrays are

  • Slides: 14
Download presentation
Array One dimensional array 1

Array One dimensional array 1

Introduction: � Arrays are data structures consisting of related data items of the same

Introduction: � Arrays are data structures consisting of related data items of the same type. � Array is a group of memory that all have the same name. � To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. 2

Introduction: � The first element in every array is the zeroth element. � Thus

Introduction: � The first element in every array is the zeroth element. � Thus the first element in the c array is c[0]. � The second element of c array is c[1]. � The last element in c array is c[i-1]. 3

Introduction A[0] A[1] A[2] A[3] A[4] A[5] index 0 1 2 3 4 5

Introduction A[0] A[1] A[2] A[3] A[4] A[5] index 0 1 2 3 4 5 value 45 3 100 30 �A is the name of array � Index is the position of array � Value the data save in particular index 4

How use array in scanf and printf ? void main ( ) { int

How use array in scanf and printf ? void main ( ) { int A[6], i; A[0] A[1] A[3] A[4] = = 45; 3; 100; 30; for ( i= 0; i<=5; i++) Printf(“n A[ %d ] = %d”, i, A[i]); } 5

How use array in scanf and printf ? void main ( ) { int

How use array in scanf and printf ? void main ( ) { int A[6], i; A[0] A[1] A[3] A[4] = = 45; 3; 100; 30; for ( i= 0; i<=5; i++) scanf(“ %d “, A[i]); for ( i= 0; i<=5; i++) Printf(“n A[ %d ] = %d”, i, A[i]); } 6

Initialization void main ( ) { int B[ ] = {5, 12, 30, 2,

Initialization void main ( ) { int B[ ] = {5, 12, 30, 2, 0, 99, 44, 77}; int i; for ( i= 0; i<=7; i++) Printf(“n B[ %d ] = %d”, i, B[i]); B[3] = B[2] + B[6]; B[0] = B[5] – B[7]; B[8] = B[4] *10; // error out of array range (out of memory) Printf(“n B[ 3 ] = %d”, B[3]); Printf(“n B[ 0 ] = %d”, B[0]); Printf(“n B[ 8 ] = %d”, B[8]); //error out of memory } 7

Initialization void main ( ) { int C[100 ] ; int i; for (

Initialization void main ( ) { int C[100 ] ; int i; for ( i= 0; i< 100; i++) Printf(“n C[ %d ] = %d”, i, C[i]); for ( i= 0; i< 100; i++) C[ i ] = i; for ( i= 0; i< 100; i++) Printf(“n C[ %d ] = %d”, i, C[i]); } 8

Initialization void main ( ) { int C[100 ] ; int i; for (

Initialization void main ( ) { int C[100 ] ; int i; for ( i= 0; i< 100; i++) Printf(“n C[ %d ] = %d”, i, C[i]); for ( i= 0; i< 100; i++) C[ i ] = 0 ; for ( i= 0; i< 100; i++) Printf(“n C[ %d ] = %d”, i, C[i]); } void main ( ) { int C[100 ] = {0} ; int i; for ( i= 0; i< 100; i++) Printf(“n C[ %d ] = %d”, i, C[i]); } 9

Example # include <stdio. h> #define SIZE 1000 Define a global void main( )

Example # include <stdio. h> #define SIZE 1000 Define a global void main( ) variable { int N[SIZE]= {9, 10, 5, 2, 22, 7, 18, 99, 1000, 84}; int i, j; printf(“ n %s t %s”, “Element”, “Value”, “Histogram”); for(i=0; i< SIZE; i++) { printf(“%d t”, i, N[i]); for(j=0; j<= N[i]; j++ ) printf(“*”); } printf(“n”); } 10

Using character arrays to store manipulate string: � Character array can be initialized using

Using character arrays to store manipulate string: � Character array can be initialized using string literal. For example: char string 1[] = “first”; � In this case the size of array string 1 is determined by the compiler based on the length of the string. � Note that the string “first” contains 5 characters plus a special string termination character called null character ‘’. 11

Continue � Also character arrays can be initialized with individual character constant in an

Continue � Also character arrays can be initialized with individual character constant in an initializer list. char string 1={‘f’, ’i’, ’r’, ’s’, ‘t’, ‘’}; � We can printed as string or as character by character. For example: printf(“%sn”, string 1); printf(“%c ”, string 1[0]); for(j=0; string 1[j] != ‘’; j++) printf(“%c “, string 1[j]); 12

Continue void main( ) { char string 2[20]; printf(“n enter your name: “); scanf(“%s”,

Continue void main( ) { char string 2[20]; printf(“n enter your name: “); scanf(“%s”, string 2); } printf(“n My name is %s”, string 2); 13

Example #include<stdio. h> void main( ) { char s 1[20]; char s 2[ ]

Example #include<stdio. h> void main( ) { char s 1[20]; char s 2[ ] = “String Literal”; int i; printf(“Enter a string: “); scanf(“%s”, s 1); printf(“string 1 is: %s nstring 2 is: %sn”, s 1, s 2); for(i=0; s 1[i] !=‘’; i++) printf(“%c “, s 1[i]); } printf(“n”); 14