©LPU CSE101 C Programming CSE101-Lec#17 • Arrays • (Arrays and Functions)
©LPU CSE101 C Programming Outline • To declare an array • To initialize an array • To pass an array to a function
©LPU CSE101 C Programming Introduction • Arrays – Collection of related data items of same data type. – Static entity – i.e. they remain the same size throughout program execution
©LPU CSE101 C Programming Arrays • Array – Group of consecutive memory locations – Same name and data type • To refer to an element, specify: – Array name – Position number in square brackets([]) • Format: arrayname[position_number] – First element is always at position 0 – Eg. n element array named c: • c[0], c[1]...c[n – 1] Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c 3 c[6] -45 6 0 72 -89 0 62 -3 1 6453 78 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4]
©LPU CSE101 C Programming Arrays • An array is an ordered list of values c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 c The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9
©LPU CSE101 C Programming Arrays • Array elements are like normal variables c[0] = 3;/*stores 3 to c[0] element*/ scanf (“%d”, &c[1]);/*reads c[1] element*/ printf (“%d, %d”, c[0], c[1]); /*displays c[0] & c[1] element*/ • The position number inside square brackets is called subscript/index. • Subscript must be integer or an integer expression c[5 - 2] = 7; (i.e. c[3] = 7)
©LPU CSE101 C Programming Defining Arrays • When defining arrays, specify: – Name – Data Type of array – Number of elements datatype arrayName[numberOfElements]; – Examples: int students[10]; float myArray[3284]; • Defining multiple arrays of same data type – Format is similar to regular variables – Example: int b[100], x[27];
©LPU CSE101 C Programming Initializing Arrays • Initializers int n[5] = { 1, 2, 3, 4, 5 }; – If not enough initializers given, then rightmost elements become 0 – int n[5] = { 0 }; // initialize all elements to 0 – C arrays have no bounds checking. • If size is omitted, initializers determine it int n[] = { 1, 2, 3, 4, 5 }; – 5 initializers, therefore 5 element array.
©LPU CSE101 C Programming Initializing Arrays • Array is same as the variable can prompt for value from the user at run time. • Array is a group of elements so we use for loop to get the values of every element instead of getting single value at a time. • Example: int array[5], i; // array of size 5 for(i=0;i<5;i++){// loop begins from 0 to 4 scanf(“%d”, &array[i]); }
©LPU CSE101 C Programming Program of Initializing an array to zero using loop.
©LPU CSE101 C Programming Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 0 n[6] 0 0 0 0 0 0 0 0 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] 0
©LPU CSE101 C Programming Program of Initializing an array element with calculations using loop.
©LPU CSE101 C Programming Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 10 n[6] 4 6 8 12 14 16 18 20 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] 2
©LPU CSE101 C Programming Total of array element values is 383 Program to compute sum of elements of array
©LPU CSE101 C Programming array = 0012FF78 &array[0] = 0012FF78 &array = 0012FF78 #include <stdio.h> /* function main begins program execution */ int main() { char array[ 5 ]; /* define an array of size 5 */ printf( " array = %pn&array[0] = %pn" " &array = %pn", array, &array[ 0 ], &array ); return 0; /* indicates successful termination */ } /* end main */ Program to explain the address of array
©LPU CSE101 C Programming Character Arrays • Character arrays – Character arrays can be initialized using string literals char string1[] = "first"; – It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '0' }; • Null character '0' terminates strings • string1 actually has 6 elements – Can access individual characters string1[ 3 ] is character ‘s’ – Array name is address of array, so & not needed for scanf scanf( "%s", string2 ); • Reads characters until whitespace encountered
©LPU CSE101 C Programming , Program to print character array as strings.
©LPU CSE101 C Programming Enter a string: Hello string1 is: Hello string2 is: string literal
©LPU CSE101 C Programming Passing Arrays to Function • Arrays can be passed to functions in two ways: 1. Pass entire array 2. Pass array element by element
©LPU CSE101 C Programming Pass entire array • Here entire array can be passed as an argument to the function • Function gets complete access to the original array • While passing entire array Address of first element is passed to function, any changes made inside function, directly affects the Original value. void modifyArray(int b[], int arraySize); • Function passing method: “ Pass by Address”
©LPU CSE101 C Programming Pass array element by element • Here individual elements are passed to the function as argument • Duplicate carbon copy of Original variable is passed to function • So any changes made inside function does not affects the original value • Function doesn’t get complete access to the original array element. void modifyElement(int e); • Function passing method: “ Pass by Value”
©LPU CSE101 C Programming Passing Arrays to Functions • Function prototype void modifyArray(int b[], int arraySize); – Parameter names optional in prototype • int b[] could be written int [] • int arraySize could be simply int void modifyArray(int [], int); • Function call int a[SIZE]; modifyArray(a, SIZE);
©LPU CSE101 C Programming Passing arrays and individual array elements to functions
©LPU CSE101 C Programming
©LPU CSE101 C Programming
©LPU CSE101 C Programming Effects of passing entire array by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6
©LPU CSE101 C Programming cse101@lpu.co.in Next Class: Applications of Arrays

Array and functions

  • 1.
    ©LPU CSE101 CProgramming CSE101-Lec#17 • Arrays • (Arrays and Functions)
  • 2.
    ©LPU CSE101 CProgramming Outline • To declare an array • To initialize an array • To pass an array to a function
  • 3.
    ©LPU CSE101 CProgramming Introduction • Arrays – Collection of related data items of same data type. – Static entity – i.e. they remain the same size throughout program execution
  • 4.
    ©LPU CSE101 CProgramming Arrays • Array – Group of consecutive memory locations – Same name and data type • To refer to an element, specify: – Array name – Position number in square brackets([]) • Format: arrayname[position_number] – First element is always at position 0 – Eg. n element array named c: • c[0], c[1]...c[n – 1] Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c 3 c[6] -45 6 0 72 -89 0 62 -3 1 6453 78 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4]
  • 5.
    ©LPU CSE101 CProgramming Arrays • An array is an ordered list of values c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 c The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9
  • 6.
    ©LPU CSE101 CProgramming Arrays • Array elements are like normal variables c[0] = 3;/*stores 3 to c[0] element*/ scanf (“%d”, &c[1]);/*reads c[1] element*/ printf (“%d, %d”, c[0], c[1]); /*displays c[0] & c[1] element*/ • The position number inside square brackets is called subscript/index. • Subscript must be integer or an integer expression c[5 - 2] = 7; (i.e. c[3] = 7)
  • 7.
    ©LPU CSE101 CProgramming Defining Arrays • When defining arrays, specify: – Name – Data Type of array – Number of elements datatype arrayName[numberOfElements]; – Examples: int students[10]; float myArray[3284]; • Defining multiple arrays of same data type – Format is similar to regular variables – Example: int b[100], x[27];
  • 8.
    ©LPU CSE101 CProgramming Initializing Arrays • Initializers int n[5] = { 1, 2, 3, 4, 5 }; – If not enough initializers given, then rightmost elements become 0 – int n[5] = { 0 }; // initialize all elements to 0 – C arrays have no bounds checking. • If size is omitted, initializers determine it int n[] = { 1, 2, 3, 4, 5 }; – 5 initializers, therefore 5 element array.
  • 9.
    ©LPU CSE101 CProgramming Initializing Arrays • Array is same as the variable can prompt for value from the user at run time. • Array is a group of elements so we use for loop to get the values of every element instead of getting single value at a time. • Example: int array[5], i; // array of size 5 for(i=0;i<5;i++){// loop begins from 0 to 4 scanf(“%d”, &array[i]); }
  • 10.
    ©LPU CSE101 CProgramming Program of Initializing an array to zero using loop.
  • 11.
    ©LPU CSE101 CProgramming Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 0 n[6] 0 0 0 0 0 0 0 0 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] 0
  • 12.
    ©LPU CSE101 CProgramming Program of Initializing an array element with calculations using loop.
  • 13.
    ©LPU CSE101 CProgramming Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 10 n[6] 4 6 8 12 14 16 18 20 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] 2
  • 14.
    ©LPU CSE101 CProgramming Total of array element values is 383 Program to compute sum of elements of array
  • 15.
    ©LPU CSE101 CProgramming array = 0012FF78 &array[0] = 0012FF78 &array = 0012FF78 #include <stdio.h> /* function main begins program execution */ int main() { char array[ 5 ]; /* define an array of size 5 */ printf( " array = %pn&array[0] = %pn" " &array = %pn", array, &array[ 0 ], &array ); return 0; /* indicates successful termination */ } /* end main */ Program to explain the address of array
  • 16.
    ©LPU CSE101 CProgramming Character Arrays • Character arrays – Character arrays can be initialized using string literals char string1[] = "first"; – It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '0' }; • Null character '0' terminates strings • string1 actually has 6 elements – Can access individual characters string1[ 3 ] is character ‘s’ – Array name is address of array, so & not needed for scanf scanf( "%s", string2 ); • Reads characters until whitespace encountered
  • 17.
    ©LPU CSE101 CProgramming , Program to print character array as strings.
  • 18.
    ©LPU CSE101 CProgramming Enter a string: Hello string1 is: Hello string2 is: string literal
  • 19.
    ©LPU CSE101 CProgramming Passing Arrays to Function • Arrays can be passed to functions in two ways: 1. Pass entire array 2. Pass array element by element
  • 20.
    ©LPU CSE101 CProgramming Pass entire array • Here entire array can be passed as an argument to the function • Function gets complete access to the original array • While passing entire array Address of first element is passed to function, any changes made inside function, directly affects the Original value. void modifyArray(int b[], int arraySize); • Function passing method: “ Pass by Address”
  • 21.
    ©LPU CSE101 CProgramming Pass array element by element • Here individual elements are passed to the function as argument • Duplicate carbon copy of Original variable is passed to function • So any changes made inside function does not affects the original value • Function doesn’t get complete access to the original array element. void modifyElement(int e); • Function passing method: “ Pass by Value”
  • 22.
    ©LPU CSE101 CProgramming Passing Arrays to Functions • Function prototype void modifyArray(int b[], int arraySize); – Parameter names optional in prototype • int b[] could be written int [] • int arraySize could be simply int void modifyArray(int [], int); • Function call int a[SIZE]; modifyArray(a, SIZE);
  • 23.
    ©LPU CSE101 CProgramming Passing arrays and individual array elements to functions
  • 24.
    ©LPU CSE101 CProgramming
  • 25.
    ©LPU CSE101 CProgramming
  • 26.
    ©LPU CSE101 CProgramming Effects of passing entire array by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6
  • 27.
    ©LPU CSE101 CProgramming cse101@lpu.co.in Next Class: Applications of Arrays

Editor's Notes

  • #6 This will allocate 10*2 bytes of space in memory