Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Capitolo 6 - Array
Array
Array
Outline
2
Nome dellarray
(Tutti gli elementi
di questo array
hanno lo stesso
nome, c)
Array
Definizione degli Array
Esempi con Array
Array come parametri a funzioni
Sorting Arrays
Esercizi: Calcolo della Media, Mediana e Moda usando Array
Array Multidimensionali
72
c[4]
1543
c[5]
-89
Formato:
arrayname[ position number ]
c[6]
c[7]
62
c[8]
-3
c[9]
c[10]
6453
c[11]
78
Numero di posizione
dellelemento
nellarray c
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Array
Gli elementi di un array sono gestiti come normali
variabili
c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
c[2]
Nome dellarray
Posizione
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
-45
Array
Operatori
[]
()
++
-- !
(type)
*
/
%
+
<
<= >
>=
==
!=
&&
||
?:
=
+= -= *=
/= %=
,
Fig. 6.2 Operator precedence.
Associativit
left to right
right to left
left to right
left to right
left to right
left to right
left to right
left to right
right to left
right to left
left to right
Tipo
highest
unary
multiplicative
additive
relational
equality
logical and
logical or
conditional
assignment
comma
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Inizializzazione
int n[ 5 ] = { 1, 2, 3, 4, 5 };
Tipo dellarray
Nome
Numero di elementi
Esempi:
int c[ 10 ];
float myArray[ 3284 ];
int b[ 100 ], x[ 27 ];
int n[ ] = { 1, 2, 3, 4, 5 };
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
initializing an array */
Outline
#include <stdio.h>
3
4
5
int main()
int i;
/* counter */
fig06_03.c
Element
0
1
2
3
4
5
6
7
8
9
Value
0
0
0
0
0
0
0
0
0
0
10
11
12
13
14
15
16
17
18
19
20
21
printf( "%7d%13d\n", i, n[ i ] );
} /* end for */
22
23
24
25 } /* end main */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline
Program Output
#include <stdio.h>
10
Outline
Array di caratteri
La stringa first un array statico di caratteri
Gli arrays di caratteri possono essere inizializzati usando stringhe di
letterali
int main()
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
10
int i; /* counter */
fig06_04.c
11
12
13
14
15
16
17
18
19
string1[ 3 ] il carattere s
printf( "%7d%13d\n", i, n[ i ] );
} /* end for */
return 0; /* indicates successful termination */
20
21 } /* end main */
Element
0
1
2
3
4
5
6
7
8
9
Value
32
27
64
18
95
14
90
70
60
37
11
Outline
3 #include <stdio.h>
4 #define SIZE 10
Program Output
fig06_05.c
5
6 /* function main begins program execution */
7 int main()
8 {
9
10
11
int j;
/* counter */
12
13
14
15
16
17
18
19
20
21
22
printf( "%7d%13d\n", j, s[ j ] );
} /* end for */
23
24
25
26 } /* end main */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12
Outline
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Element
0
1
2
3
4
5
6
7
8
9
Value
2
4
6
8
10
12
14
16
18
20
13
Outline
Program Output
#include <stdio.h>
#define SIZE 12
14
Outline
fig06_06.c
5
6
int main()
10
11
int i;
12
/* counter */
13
14
15
16
17
} /* end for */
18
19
20
21
22
23 } /* end main */
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2
3
#include <stdio.h>
#define FREQUENCY_SIZE 11
15
Outline
21
23
24
fig06_07.c (Part 1
of 2)
22
25
26
} /* end for */
27
int main()
28
/* display results */
29
10
30
11
31
32
12
13
33
14
34
16
Outline
fig06_07.c (Part 2
of 2)
35
15
16
36
17
37
18
1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
38 } /* end main */
19
5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
Rating
1
2
3
4
5
6
7
8
9
10
20
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Frequency
2
2
2
2
5
11
5
7
1
3
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Program Output
17
Outline
2
3
#include <stdio.h>
#define SIZE 10
25
27
int main()
10
11
12
fig06_08.c (Part 1
of 2)
Element
0
1
2
3
4
5
6
7
8
9
16
17
fig06_08.c (Part 2
of 2)
29 } /* end main */
15
printf( "%7d%13d
18
Outline
28
13
18
26
14
24
Value
19
3
15
7
11
9
13
5
17
1
Histogram
*******************
***
***************
*******
***********
*********
*************
*****
*****************
*
Program Output
", i, n[ i ]) ;
19
for ( j = 1; j <= n[ i ]; j++ ) { /* print one bar */
20
21
22
23
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
19
2
3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 7
Outline
25
26
27
28
fig06_09.c (Part 1
of 2)
} /* end for */
29
30
20
Outline
fig06_09.c (Part 2
of 2)
31
32 } /* end main */
int main()
Face
1
2
3
4
5
6
10 {
11
int face;
12
int roll;
/* roll counter */
13
14
15
Frequency
1029
951
987
1033
1010
990
16
17
18
19
20
21
face = rand() % 6 + 1;
++frequency[ face ]; /* replaces 26-line switch of Fig. 5.8 */
} /* end for */
22
23
24
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Program Output
21
#include <stdio.h>
Outline
26
printf( "\n" );
22
27
28
Outline
29
int main()
char string1[ 20 ];
10
int i;
fig06_10.c (Part 1
of 2)
30 } /* end main */
Enter a
string1
string2
string1
H e l l
/* reserves 20 characters */
fig06_10.c (Part 2
of 2)
/* counter */
11
12
13
14
15
16
/* output strings */
17
18
19
string1, string2 );
20
21
22
23
} /* end for */
24
25
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
23
Outline
#include <stdio.h>
3
4
5
/* function prototype */
fig06_11.c (Part 1
of 3)
26
27
28
int i; /* counter */
fig06_11.c (Part 2
of 3)
29
7
8
30
int main()
31
32
11
33
12
staticArrayInit();
34
13
automaticArrayInit();
35
10 {
printf( "array1[ %d ] = %d
", i, array1[ i ] );
} /* end for */
36
14
15
37
16
staticArrayInit();
38
17
automaticArrayInit();
39
40
18
19
20
21 } /* end main */
22
41
42
printf( "array1[ %d ] = %d
", i, array1[ i ] += 5 );
} /* end for */
43
44 } /* end function staticArrayInit */
45
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24
Outline
25 {
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25
Outline
48 {
49
50
int array2[ 3 ] = { 1, 2, 3 };
51
int i; /* counter */
fig06_11.c (Part 3
of 3)
52
53
54
55
56
57
58
printf("array2[ %d ] = %d
26
entering staticArrayInit:
] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0
exiting staticArrayInit:
] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5
Values on
array2[ 0
Values on
array2[ 0
entering automaticArrayInit:
] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3
exiting automaticArrayInit:
] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8
Outline
Program Output
", i, array2[ i ] );
} /* end for */
Values on
array1[ 0
Values on
array1[ 0
entering staticArrayInit:
] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5
exiting staticArrayInit:
] = 10 array1[ 1 ] = 10 array1[ 2 ] = 10
Values on
array2[ 0
Values on
array2[ 0
entering automaticArrayInit:
] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3
exiting automaticArrayInit:
] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8
59
60
61
62
63
64
65
printf( "array2[ %d ] = %d
", i, array2[ i ] += 5 );
} /* end for */
66
67 } /* end function automaticArrayInit */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
27
28
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
29
Outline
#include <stdio.h>
fig06_12.c
#include <stdio.h>
#define SIZE 5
int main()
/* function prototypes */
30
Outline
fig06_13.c (Part 1
of 3)
9
10
printf( "
11
"
12
11 int main()
&array = %p\n",
12 {
13
14
13
14
int i; /* counter */
15
15
16
16 } /* end main */
array = 0012FF78
&array[0] = 0012FF78
&array = 0012FF78
17
Program Output
18
19
20
21
22
printf( "%3d", a[ i ] );
} /* end for */
23
24
printf( "\n" );
25
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
26
27
modifyArray( a, SIZE );
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
31
Outline
50
in memory */
52 {
30
fig06_13.c (Part 2
of 3)
53
int j; /* counter */
31
32
55
33
printf( "%3d", a[ i ] );
56
34
} /* end for */
35
54
57
b[ j ] *= 2;
58
} /* end for */
36
/* output value of a[ 3 ] */
59
37
38
61
62 /* in function modifyElement, "e" is a local copy of array element
39
40
63
41
42
/* output value of a[ 3 ] */
65 {
43
66
/* multiply parameter by 2 */
67
44
45
32
Outline
28
29
46
47 } /* end main */
48
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig06_13.c (Part 3
of 3)
33
Outline
34
Outline
#include <stdio.h>
3
4
Program Output
5
6
int main()
10
fig06_14.c (Part 1
of 2)
11
12
tryToModifyArray( a );
13
14
printf("%d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ] );
15
16
17
18 } /* end main */
19
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
35
Outline
23 {
24
b[ 0 ] /= 2;
/* error */
25
b[ 1 ] /= 2;
/* error */
26
b[ 2 ] /= 2;
/* error */
fig06_14.c (Part 2
of 2)
Compiling...
FIG06_14.C
fig06_14.c(24) : error C2166: l-value specifies const object
fig06_14.c(25) : error C2166: l-value specifies const object
fig06_14.c(26) : error C2166: l-value specifies const object
Program Output
36
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
37
3
4
#include <stdio.h>
#define SIZE 99
Outline
fig06_16.c (Part 1
of 8)
19
20
21
{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
22
7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
23
6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
24
7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
/* function prototypes */
25
6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
26
7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
27
5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
28
7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
29
7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
30
4, 5, 6, 1, 6, 5, 7, 8, 7 };
13
31
32
/* process responses */
15 int main()
33
mean( response );
16 {
34
median( response );
35
17
18
38
Outline
fig06_16.c (Part 2
of 8)
36
37
38
39 } /* end main */
40
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
int j;
45
/* counter */
65
46
printf( "%s\n%s\n%s\n", "********", "
Mean", "********" );
48
fig06_16.c (Part 3
of 8)
67
68
69
70
50
71
52
total += answer[
];
72
} /* end for */
53
54
printf( "\n%s\n%s\n%s\n%s",
66
49
51
40
Outline
64 {
44
47
39
Outline
73
74
75
55
76
56
77
57
78
58
79
59
80
double
) total / SIZE );
61
82
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig06_16.c (Part 4
of 8)
41
Outline
85 {
86
int rating;
/* counter */
87
int j;
/* counter */
88
int h;
/* counter */
89
int largest = 0;
90
fig06_16.c (Part 5
of 8)
106
printf( "%s%11s%19s\n\n%54s\n%54s\n\n",
107
108
"1
printf( "\n%s\n%s\n%s\n",
"********", "
Mode", "********" );
5" );
fig06_16.c (Part 6
of 8)
printf( "%8d%11d
114
115
116
96
118
119
freq[ rating ] = 0;
} /* end for */
99
103
117
102
111
/* initialize frequencies to 0 */
101
2", "5
/* output results */
95
100
110
112
94
98
113
93
97
42
Outline
109
91
92
105
/* summarize frequencies */
for ( j = 0; j < SIZE; j++ ) {
120
121
122
printf( "*" );
123
124
++freq[ answer[ j ] ];
125
} /* end for */
126
104
127
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
128
129
130
131
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
43
Outline
154
156
157
fig06_16.c (Part 7
of 8)
136 {
162 {
137
138
int j;
139
/* counter */
140
141
142
163
144
145
int j; /* counter */
164
165
166
167
168
143
169
170
171
146
147
172
148
if ( a[ j ] > a[ j + 1 ] ) {
173
printf( "%2d", a[ j ] );
} /* end for */
149
hold = a[ j ];
174
150
a[ j ] = a[ j + 1 ];
151
a[ j + 1 ] = hold;
152
} /* end if */
153
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
44
Outline
155
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig06_16.c (Part 8
of 8)
45
********
Mean
********
The mean is the average value of the data
items. The mean is equal to the total of
all the data items divided by the number
of data items ( 99 ). The mean value for
this run is: 681 / 99 = 6.8788
********
Median
********
The unsorted
6 7 8 9 8 7
6 7 8 9 3 9
6 7 8 7 8 7
5 6 7 2 5 3
7 4 4 2 5 3
The sorted
1 2 2 2 3
5 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
Outline
46
Outline
Program Output
********
Mode
********
Response
Program Output
(continued)
Frequency
Histogram
5
array
8 9 8
8 7 8
9 8 9
9 4 6
8 7 5
array
3 3 3
6 6 6
7 7 7
8 8 8
9 9 9
of responses is
9 7 8 9 5 9 8 7
7 7 8 9 8 9 8 9
2 7 8 9 8 9 8 9
4 7 8 9 6 8 7 8
6 4 5 6 1 6 5 7
is
4 4
6 6
7 7
8 8
9 9
4
7
7
8
9
4
7
7
8
9
4
7
7
8
9
5
7
8
8
9
5
7
8
8
9
5
7
8
8
9
5
7
8
8
9
8
7
7
9
8
7
8
5
7
7
8
9
3
8
5
7
8
8
9
5
7
8
8
9
5
7
8
8
1
0
1
5
2
0
2
5
1
1
*
2
3
***
3
4
****
4
5
*****
5
8
********
6
9
*********
7
23
***********************
8
27
***************************
9
19
*******************
The mode is the most frequent value.
For this run the mode is 8 which occurred 27 times.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
47
Array Multidimensionali
Array Multidimensionali
Array multidimensionali
Inizializzazione
Column 1
48
Column 2
Column 3
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 ]
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
Indice di colonna
printf( "%d", b[ 0 ][ 1 ] );
Nome array
Indice di riga
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
#include <stdio.h>
49
fig06_21.c (Part 1
of 2)
31
int i; /* counter */
32
int j; /* counter */
33
34
int main()
35
fig06_21.c (Part 2
of 2)
36
10
11
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
12
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
13
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
15
16
printArray( array1 );
37
38
39
40
14
41
42
43
17
18
44
19
printArray( array2 );
21
22
printArray( array3 );
Program Output
20
23
return 0; /* indicates successful termination */
24
50
Outline
30 {
4
5
Outline
25
26 } /* end main */
27
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2
3
#include <stdio.h>
#define STUDENTS 3
#define EXAMS 4
51
Outline
24
25
26
27
fig06_22.c (Part 1
of 6)
28
29
/* function prototypes */
30
int minimum( const int grades[][ EXAMS ], int pupils, int tests );
31
int maximum( const int grades[][ EXAMS ], int pupils, int tests );
32
34
36
37
38
15 {
16
33
35
12
17
18
19
20
21
22
23
39
40
41 } /* end main */
42
43 /* Find the minimum grade */
44 int minimum( const int grades[][ EXAMS ], int pupils, int tests )
45 {
46
int i;
/* counter */
47
int j;
/* counter */
48
49
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
52
Outline
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig06_22.c (Part 2
of 6)
50
51
53
Outline
52
/* loop through columns of grades */
54
fig06_22.c (Part 3
of 6)
55
} /* end if */
83
54
Outline
fig06_22.c (Part 4
of 6)
84
85
86
87
63
64
79
82
61
62
78
81
lowGrade = grades[ i ][ j ];
59
60
80
57
58
76
77
53
56
75
88
89
65
90
67
92
69 int maximum( const int grades[][ EXAMS ], int pupils, int tests )
70 {
95 {
71
int i;
/* counter */
96
int i;
72
int j;
/* counter */
97
/* counter */
73
98
74
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
99
100
101
total += setOfGrades[ i ];
102
55
Outline
123
124
125
} /* end for */
126
103
104
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig06_22.c (Part 5
of 6)
fig06_22.c (Part 6
of 6)
127
128
105
129
107
int i; /* counter */
112
int j; /* counter */
113
114
115
printf( "
[0]
[1]
[2]
[3]" );
116
117
118
[0]
studentGrades[0] 77
studentGrades[1] 96
studentGrades[2] 70
[1]
68
87
90
[2]
86
89
86
[3]
73
78
81
Lowest grade: 68
Highest grade: 96
The average grade for student 0 is 76.00
The average grade for student 1 is 87.50
The average grade for student 2 is 81.75
119
120
121
122
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
56
Outline
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.