You are on page 1of 26

C Arrays

Computer Programming I

Carlos Alberto Hinojosa Montero


July 17, 2016
Universidad Industrial de Santander
Table of contents

1. Arrays

2. Defining Arrays

3. Passing Arrays to functions

4. Sorting Arrays

5. Multidimensional Array

6. Variable-length Arrays

1
Arrays
Arrays

Arrays are data structures consisting of related data items of the same
type, in contiguous memory locations.

2
Arrays

For example, if a = 5 and b = 6 , then the statement


c [ a + b ] += 2;

To print the sum of the values contained in the first three elements of
array c , wed write
printf ( " % d " , c [ 0 ] + c [ 1 ] + c [ 2 ] ) ;

To divide the value of element 6 of array c by 2 and assign the result to


the variable x , write
x = c [ 6 ] / 2;

3
Level of precedence

4
Defining Arrays
Defining Arrays

The following definition reserves 100 elements for integer array b and 27
elements to integer array x
int b [ 100 ] , x [ 27 ];

Defining an Array and Using a Loop to Initialize the Arrays Elements


1 # include < stdio .h >
2 int main ( void ) {
3 int n [ 10 ]; // n is an array of 10 i n t e g e r s
4 // int n [ 10 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0};
5 size_t i ; // counter
6 for ( i = 0; i < 10; ++ i ) {
7 n [ i ] = 0; // set element at l o c a t i o n i to 0
8 } // end for
9 printf ( " % s %13 s \ n " , " Element " , " Value " ) ;
10 // output c o n t e n t s of array n in tabular format
11 for ( i = 0; i < 10; ++ i ) {
12 printf ( " %7 u %13 d \ n " , i , n [ i ] ) ;
13 } // end for
14 } // end main
5
Defining Arrays

If there are fewer initializers than elements in the array, the remaining
elements are initialized to zero
int n [ 10 ] = { 0 }; // i n i t i a l i z e s entire array to zeros

Its important to remember that arrays are not automatically initialized to


zero. You have to do it.
You can omit the size of the array
int n [] = { 1 , 2 , 3 , 4 , 5 };

You can create an array using a symbolic constant, using #define


1 # include < stdio .h >
2 # define SIZE 10
3 int main ( void ) {
4 int n [ SIZE ] ;
5 ....
6 ...
7 }
6
Excersice

Write a C program that sum the values contained in the 12-element


integer array a. with a = 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45

7
Character Arrays

We’ve discussed only integer arrays. However, Arrays are capable of


holding data of any type.

The string ”first” contains 5 characters plus a string termination


character called null character i.e.

Because a string is really an array of characters, we can access individual


characters in a string directly using array subscript notation. For
example, string1[0] is the character ’f’ and string1[3] is the character ’s’ .
8
Character Arrays

We can also input a string directrly into a character array.

This Array can store a string of at most 19 characters and a terminating


null character.

In arrays the & (usually used in scanf clause) is not necesary since an
array is passed by reference.

9
Character Arrays

Printing the string stored in variable string2

Function printf, like scanf, does not check how large the character array
is. The characters of the string are printed until a terminating character
is encountered.

10
Passing Arrays to functions
Passing Arryas to functions

To pass an array argument to a function, specify the array’s name


without any brackets.
int h o u r l y T e m p e r a t u r e s [ HOU RS_IN_A_ DAY ];

the function call


modifyArray ( hourlyTemperatures , H OURS_IN_ A_DAY )

passes array hourlyTemperatures and its size to function modifyArray.


Recall that all arguments in C are passed by value. C automatically
passes arrays to functions by reference.

11
Passing Arryas to functions

An array name is really the address of the first element of the array, the
following example show the values of array , &array [0] and &array using
identifier %p
1 # include < stdio .h >
2 int main ( void ) {
3 char array [ 5 ]; // define an array of size 5
4 printf ( " array = % p \ n & array [0] = % p \ n & array = % p \ n " ,
array , & array [ 0 ] , & array ) ;
5 } // end main

Showing that name array is pointing to the same memory address.

12
Passing Arrays to functions

For a function to receive an array through a function call, the function’s


parameter list must specify that an array will be received.
void modifyArray ( int b [] , int size )

13
Sorting Arrays
Sorting Arryas

Following code shows algorithm called bubble sort.


1 // Sorting an array ’s values into a s c e n d i n g order .
2 # include < stdio .h >
3 # define SIZE 10
4 // f u n c t i o n main begins program e x e c u t i o n
5 int main ( void )
6 {
7 // i n i t i a l i z e a
8 int a [ SIZE ] = { 2 , 6 , 4 , 8 , 10 , 12 , 89 , 68 , 45 , 37
};
9 int pass ; // passes counter
10 size_t i ; // c o m p a r i s o n s counter
11 int hold ; // t e m p o r a r y l o c a t i o n used to swap array
elements
12 puts ( " Data items in original order " ) ;
13 // output o r i g i n a l array
14 for ( i = 0; i < SIZE ; ++ i ) {
15 printf ( " %4 d " , a [ i ] ) ;
16 } // end for

14
Sorting Arrays

1 // loop to control number of passes


2 for ( pass = 1; pass < SIZE ; ++ pass ) {
3 // loop to control number of c o m p a r i s o n s per
pass
4 for ( i = 0; i < SIZE - 1; ++ i ) {
5 // compare a d j a c e n t e l e m e n t s and swap them if first
6 // element is greater than second element
7 if ( a [ i ] > a [ i + 1 ] ) {
8 hold = a [ i ];
9 a [ i ] = a [ i + 1 ];
10 a [ i + 1 ] = hold ;
11 } // end if
12 } // end inner for
13 } // end outer for
14 puts ( " \ nData items in ascending order " ) ;
15 // output sorted array
16 for ( i = 0; i < SIZE ; ++ i ) {
17 printf ( " %4 d " , a [ i ] ) ;
18 } // end for
19 puts ( " " ) ;
20 } // end main 15
Excersice

Write a C program to compute Mean, median, and mode, using arrays


and functions.

16
Multidimensional Array
Multidimensional Arrays

It represents tables of values consisting of information arranged in rows


and columns.

17
Multidimensional array

A multidimensional array can be initialized when its defined, much like a


single-subscripted array
int b [ 2 ][ 2 ] = { { 1 , 2 } ,{ 3 , 4 } };

The values are grouped by row in braces. If there are not enough
initializers for a given row, the remaining elements of that row are
initialized to 0 . Thus,
int b [ 2 ][ 2 ] = { { 1 , } ,{ 3 , 4 } };

would initialize b[0][0] to 1 , b[0][1] to 0.

18
Variable-length Arrays
Variable-length Arrays

What if you dont know an arrays size at compilation time?, To handle


this, youd have to use dynamic memory allocation with malloc and
related functions. The C standard allows you to handle arrays of
unknown size using variable-length arrays (VLAs).
A variable-length array is an array whose length, or size, is defined in
terms of an expression evaluated at execution time.
1 int arraySize ;
2 printf ( " % s " , " Enter size of a one - dimensional array : " ) ;
3 scanf ( " % d " , & arraySize ) ;
4 int array [ arraySize ]; // declare 1 - D variable - length array

[Note: This feature is not supported in Microsoft Visual C++.]

19

You might also like