Lecture 4: Arrays & Functions
Objectives of this lecture
q Take a quick review of concepts assumed to be known
q Learn how to pass element of an array as a function
parameter
q Learn how to pass a whole array as a function
parameter
q Be able to apply these concepts in problem solving
C Concepts already known
q Data types
q Operators & Operator precedence
q Control Structures (If & Switch)
q Repetition
structure (for, while, do-while)
q Functions
q 1-D array
Individual Elements of Array as parameters
q Similar
to passing simple arguments
q The formal parameter should be a simple variable
q The actual parameter should be the array name followed
by the particular subscript in []
The following
example demonstrates this.
/* reads a 1-D array and prints the sum of
even and odd numbers */
#include <stdio.h>
#define SIZE 4
int odd(int num);
main()
{ int x[SIZE], i, oddsum, evensum;
printf("Enter %d integer
values\n", SIZE);
for (i=0; i<SIZE; i++)
scanf("%d",
&x[i]);
oddsum=0;
evensum=0;
for (i=0; i<SIZE; i++)
if (odd(x[i]))
oddsum=oddsum+x[i];
else
evensum=evensum+x[i];
printf("The sum of odd
elements is %d\n", oddsum);
printf("The sum of even
elements is %d\n", evensum);
return 0;
}
/* determines if an element is odd */
int odd(int num)
{ int isodd;
isodd = num%2==1;
return isodd;
}
Passing the whole array as a parameter
q Unlike simple variables, array is not passed into a
function, but rather its address.
q This is done by specifying the array name followed by
braces [] (size is not necessary.
q This makes processing more efficient
q The actual parameter is the array name (no braces)
Example 1:
Problem:
Each faculty in the ICS department has one of the ID’s: 1, 2, 3, …,
15 to use for photocopying. The data collected after using the photocopier
for one week is in the order of usage as shown below:
ID PAPERS USED
3 15
1 40
2 25
3 10
……..
2 50
0 0
The last entry being the sentinel. Write a program to compute:
1.
The
number of papers used by each faculty
2.
The
total number used by all faculties
3.
The
faculty the used the highest number of papers
Reasoning: We
use a 1-D array of size 15 to sum the papers used by each faculty in the cell
corresponding to his id. The sums are
computed as the data is entered until 0 0 is read.
Formal
statement: Paper usage problem
Input: a sequence of ID and Paper used.
Output:
total for each faculty, overall total, id with max paper
Processing: sum up for each id, sum for all id, find maximum
Solution:
Step
1: LOOP 15 times
Sum[I]=0
Step
2: WHILE data remains
a. INPUT id, papers
b. sum[id]=sum[id]+papers
Step
3: total=0
Step
4: LOOP 15 times
total=total+sum[id]
Step
5: OUTPUT header
LOOP
15 times
IF sum[id]>0
OUTPUT
id, sum[id]
Step
6: OUTPUT total
Step
7: max=sum[0], pos=1
Step
8: LOOP 14 times
IF
sum[id]>max
a. max=sum[id]
b. pos=id+1
step
9: OUTPUT pos
step
10: STOP
#include <stdio.h>
#define SIZE 15
/* Find the number of papers used by each faculty
of ICS Dept. who used the
photocopier*/
void process_data (int sum[]);
int add_all (int sum[]);
int id_max (int sum[]);
void print_sum(int sum[]);
main ()
{ int total,max,sum[SIZE];
/* read and sum-up the data
for each faculty */
process_data(sum);
/* get the overall total */
total=add_all(sum);
/* get the faculty with
maximum usage */
max=id_max(sum);
/* print the total of each
faculty */
print_sum(sum);
printf("The tatal number
of paper used is %d\n", total);
printf("The faculty with
max. usage is %d\n",max);
return 0;
}
/* read and sum-up the data for each faculty */
void process_data (int sum[])
{ int id, paper;
for (id=0; id<SIZE; id++)
sum[id]=0;
printf("Enter ID and
Paper used, (0 0 to end)\n");
scanf("%d%d",&id, &paper);
while (id>0 &&
id<SIZE)
{
sum[id-1]=sum[id-1]+paper;
scanf("%d%d",&id,
&paper);
}
}
/* get the overall total */
int add_all(int sum[])
{ int id, total;
total=0;
for (id=0; id<SIZE; id++)
total = total+sum[id];
return total;
}
/* get the faculty with maximum usage */
int id_max(int sum[])
{ int id, max, pos;
pos=0;
max=sum[pos];
for (id=1; id<SIZE; id++)
if (sum[id]>max)
{ max=sum[id];
pos=id;
}
return pos+1;
}
/* print the total of each faculty */
void print_sum(int sum[])
{ int id;
printf("FAC_ID PAPER_USED\n");
for (id=0; id<SIZE; id++)
if (sum[id]>0)
printf ("%4d%8d\n", id+1, sum[id]);
}