Synapseindia - PHP Development Arrays Fuction Part-2
Synapseindia - PHP Development Arrays Fuction Part-2
Synapseindia - PHP Development Arrays Fuction Part-2
Mean average
Median number in middle of sorted list
1, 2, 3, 4, 5
3 is the median
1, 1, 1, 2, 3, 3, 4, 5
1 is the mode
1. Function
prototypes
1.1 Initialize
array
2. Call
functions
mean,
median,
2
and mode
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Mean", "********" );
3. Define
function
mean
3.1 Define
function
median
3.1.1 Sort
Array
3
65 }
66
67 void mode( int freq[], const int answer[] )
68 {
69
int rating, j, h, largest = 0, modeValue = 0;
70
71
72
73
74
75
76
printf( "\n%s\n%s\n%s\n",
"********", " Mode", "********" );
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
3.2.1 Increase
frequency[]
depending on
response[]
printf( "%s%11s%19s\n\n%54s\n%54s\n\n",
"Response", "Frequency", "Histogram",
"1
1
2
2", "5
0
5
0
5" );
95
printf( "\n" );
96
}
97
98
printf( "The mode is the most frequent value.\n"
99
"For this run the mode is %d which occurred"
100
" %d times.\n", modeValue, largest );
101 }
102
103 void bubbleSort( int a[] )
104 {
105
int pass, j, hold;
106
107
for ( pass = 1; pass <= SIZE - 1; pass++ )
108
109
for ( j = 0; j <= SIZE - 2; j++ )
110
111
if ( a[ j ] > a[ j + 1 ] ) {
112
hold = a[ j ];
Bubble sort: if elements
113
a[ j ] = a[ j + 1 ];
order, swap them.
114
a[ j + 1 ] = hold;
115
}
116 }
117
118 void printArray( const int a[] )
119 {
120
int j;
121
122
for ( j = 0; j <= SIZE - 1; j++ ) {
123
124
if ( j % 20 == 0 )
125
printf( "\n" );
3.3 Define
bubbleSor
t
3.3 Define
printArra
out of
y
126
127
128
printf( "%2d", a[ j ] );
}
129 }
********
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
Program
Output
********
Median
********
The unsorted array of responses is
7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7
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
array
3 3 3
6 6 6
7 7 7
8 8 8
9 9 9
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
5
7
8
8
9
5
7
8
8
9
5
7
8
8
********
Mode
********
Response
Frequency
Histogram
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.
Program
Output
Simple
Compare each element of array with key value
Useful for small and unsorted arrays
Binary search
Row 0
Row 1
Row 2
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 ]
Column subscript
Array
name Row subscript
10
Initialization
1
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } 3};
Referencing elements
11
1. Initialize
variables
1.1 Define
functions to
take double
scripted
arrays
1.2 Initialize
12
studentgr
ades[][]
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
3. Define
functions
return lowGrade;
}
/* Find the maximum grade */
int maximum( const int grades[][ EXAMS ],
int pupils, int tests )
{
int i, j, highGrade = 0;
for ( i = 0; i <= pupils - 1; i++ )
for ( j = 0; j <= tests - 1; j++ )
if ( grades[ i ][ j ] > highGrade )
highGrade = grades[ i ][ j ];
return highGrade;
}
/* Determine the average grade for a particular exam */
double average( const int setOfGrades[], int tests )
{
13
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
int i, total = 0;
for ( i = 0; i <= tests - 1; i++ )
total += setOfGrades[ i ];
3. Define
functions
[0]
[1]
[2]
[3]" );
14
[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
Program
Output
15