Arrays: P. Gupta
Arrays: P. Gupta
Arrays: P. Gupta
P. Gupta
Arrays
• Many applications require multiple data items
that have common characteristics.
▫ In mathematics, we often express such groups of
data items in indexed form:
x1, x2, x3, …, xn
3 numbers 4 numbers
if ((a <= b) && (a <= c)) if ((a <= b) && (a <= c) && (a <= d))
min = a; min = a;
else else
if (b <= c) if ((b <= c) && (b <= d))
min = b; min = b;
else else
min = c; if (c <= d)
min = c;
else
min = d;
The Problem
• Suppose we have 10 numbers to handle; or 20;
or 100.
• Where do we store the numbers ? Use 100
variables ??
• How to tackle this problem?
• Solution:
▫ Use arrays.
Using Arrays
X is a 10-element one
dimensional array
Declaring Arrays
a[x+2] = 25;
b[3*x-y] = a[10-x] + 5;
A Warning
• In C, while accessing array elements, array
bounds are not checked.
• Example:
int marks[5];
:
:
marks[8] = 75;
▫ The above assignment would not necessarily cause
an error.
▫ Rather, it may result in unpredictable program
results.
Initialization of Arrays
• General form:
type array_name[size] = { list of values };
• Examples:
int marks[5] = {72, 83, 65, 80, 76};
char name[4] = {„A‟, „m‟, „i‟, „t‟};
• Some special cases:
▫ If the number of values in the list is less than the
number of elements, the remaining elements are
automatically set to zero.
float total[5] = {24.2, -12.5, 35.1}
total[0]=24.2, total[1]=-12.5, total[2]=35.1,
total[3]=0, total[4]=0
Contd.
▫ The size may be omitted. In such cases the
compiler automatically allocates enough space for
all initialized elements.
• C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th)
location receives the null character „\0‟.
min = 99999;
for (i=0; i<10; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
}
Alternate
Version 1 #include <stdio.h>
#define size 10
main()
{
int a[size], i, min;
Student 1 75 82 90 65 76
Student 2 68 75 80 70 72
Student 3 88 74 85 76 80
Student 4 50 65 68 40 70
Contd.
• Examples:
x[m][n] = 0;
c[i][k] += a[i][j] * b[j][k];
a = sqrt (a[j*3][k]);
How is a 2-D array stored in memory?
• Starting from a given memory location, the elements
are stored row-wise in contiguous memory locations.
x: starting address of the array in memory
c: number of columns
k: number of bytes allocated per array element
▫ a[i][j] is allocated memory location at
address x + (i * c + j) * k
a[0]0] a[0][1] a[0]2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
scanf (“%d %d”, &n1, &n2, &n3); for (p=0; p<n1; p++)
{
for (p=0; p<n1; p++) printf (“\n”);
for (q=0; q<n2; q++) for (q=0; q<n3; q++)
scanf (“%d”, &a[p][q]); printf (“%f ”, c[p][q]);
for (p=0; p<n2; p++) }
for (q=0; q<n3; q++) }
scanf (“%d”, &b[p][q]);
Some Exercise Problems to Try Out
• Find the mean and standard deviation of a set of
n numbers.
• A shop stores n different types of items. Given
the number of items of each type sold during a
given month, and the corresponding unit prices,
compute the total monthly sales.
Passing Arrays to Function
• Array element can be passed to functions as
ordinary arguments.
IsFactor (x[i], x[0])
sin (x[5])
Passing Entire Array to a Function
• An array name can be used as an argument to a
function.
▫ Permits the entire array to be passed to the
function.
▫ The way it is passed differs from that for ordinary
variables.
• Rules:
▫ The array name must appear by itself as
argument, without brackets or subscripts.
▫ The corresponding formal argument is written in
the same manner.
Declared by writing the array name with a pair of empty
brackets.
Whole array as Parameters
#define ASIZE 5
float average (int a[]) {
int i, total=0;
for (i=0; i<ASIZE; i++)
total = total + a[i];
return ((float) total / (float) ASIZE);
}
main ( ) {
int x[ASIZE] ; float x_avg;
x = {10, 20, 30, 40, 50}
x_avg = average (x) ;
}
Contd.
main()
{
int n;
float list[100], avg;
We do not need to :
write the array avg = average (n, list);
size. It works with :
arrays of any size. }
#include <stdio.h>