0% found this document useful (0 votes)
6 views27 pages

9.0 Array

This document provides an overview of arrays in the C programming language, detailing their definition, initialization, and usage. It explains one-dimensional and multidimensional arrays, including how to access and manipulate their elements. The document also highlights common practices, such as using macros for array sizes and the importance of checking array bounds to prevent errors.

Uploaded by

c.sunjid707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

9.0 Array

This document provides an overview of arrays in the C programming language, detailing their definition, initialization, and usage. It explains one-dimensional and multidimensional arrays, including how to access and manipulate their elements. The document also highlights common practices, such as using macros for array sizes and the importance of checking array bounds to prevent errors.

Uploaded by

c.sunjid707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAMMING LANGUAGE

1 [EEE]
Lecture 9: Array
M. Arifur Rahman
Scalar Variables versus
Aggregate Variables
• So far, the only variables we’ve seen are scalar: capable
of holding a single data item.

• C also supports aggregate variables, which can store


collections of values.

• There are two kinds of aggregates in C: arrays and


structures.

2
One-Dimensional Arrays
• An array is a data structure containing a number of data
values, all of which have the same type.
• These values, known as elements, can be individually
selected by their position within the array.
• The simplest kind of array has just one dimension.
• The elements of a one-dimensional array a are
conceptually arranged one after another in a single row
(or column).

3
Defining Arrays
• When defining arrays, specify
 Name
 Type of array (Array can be defined over any type)
 Number of elements
o arrayType arrayName[numberOfElements];
 Examples:
o float grade[7];
o int c[10];
• Defining multiple arrays of same type
 Format similar to regular variables
 Example:
o int b[ 100 ], x[ 27 ];
4
Array Elements
• To refer to an element, specify
 Array name for the collection
 Position number for the member
• Format:
o arrayname[position number]
 First element at position 0
 n element array named c:
o c[ 0 ], c[ 1 ]...c[ n – 1 ]

5
One-Dimensional Arrays
• To declare an array, we must specify the type of the
array’s elements and the number of elements:
int a[10];
• The elements may be of any type; the length of the array
can be any (integer) constant expression.
• Using a macro to define the length of an array is an
excellent practice:
#define N 10

int a[N];

6
Array Subscripting
• To access an array element, write the array name
followed by an integer value in square brackets.
• This is referred to as subscripting or indexing the array.
• The elements of an array of length n are indexed from 0
to n – 1.
• If a is an array of length 10, its elements are designated
by a[0], a[1], …, a[9]:

7
Array Subscripting
• Expressions of the form a[i] are lvalues, so they can be
used in the same way as ordinary variables:
a[0] = 1;
printf("%d\n", a[5]);
++a[i];

• In general, if an array contains elements of type T, then


each element of the array is treated as if it were a
variable of type T.

8
Array Subscripting
• Many programs contain for loops whose job is to perform
some operation on every element in an array.
• Examples of typical operations on an array a of length N:
for (i = 0; i < N; i++)
a[i] = 0; /* clears a */

for (i = 0; i < N; i++)


scanf("%d", &a[i]); /* reads data into a */

for (i = 0; i < N; i++)


sum += a[i]; /* sums the elements of a */
9
Array Subscripting
• C doesn’t require that subscript bounds be checked; if a
subscript goes out of range, the program’s behavior is
undefined.
• A common mistake: forgetting that an array with n
elements is indexed from 0 to n – 1, not 1 to n:
int a[10], i;

for (i = 1; i <= 10; i++)


a[i] = 0;
With some compilers, this innocent-looking for statement
causes an infinite loop.

10
Using Arrays: Averaging Grades
float grade[7], total;
/* initialize grades somehow */
total = grade[0] + grade[1] + grade[2] + grade[3] +
grade[4] + grade[5] + grade[6];
printf(“average = %f\n”, total/7.0);

• Or,
for (int i=0; i<7; i++)
total+= grade[i];
printf(“average = %f\n”, total/7.0);

• Actually, we can use arrays efficiently with loops


11
Array Initialization
• An array, like any other variable, can be given an initial
value at the time it’s declared.
• The most common form of array initializer is a list of
constant expressions enclosed in braces and separated
by commas:
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

12
Array Initialization
• If an initializer is present, the length of the array may be
omitted:
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

• The compiler uses the length of the initializer to


determine how long the array is.

13
BREAK

14
Static Array Sizes
• The size of array should be static; no dynamic array in C
 The following is wrong:
int x;
scanf(“%d”, &x);
int temp[x];

• You should use dynamic memory allocation like malloc()


for dynamically-sized arrays

 15
C Does Not Check Array Bounds
double grade[7];
int i = 9;
grade[i] = 3.5;
/* Is i out-of-range? If so, any error message ?*/

• You should check it for yourself if not sure


if (0 <= i && i < 7)
/* OK to access grade[i] */
else
printf(“Array Index %d out-of-range.\n”, i);

 16
Program: Checking a Number
for Repeated Digits
• The repdigit.c program checks whether any of the digits
in a number appear more than once.
• After the user enters a number, the program prints either
Repeated digit or No repeated digit:
Enter a number: 28212
Repeated digit
The number 28212 has a repeated digit (2); a number like 9357 doesn’t

 17
repdigit.c

/* Checks numbers for repeated digits */


#include <stdio.h>
int main(void)
{
int digit_seen[10] = {0};
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
while (n > 0) {
digit = n % 10;
digit_seen[digit]++;
n /= 10;
}

 18
Multidimensional Arrays
• An array may have any number of dimensions.
• The following declaration creates a two-dimensional
array (a matrix, in mathematical terminology):
int m[5][9];
• m has 5 rows and 9 columns. Both rows and columns are
indexed from 0:

 19
Multidimensional Arrays
• To access the element of m in row i, column j, we must
write m[i][j].
• The expression m[i] designates row i of m, and m[i][j]
then selects element j in this row.

 20
Multidimensional Arrays
• Although we visualize two-dimensional arrays as tables,
that’s not the way they’re actually stored in computer
memory.
• C stores arrays in row-major order, with row 0 first, then
row 1, and so forth.
• How the m array is stored:

 21
Multidimensional Arrays
• Nested for loops are ideal for processing multidimensional arrays.
• Consider the problem of initializing an array for use as an identity
matrix. A pair of nested for loops is perfect:

double ident[10][10];
int row, col, N=10;

for (row = 0; row < N; row++)


for (col = 0; col < N; col++)
if (row == col)
ident[row][col] = 1.0;
else
ident[row][col] = 0.0;
 22
Initializing a Multidimensional
Array
• We can create an initializer for a two-dimensional array
by nesting one-dimensional initializers:
int m[5][9] = { {1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 1}};
• Initializers for higher-dimensional arrays are constructed
in a similar fashion.
• C provides a variety of ways to abbreviate initializers for
multidimensional arrays
 23
Initializing a Multidimensional
Array
• If an initializer isn’t large enough to fill a
multidimensional array, the remaining elements are
given the value 0.
• The following initializer fills only the first three rows of
m; the last two rows will contain zeros:
int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 1, 0, 0, 1, 0}};

 24
Initializing a Multidimensional
Array
• If an inner list isn’t long enough to fill a row, the
remaining elements in the row are initialized to 0:
int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1},
{0, 1, 0, 1, 1, 0, 0, 1},
{1, 1, 0, 1, 0, 0, 0, 1},
{1, 1, 0, 1, 0, 0, 1, 1, 1}};

 25
Initializing a Multidimensional
Array
• We can even omit the inner braces:
int m[5][9] = {1, 1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 1, 1};
• Once the compiler has seen enough values to fill one row,
it begins filling the next.

 26
End of
Lecture

27

You might also like