0% found this document useful (0 votes)
26 views

CS12 - Computer Programming 1 1

The document discusses passing arrays to functions in C programming. It explains that array elements can be passed by value or reference like other variables. Entire arrays can also be passed to functions by passing the address of the first element and the dimensions of the array. Examples are provided to demonstrate passing individual array elements by value and reference, as well as passing an entire array to a function. Functions are also demonstrated that find the maximum value in an array, add two arrays element-wise, and validate unique numbers in an array.

Uploaded by

Lea Dela Rama
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CS12 - Computer Programming 1 1

The document discusses passing arrays to functions in C programming. It explains that array elements can be passed by value or reference like other variables. Entire arrays can also be passed to functions by passing the address of the first element and the dimensions of the array. Examples are provided to demonstrate passing individual array elements by value and reference, as well as passing an entire array to a function. Functions are also demonstrated that find the maximum value in an array, add two arrays element-wise, and validate unique numbers in an array.

Uploaded by

Lea Dela Rama
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 5

CS12 - Computer Programming 1

Chapter 5

Individual array elements can be passed by value or by reference just like all other variables!!! Pass-by-value example:
void printcard(int c) { if(c==1) Printf(A);; ... } void main() { int cards[5] ; ... for(int n=0; n<5; n++) printcard(card[n] ); }
Chapter 5

CS12 - Computer Programming 1

Pass-by-reference example:
void swap(int& x, int& y) { int temp; if (x > y){ temp = x; x = y; y = temp; } } void main() { int A[10] = {9,8,7,6,5,4,3,2,1,0}; swap(A[3], A[5]); }
Chapter 5 CS12 - Computer Programming 1 3

Chapter 5

CS12 - Computer Programming 1

Arrays can be passed to functions in their entirety. All that is required is the address of the first element and dimensions of the array. The remainder of the array will be passed by reference automatically

Chapter 5

CS12 - Computer Programming 1

//Find the largest value in an array //input: n - number of elements to check // a[ ] - array of elements // output:index to the largest element

#include <stdio.h> int max_element_index(int n, int a[]) { int max_index = 0; for (int i=1; i<n; i++) if (a[i] > a[max_index]) max_index = i; return max_index; } void main() { int A[10] = {9,8,7,6,5,4,10,2,1,0}; printf(%i\n,A[max_element_index(10,A)]); }
Chapter 5 CS12 - Computer Programming 1 6

//Add a[i] and b[i] and store the sum in c[i] //Array elements with subscripts ranging from //0 to size-1 are added element by element

void add_array(int size, double a[], double b[], double c[]){ for (int i=0; i<size; i++) c[i] = a[i] + b[i]; } main(){ add_array (5, x, y, z ); }
Chapter 5 CS12 - Computer Programming 1 7

unique.c Write a program that has a function named validate() that reads 20 numbers, each of which is between 10 and 100, inclusive. All the numbers will be stored in an array named one. As each number is read, validate it and store it in another array named unique only if it is not a duplicate of a number already read. Display the content of the two arrays. Provide for the "worst case" in which all 20 numbers are different. Provide error trappers for possible situations.

Chapter 5

CS12 - Computer Programming 1

You might also like