source: proiecte/Parallel-DT/R8/Src/average.c @ 24

Last change on this file since 24 was 24, checked in by (none), 14 years ago

blabla

File size: 1.6 KB
Line 
1/*************************************************************************/
2/*                                                                       */
3/*      Average results for training and test sets                       */
4/*      ------------------------------------------                       */
5/*                                                                       */
6/*      This is a generic program that averages any numbers found on     */
7/*      a set of lines of the same pattern.                              */
8/*                                                                       */
9/*************************************************************************/
10
11
12#include <stdio.h>
13
14#define MAXLINE         200     /* max line length */
15#define MAXVALS          10     /* max values to be averaged */
16
17
18main()
19{
20    char Line[MAXLINE], *p1, *p2;
21    int Numbers=0, Lines=0, i, TrainTest;
22    float Val, Sum[2][MAXVALS];
23    double strtod();
24
25    for ( i = 0 ; i < MAXVALS ; i++ )
26    {
27        Sum[0][i] = Sum[1][i] = 0;
28    }
29
30    while ( fgets(Line, MAXLINE, stdin) )
31    {
32        i = 0;
33        TrainTest = Lines % 2;
34        printf("%s", Line);
35
36        /*  Count the numbers appearing on the line  */
37
38        for ( p1 = Line ; *p1 != '\n' ; p1++ )
39        {
40            if ( *p1 < '0' || *p1 > '9' ) continue;
41
42            Val = strtod(p1, &p2);
43            Sum[TrainTest][i++] += Val;
44            p1 = p2-1;
45        }
46
47        /*  The number of numbers must match any previous lines  */
48
49        if ( Lines )
50        {
51            if ( i != Numbers ) exit();
52        }
53        else
54        {
55            Numbers = i;
56        }
57
58        Lines++;
59    }
60
61    putchar('\n');
62    for ( TrainTest = 0 ; TrainTest <= 1 ; TrainTest++ )
63    {
64        i = 0;
65        printf("%s:\t", TrainTest ? "test" : "train");
66
67        for ( p1 = Line ; *p1 != '\n' ; p1++ )
68        {
69            if ( *p1 < '0' || *p1 > '9' )
70            {
71                putchar(*p1);
72            }
73            else
74            {
75                printf("%.1f", Sum[TrainTest][i++] / (0.5 * Lines));
76                strtod(p1, &p2);
77                p1 = p2-1;
78            }
79        }
80        putchar('\n');
81    }
82}
Note: See TracBrowser for help on using the repository browser.