File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ #define _CRT_SECURE_NO_WARNINGS
2
+ #include < iostream>
3
+ #include < cstdio>
4
+ #include < string.h>
5
+ using namespace std ;
6
+ int main () {
7
+ int n, cnt = 0 ;
8
+ char a[50 ], b[50 ];
9
+ double temp, sum = 0.0 ;
10
+ cin >> n;
11
+ for (int i = 0 ; i < n; i++) {
12
+ // memset(a, 0, 50 * sizeof(char));
13
+ scanf (" %s" , a);
14
+ sscanf (a, " %lf" , &temp);
15
+ sprintf (b, " %.2f" , temp);
16
+ int flag = 0 ;
17
+ for (int j = 0 ; j < strlen (a); j++) {
18
+ if (a[j] != b[j]) {
19
+ flag = 1 ;
20
+ break ;
21
+ }
22
+ }
23
+ if (flag || temp < -1000 || temp > 1000 ) {
24
+ printf (" ERROR: %s is not a legal number\n " , a);
25
+ continue ;
26
+ }
27
+ else {
28
+ sum += temp;
29
+ cnt++;
30
+ }
31
+ }
32
+ if (cnt == 0 ) {
33
+ printf (" The average of 0 numbers is Undefined" );
34
+ }
35
+ else if (cnt == 1 ) {
36
+ printf (" The average of 1 number is %.2f" , sum);
37
+ }
38
+ else {
39
+ printf (" The average of %d numbers is %.2f" , cnt, sum / cnt);
40
+ }
41
+ return 0 ;
42
+ }
You can’t perform that action at this time.
0 commit comments