Sie sind auf Seite 1von 7

Department of Computer and Information Science,

School of Science, IUPUI

CSCI 230

Arrays
Declarations
Dale Roberts, Lecturer
IUPUI
droberts@cs.iupui.edu

Dale Roberts
Arrays
Array Name of array (Note that all
Group of consecutive memory locations elements of this array have the
same name, my_array)
Same name and type, ex: an array of integers
To refer to an element, specify my_array[0] -45
Array name my_array[1] 6
Position number of particular element in the array my_array[2] 0

Format: my_array[3] 72

array_name[ position number ] my_array[4] 1543

First element at position 0 my_array[5] -89

n element array named c: my_array[6] 0


c[ 0 ], c[ 1 ]...c[ n 1 ] my_array[7] 62
Example: int my_array[12] my_array[8] -3
my_array[0]= -45 value stored 1
my_array[9]
Position number must be an integer number or an 6453
my_array[10]
integer expression 78
my_array[11]
Example: my_array[1.5] ERROR!!
my_array[i+j] valid if i and j are integers
Position number of the element
within array my_array

Dale Roberts
Arrays (cont.)
Array elements are like normal variables
my_array[8] = -3;
scanf("%d", &my_array[8]); printf("%d",my_array[8]);
Perform operations in subscript. If x equals 3:
my_array[ 5 - 2 ] == my_array[ 3 ] == my_array[ x ]
Declaring Arrays
When declaring arrays, specify
Name
Type of array
Number of elements: arrayType arrayName[numberOfElements];
Examples:
int c[ 100 ]; /* reserve memory sufficient enough to store 100
elements of type integer */
float myArray[ 3284 ];

Dale Roberts
Arrays (cont.)
Declaring multiple arrays of same type: format similar to regular variables
Example: int b[ 100 ], x[ 27 ];
Arrays may be declared to contain other data types
Example: int a[ 100 ];
float b[ 100 ];
char c[ 100 ]; /* Strings are stored by using character arrays */
Example:
#include <stdio.h>
/* a simple program that uses arrays */
main(
{
int i, array_int[100];

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


array_int[i]=0;
for (i=0; i<100; i++)
printf(element %d: %d\n, i, array_int[i]);
}

Dale Roberts
Arrays (cont.)
Initializers
int n[5] = {1, 2, 3, 4, 5};
Example: main()
{
int i, a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (i=0; i<10; i++)
printf(Element: %d\n, a[i]);
}
If there are fewer initializations than elements in the array, then the
remaining elements are automatically initialized to 0.
int n[5] = {0} /* All elements 0 */
int a[10] = {1, 2} /* a[2] to a[9] are initialized to zeros */
int b[5] = {1, 2, 3, 4, 5, 6} /* syntax error */
C arrays have no bounds checking
If size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 }; /* 5 initializers, therefore 5 element array */
Scalable Arrays: a better programming style
#define SIZE 10
int c[SIZE]; /* defines a symbolic constant size with value 10 */

Dale Roberts
Arrays (cont.)
Example:
#include <stdio.h>
#define SIZE 100
main()
{
int i, a[SIZE];
int sum = 0;

for (i=0; i < SIZE; i++)
sum = sum + a[i];
printf(sum: %d\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;
1. Initialize array
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 ]) ;
2. Loop
15
16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */
17 printf( "%c", '*' );
18
3. Print
19 printf( "\n" );
20 } Program output
21 Element Value Histogram
22 return 0; 0 19 *******************
23 } 1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *

Dale Roberts

Das könnte Ihnen auch gefallen