当前位置: 首页 > 文档资料 > C++大学教程 >

4.7 实例研究:用数组计算平均值、中数和模

优质
小牛编辑
133浏览
2023-12-01

下面要举一个更大的例子。计算机常用于编译和分析调查结果,图4.17的程序用数组 response 初始化调查的99个答复(用常量变量 response 表示),每个答复是1到9的数值。程序计算99个值的平均值、中数和模。

1 // Fig. 4.17: fig04_lT.cpp
2 // This program introduces the topic of survey data analysis.
3 // It computes the mean, median, and mode of the data.
4 #include <iostream.~
5 #include <iomanip.h>
6
7 void mean( const int[],int );
8 void median( int []
9 voidmode(int[],int[],int);
l0 void bubbleSort( int[ ] , int );
11 voidprintArray(constint[],int);
12
13 int main()
14 {
15 const int responseSize = 99;
16 intfrequency[10] ={ 0 },
17 response[ responseSize ] =
i8 {6,7,8,9,8,7,8,9,8,9,
19 7,8,9,5,9,8,7,8,7,8,
20 6,7,8,9,3,9,8,7,8,7,
21 7,8,9,8,9,8,9,7,8,9,
22 6,7,8,7,8,7,9,8,9,2,
23 7,8,9,8,9,8,9,7,5,3,
24 5,6,7,2,5,3,9,4,6,4,
25 7,8,9,6,8,7,8,9,7,8,
26 7,4,4,2,5,3,8,7,5,6,
27 4,5,6,1,6,5,7,8,7};
28
29 mean( response, responseSize );
30 median( response, responseSize );
31 mode( frequency, response, responseSize );
32
33 return O;
34}
35
36 void mean( const int answer[], int arraySize )
37 {
38 int total = 0;
39
4O cout << "********\n Mean\n********\n";
41
42 for ( int j = 0; j < arraySize; j++ )
43 total += answer[ j ];
44
45 cout << "The mean is the average value of the data\n"
46 << "items. The mean is equal to the total of\n"
47 << "all the data items divided by the number\n"
48 << "of data items (" << arraySize
49 << "}. The mean value for\nthis run is:"
50 << total <<" / "<< arraySize <<" ="
51 << setiosflags{ ios::fixed ] ios::showpoint }
52 << setprecision( 4 ) << ( float } total / arraySize
53 << "\n\n";
54 }
55
56 void median( int answer[], int size }
57 {
58 cout << "\n********\n Median\n********\n"
59 << "The unsorted array of responses is";
6O
61 printArray( answer, size );
62 bubbleSort( answer, size );
63 cout << "\n\nThe sorted array is";
64 printArray( answer, size );
65 cout << "\n\nThe median is element" << size / 2
66 <<" of\nthe sorted" << size
67 << "element array.\nFor this run the median is"
68 << answer[ size / 2 ] << "\n\n";
69 }
70
71 void mode( int freq[ ], int answer[], int size )
72 {
73 int rating, largest = 0, modeValue = 0;
74
75 cout << "\n********\n Mode\n********\n";
76
77 for ( rating = 1; rating <= 9; rating++ )
78 freq[ rating ] = 0;
79
80 for ( int j = 0; j < size; j++ }
81 ++freq[ answe[ j ] ];
82
83 cout << "Response"<< setw( 11 ) << "Frequency"
84 << setw( 19 ) << "Histogram\n\n" << setw( 55 )
85 << "1 1 2 2\n" << setw( 56 )
86 << "5 0 5 0 5\n\n";
87
88 for ( rating = 1; rating <= 9; rating++ ) {
89 cout << setw( 8 ) << rating << setw(11)
90 << freq[ rating ] << " ";
91
92 if ( freq[ rating ] > largest ) {
93 largest = freq[ rating ];
94 modeValue = rating;
95 }
96
97 for (int h = 1; h <= freq[ rating ]; h++ )
98 cout << '*';
99
100 cout << '\n';
101 }
102
103 cout << "The mode is the most frequent value.\n"
104 << "For this run the mode is "<< modeValue
105 << "which occurred" << largest <<" times." << endl;
106 }
107
108 void bubbleSort( int a[], int size )
109 {
110 int hold;
111
112 for (int pass = 1; pass < size; pass++ )
113
114 for ( int j - 0; j < size - 1; j++ )
115
116 if ( a[ j ] > a[ j + 1 ] ) {
117 hold = a[ j ];
118 a[ j ] =a[ j + 1 ];
119 a[ j + 1 ] = hold;
120 }
121 }
122
123 void printArray( const int a[ ], int size )
124 {
125 for ( int j = 0; j < size; j++ )
126
127 if ( j % 20 == 0 )
128 cout << endl;
129
130 cout << setw( 2 ) << a[ j ]
131 }
132 }

图4.17调查数据分析程序

平均值是99个数的算术平均值。函数mean计算99个元素的和除以99所得的平均值。

中数是“中间值”,函数median调用bubbleSort函数排序(按升序)答复数组,并选出数组的中间元素answer[responseSize/2]。注意,如果元素个数为偶数,则中数为中间两个元素的平均值,函数median目前没有提供这个功能。调用函数printArray输出response数组。

模是99个答复中最经常出现的值。函数mode计算每种答复的个数,然后选择个数最多的值。

这个版本的函数mode不处理连接(见练习4.14)。函数mode还产生直方图,帮助用图形确定模。图4.18包含这个程序的示例输出。这个例子包括数组问题中通常需要的最常见操作,包括将数组传递给函数。

* * * * * * * *
Mena
* * * * * * * *
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

* * * * * * * *
Mena
* * * * * * * *
The unsorted array of responses is
6 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 8 6 4 5 6 1 6 5 7 8 7

The sorted array is
1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5
5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 3 7 7 7 7 7 7 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
The median is element 49 of
The sorted 99 element array.
For this run the median is 7

* * * * * * * *
Mode
* * * * * * * *
Response Frequency Histogram
1 1 2 2
5 0 5 0 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 is 8 which occurred 27 times.

图 4.18 调查数据分析程序的运行结果