I've been looking for a few days all over the internet and I just can't get clarity on this at all:
The idea:
I am given a set of data with a column on the leftmost part of my data as a designator. This column distinguishes what set of data goes with what, but the format is awkward to handle, programming-wise:
0006 3129675 391894 14.31 2589014 94.57 148767 5.43 2635952 96.28 2589016 94.57
00061 3 0 0.00 3 100.00 0 0.00 3 100.00 3 100.00
00062 1 0 0.00 1 100.00 0 0.00 1 100.00 1 100.00
00063 1 0 0.00 0 0.00 1 100.00 0 0.00 0 0.00
0007 2230275 296987 15.36 1786722 92.42 146566 7.58 1842030 95.28 1786722 92.42
1100 2234323 11137 0.50 2085441 93.80 137745 6.20 2127517 95.70 2085441 93.80
1101 118662 214 0.18 115408 97.43 3040 2.57 115973 97.91 115408 97.43
11011 2212 2 0.09 2157 97.60 53 2.40 2167 98.05 2157 97.60
1102 62665 143 0.23 60929 97.45 1593 2.55 61210 97.90 60929 97.45
11021 895 4 0.45 862 96.75 29 3.25 864 96.97 862 96.75
1103 19478 37 0.19 18980 97.63 461 2.37 19073 98.11 18980 97.63
11031 265 1 0.38 258 97.73 6 2.27 259 98.11 258 97.73
1104 73202 108 0.15 71129 97.31 1965 2.69 71488 97.80 71129 97.31
11041 1396 3 0.22 1340 96.20 53 3.80 1355 97.27 1340 96.20
(Sorry for the formatting) So, the "0006" has a subgroup of 3 more lines of data: 00061, 00062, 00063. The rest of the data to the right of this column I process and know what to do. I just have this stumbling block on how to make this column into what I want.
The question:
Initially, I scanf in all of that in their respective columns (12 columns total) having the first column, in particular, as int. What I want to do with this column is chop off any 5th digit, which - from all of the reading I've done over the last few days - needs to be in character format. Well, I'm not sure how to convert an int array into a character array, truncate, and then convert back to int. How do I do this or is there a far more efficient way to go about this?
What I want to see:
0006 3129675 391894 14.31 2589014 94.57 148767 5.43 2635952 96.28 2589016 94.57
0006 3 0 0.00 3 100.00 0 0.00 3 100.00 3 100.00
0006 1 0 0.00 1 100.00 0 0.00 1 100.00 1 100.00
0006 1 0 0.00 0 0.00 1 100.00 0 0.00 0 0.00
0007 2230275 296987 15.36 1786722 92.42 146566 7.58 1842030 95.28 1786722 92.42
1100 2234323 11137 0.50 2085441 93.80 137745 6.20 2127517 95.70 2085441 93.80
1101 118662 214 0.18 115408 97.43 3040 2.57 115973 97.91 115408 97.43
1101 2212 2 0.09 2157 97.60 53 2.40 2167 98.05 2157 97.60
1102 62665 143 0.23 60929 97.45 1593 2.55 61210 97.90 60929 97.45
1102 895 4 0.45 862 96.75 29 3.25 864 96.97 862 96.75
1103 19478 37 0.19 18980 97.63 461 2.37 19073 98.11 18980 97.63
1103 265 1 0.38 258 97.73 6 2.27 259 98.11 258 97.73
1104 73202 108 0.15 71129 97.31 1965 2.69 71488 97.80 71129 97.31
1104 1396 3 0.22 1340 96.20 53 3.80 1355 97.27 1340 96.20
That way, in my loops, all I have to do is check that first column to see if the previous entry is equal to the current. If so, process. If not, keep as is.
Desired end result:
0006 SUM INDIVIDUAL COLUMNS ...
0007 KEEP AS IS
1100 KEEP AS IS
1101 SUM INDIVIDUAL COLUMNS ...
1102 SUM INDIVIDUAL COLUMNS ...
....
I've never done something like this and really don't know how to code it. So, this is all I have as a starting point:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
//Processing values into arrays:
FILE *fp;
fp = fopen("test.txt", "r");
//Numbers to be summed:
l = (int*) malloc(N*sizeof(double));
ti = (int*) malloc(N*sizeof(double));
ef = (int*) malloc(N*sizeof(double));
z4c = (int*) malloc(N*sizeof(double));
z4b = (int*) malloc(N*sizeof(double));
cc = (int*) malloc(N*sizeof(double));
wc = (int*) malloc(N*sizeof(double));
//Percentages to be recalculated:
p1 = (double *) malloc(N*sizeof(double));
p2 = (double *) malloc(N*sizeof(double));
p3 = (double *) malloc(N*sizeof(double));
p4 = (double *) malloc(N*sizeof(double));
p5 = (double *) malloc(N*sizeof(double));
//Creating arrays:
for( i = 0; i < N + 1; i++)
{
fscanf(fp, "%d %d %d %lf %d %lf %d %lf %d %lf %d %lf",
&list, &total_input, &edit_fail, &perc1, &zip_4_coded, &perc2,
&zip_4_blank, &perc3, &crc_coded, &perc4, &ws_coded, &perc5); //Reads the file that I showed you a sample of and takes in only what I currently need.
l[i] = list;
ti[i] = total_input;
ef[i] = edit_fail;
z4c[i] = zip_4_coded;
z4b[i] = zip_4_blank;
cc[i] = crc_coded;
wc[i] = ws_coded;
/*printf("%d %d %d %d %d %d %d\n", l[i], ti[i],
ef[i], z4c[i], z4b[i], cc[i], wc[i]);*/ //Checks to see if I'm getting what I'm suppose to have.
}
//Changing any list array values from 5-digit to 4:
?????????????...
|
Thanks...