You are on page 1of 7

Dale Roberts

Department of Computer and Information Science, Department of Computer and Information Science,
SchooI of Science, IUPUI SchooI of Science, IUPUI
CSCI 230
Arrays
Declarations
Dale Roberts, Lecturer Dale Roberts, Lecturer
IUPUI IUPUI
droberts@cs.iupui.edu droberts@cs.iupui.edu
Dale Roberts
rrays rrays
rray rray
Group of consecutive memory locations Group of consecutive memory locations
Same name and type, ex: an array of integers Same name and type, ex: an array of integers
%o refer to an eIement, specify %o refer to an eIement, specify
Array name Array name
Position number of particular element in the array Position number of particular element in the array
Format: Format:
array_name array_name[ [ position number position number ] ]
irst element at position irst element at position 0 0
n n element array named element array named c c::
c[ 0 ] c[ 0 ], , c[ 1 ] c[ 1 ]... ...c[ c[ n n - - 1 1 ] ]
Example Example: : int my_array[12] int my_array[12]
my_array[0]= my_array[0]= - -45 45 value stored value stored
Position number must be an integer number or an Position number must be an integer number or an
integer expression integer expression
Example Example: : my_array[1.5] my_array[1.5] ERROR!! ERROR!!
my_array[i+j] my_array[i+j] valid if valid if i i and and j j are integers are integers
ame of array (ote that aII
eIements of this array have the
same name, my_array)
Position number of the eIement
within array my_array
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
my_array[0]
my_array[1]
my_array[2]
my_array[3]
my_array[4]
my_array[5]
my_array[6]
my_array[7]
my_array[8]
my_array[9]
my_array[10]
my_array[11]
Dale Roberts
rrays rrays (cont.) (cont.)
rray eIements are Iike normaI variabIes rray eIements are Iike normaI variabIes
my_array[8] my_array[8] = = - -3; 3;
scanf("%d", &my_array[8]); printf("%d",my_array[8]); scanf("%d", &my_array[8]); printf("%d",my_array[8]);
Perform operations in subscript. f Perform operations in subscript. f x x equals equals 3: 3:
my_array[ 5 my_array[ 5 - - 2 ] == my_array[ 3 ] == my_array[ x ] 2 ] == my_array[ 3 ] == my_array[ x ]
DecIaring rrays DecIaring rrays
hen declaring arrays, specify hen declaring arrays, specify
Name Name
Type of array Type of array
Number of elements: Number of elements: arrayType arrayName[numberOfElements]; arrayType arrayName[numberOfElements];
Examples Examples: :
int c[ 100 ]; int c[ 100 ]; /* reserve memory sufficient enough to store 100 /* reserve memory sufficient enough to store 100
elements of type integer */ elements of type integer */
float myArray[ 3284 ]; float myArray[ 3284 ];
Dale Roberts
rrays rrays (cont.) (cont.)
eclaring multiple arrays of same type: eclaring multiple arrays of same type: format similar to regular variables format similar to regular variables
Example Example:: int b[ 100 ], x[ 27 ]; int b[ 100 ], x[ 27 ];
Arrays may be declared to contain other data types Arrays may be declared to contain other data types
Example Example:: int a[ 100 ]; int a[ 100 ];
float b[ 100 ]; float b[ 100 ];
char c[ 100 ]; /* char c[ 100 ]; /* Strings are stored by using character arrays Strings are stored by using character arrays */ */
Example Example: :
#include <stdio.h> #include <stdio.h>
/* a simple program that uses arrays */ /* a simple program that uses arrays */
main( main(
{ {
int i, array_int[100]; int i, array_int[100];
for (i=0; i<100; i++) for (i=0; i<100; i++)
array_int[i]=0; array_int[i]=0;
for (i=0; i<100; i++) for (i=0; i<100; i++)
printf(element %d: %d printf(element %d: %d\ \n, i, array_int[i]); n, i, array_int[i]);
} }
Dale Roberts
rrays rrays (cont.) (cont.)
InitiaIizers InitiaIizers
int n[5] = {1, 2, 3, 4, 5}; int n[5] = {1, 2, 3, 4, 5};
Example Example:: main() main()
{ {
int i, a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int i, a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (i=0; i<10; i++) for (i=0; i<10; i++)
printf(Element: %d printf(Element: %d\ \n, a[i]); n, a[i]);
} }
f there are fewer initializations than elements in the array, then the f there are fewer initializations than elements in the array, then the
remaining elements are automatically initialized to remaining elements are automatically initialized to 0. 0.
int n[5] = {0} int n[5] = {0} /* /* All elements 0 All elements 0 */ */
int a[10] = {1, 2} int a[10] = {1, 2} /* a[2] /* a[2] to to a[9] a[9] are initialized to zeros are initialized to zeros */ */
int b[5] = {1, 2, 3, 4, 5, 6} int b[5] = {1, 2, 3, 4, 5, 6} /* /* syntax error syntax error */ */
C arrays have no bounds checking C arrays have no bounds checking
f size omitted, initializers determine it f size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 }; /* int n[ ] = { 1, 2, 3, 4, 5 }; /* 5 initializers, therefore 5 element array 5 initializers, therefore 5 element array */ */
Scalable Arrays: a better programming style Scalable Arrays: a better programming style
#define SIZE 10 #define SIZE 10
int c[SIZE]; int c[SIZE];/* defines a symbolic constant size with value 10 */ /* defines a symbolic constant size with value 10 */
Dale Roberts
rrays rrays (cont.) (cont.)
ExampIe:
#include <stdio.h
#define SIZE 100
main()
,
int i, a[SIZE]; int i, a[SIZE];
int sum = 0; int sum = 0;
. .
for (i=0; i < SIZE; i++) for (i=0; i < SIZE; i++)
sum = sum + a[i]; sum = sum + a[i];
printf(sum: %d printf(sum: %d\ \n, sum); n, sum);
} }
Dale Roberts
1 /* Fig. 6.8: fig06_08.c
2 Histogram printing program */
3 #include <stdio.h>
4 #define SIZE 10
5
6 int main()
7 {
8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
9 int i, j;
10
11 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );
12
13 for ( i = 0; i <= SIZE - 1; i++ ) {
14 printf( "%7d%13d ", i, n[ i ]) ;
15
16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */
17 printf( "%c", '*' );
18
19 printf( "\n" );
20 }
21
22 return 0;
23 }
Element Value Histogram
0 19 *******************
1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *
. InitiaIize array
2. Loop
3. Print
Program output

You might also like