0% found this document useful (0 votes)
3 views16 pages

Lecture #14

The document discusses the concept of passing arrays to functions in C programming, highlighting that arrays are passed by reference, allowing modifications to the original array. It explains how to pass entire arrays and individual elements, along with examples demonstrating functions for finding maximum values, summing elements, and reversing arrays. Additionally, it covers the syntax for defining function parameters that accept arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Lecture #14

The document discusses the concept of passing arrays to functions in C programming, highlighting that arrays are passed by reference, allowing modifications to the original array. It explains how to pass entire arrays and individual elements, along with examples demonstrating functions for finding maximum values, summing elements, and reversing arrays. Additionally, it covers the syntax for defining function parameters that accept arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

M ü h e ndislik ve M imarlık Fakü lte si

BİL1102
Programlamaya Giriş

Ders #14 1D Arrays and Functions

Doç.Dr. Gonca Gökçe MENEKŞE DALVEREN

2024 Güz
Passing Arrays to Functions (1)
• To pass an array argument to a function, specify the array’s name without any brackets.

• Ex int myArray[24];
myfunction(myArray, 24);

• Arrays passed call-by-reference

1-2
Passing Arrays to Functions (2)
• C automatically passes arrays to functions by reference - the called
functions can modify the element values in the callers’ original arrays.

• The name of the array evaluates to the address of the first element of the
array.

• Because the starting address of the array is passed, the called function
knows precisely where the array is stored.
• Therefore, when the called function modifies array elements in its function
body, it’s modifying the actual elements of the array in their original memory
locations.

• An array name is really the address of the first element of the array.

1-3
Passing Arrays to Functions (5)
Passing array elements

•Passed by call-by-value
•Pass subscripted name (myArray[3]) to function
Function prototype

void modifyArray( int b[ ], int arraySize );

•Paramater names optional in prototype


• int b[ ] could be written int [ ]
• int arraySize could be simply int

1-6
Passing Arrays to Functions (6)
Passing array elements

•Although entire arrays are passed by reference,


individual array elements are passed by value exactly
as simple variables are.
•Such simple single pieces of data (such as individual
integers, floats and chars) are called scalars.
•To pass an element of an array to a function, use the
indexed name of the array element as an argument in
the function call.

1-7
Passing arrays to Functions Ex
void fun(int arr[])
or
void fun(int *a)
1) Passing an element of an array
Determine if an array element is even.
int even (int);
int main()
{ int arr[10], i;
for (i=0; i < 10; i++)
{ scanf(“%d”, &arr[i]);
if (even(arr[i])) int even (int a)
printf(“%d is even”, arr[i]); {
else
printf(“%d is odd”, arr[i]);
if (a % 2 == 0)
} return(1);
return(0); else
} return(0);
}

1-9
2) Passing the whole array
Write a function to find the sum of an array element is even.
????

int sumarr(int [], int);


int main()
{ int arr1[ ] = {7, 9 ,11};
printf(“sum of array = %d”, sumarr(arr1,3));
}
int sumarr(int x[], int length)
{ int sum =0, i;
for (i=0; i < length; i++)
sum+= x[i];
return(sum);
}

1-10
2) Passing the whole array
Because arrays are implemented as pointers, they are always
passed as call by reference.
void change(int[ ], int);
int main()
{ int one[ ] = {3, 5, 7}, i;
change(one, 3);
for (i=0; i < 3; i++)
printf(“%d”, one[i]);
}
void change(int xarr[ ], int s) OUTPUT
{ int i; 4 6 8
for (i=0; i < s; i++)
xarr[i]++;
}

1-11
Example1
Find the max of a 5-element int array
#include <stdio.h>
int findmax(int [], int);
int main()
{ int a[ ] = {5, 4, 6, 8, 1};
printf(“max of the array : %d”, findmax(a, 5));
}
int findmax(int arr[ ], int size)
{ int i, max=arr[0];
for (i=1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return(max);
}

1-12
Example2
Declare 2 10-element double arrays & input their elements using
a function.
#include <stdio.h>
void input( double [], int size);
int main()
{ double a[10], b[10];
input(a, 10);
input(b, 10);
....
}
void input(double p[ ], int s)
{ int i;
for (i=0; i < s; i++)
scanf(“%lf”, &p[i]);
}

1-13
Example3
Write a program that inputs the size of the array n(<=100) &
then reads n elements into the arrays x. Using a function, the
program should find the reverse of the array.

Eg x: 5 6 7 8

reverse x : 8 7 6 5

1-14
void reverse(int size, int arr[])
Example3
#include <stdio.h> { int i, temp;
void reverse(int size, int a[]); int mid = size / 2;
int main() for (i=0; i < mid; i++)
{ int x[100], n, i; {
printf(“Enter the size of the array:”); temp= arr[i];
scanf(“%d”, &n); arr[i] = arr[size- i -1];
printf(“Enter array elements:”); arr[size- i -1] = temp;
for (i=0; i < n; i++) }
scanf(“%d”, &x[i]); }
reverse(n, x);
printf(“Reversed array elements:”);
for (i=0; i < n; i++)
scanf(“%d”, &x[i]);
}
...
1-15
Arrays as Function Parameters

1-16
The square brackets next to the formal parameter arr indicates
that it is an array. We don’t write the size of the array within the
brackets, but instead we pass it as another parameter to the
function.
double maxar (double arr[], int size)

While we are calling a function with an array parameter, we just


write the name of the array as the actual parameter, because
we pass the entire array as a parameter, not only one element
of it. This is the only place where an array name is used alone,
thus without an index in brackets. Ex:

maximum = maxar (ages, 5);


Find the maximum value in two arrays, a with 50 elements and
b with 60 elements.

double a[50], b[60], max_a, max_b;


...
max_a = maxar(a, 50);
max_b = maxar(b, 60);

printf (“Max value in two arrays = ”);


if (max_a > max_b)
printf (“%f”, max_a);
else
printf (“%f”, max_b);
Searching Arrays (cont.)
Searching an Array with Linear Search

1-36

You might also like