Docsity

Passing Arrays and Functions in C: An Overview, Slides of Computer Fundamentals

Partial preview of the text

Download Passing Arrays and Functions in C: An Overview and more Slides Computer Fundamentals in PDF only on Docsity!

• It is shipped in every standard library with compiler

• Most major programming languages give some kind

of random number generator as a function as part of

library

• Writing a random number generator is itself a field

Importance of rand ( )

Outline

fig06_09.c (Part 1

of 2)

6.5 Passing Arrays to Functions

  • Passing arrays
    • To pass an array argument to a function, specify the name of

the array without any brackets

int myArray[ 24 ];

myFunction( myArray, 24 );

  • Array size usually passed to function
  • Arrays passed call-by-reference
  • Name of array is address of first element
  • Function knows where the array is stored
  • Modifies original memory locations
  • Passing array elements
  • Passed by call-by-value
  • Pass subscripted name (i.e., myArray[ 3 ]) to function

6.5 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

Outline

fig06_13.c (Part 1

of 3)

Outline

fig06_13.c (Part 2

of 3)

Outline

Program Output

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

Outline

fig06_14.c (Part 1

of 2)

6.6 Sorting Arrays

  • Sorting data
    • Important computing application
    • Virtually every organization must sort some data
  • Bubble sort (sinking sort)
    • Several passes through the array
    • Successive pairs of elements are compared
      • If increasing order (or identical ), no change
      • If decreasing order, elements exchanged
    • Repeat
  • Example:
    • original: 3 4 2 6 7
    • pass 1: 3 2 4 6 7
    • pass 2: 2 3 4 6 7

Outline

fig06_15.c (Part 1

of 3)