我的输出是285个数字,但它全部在第1列(按升序排列)。它在某种程度上应该分为前5列的50个数字,然后是最后一个的35个。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE_FLOAT n
#define MAX_NUM_OF_FLOATS 1000
void swap(float * a, float * b);`enter code here`
void sort(float ary [], int numElements );
int main(int argc, char** argv) {
int i,num_read;
float x, n;
float a[MAX_NUM_OF_FLOATS];`enter code here`
double x1,x2,mn;
FILE * inFile;
inFile = fopen("DataFloat.txt", "r");
if( inFile == NULL) {
printf("\nError opening input file; program aborted\n");
exit(1);
}
i = 0;
while( fscanf(inFile, "%f", & a[i]) == 1 ){
x1+=a[i];
x2+=a[i]*a[i];
i++;
mn=x1/(double)n;
}
fclose(inFile);
if(n<1){
printf("\nError.");
exit(1);
}
num_read = i;
for(i = 0; i < num_read; i++)
sort(a, num_read);
printf("Column1 Column2 Column3 Column4 Column5 Column6\n",a[i]);
for(i = 0; i < num_read; i++) <--Am i supposed to be using a for loop here or while-loop?
printf("%5.2f\n", a[i] );
return (EXIT_SUCCESS);
} // end of main
void sort(float ary [], int numElements ) {
int i, currentIndex, foundIndex, nextIndex;
float min;
// the outer for-loop is the placement loop
for( currentIndex = 0; currentIndex < numElements - 1; currentIndex++){
min = ary[currentIndex];
foundIndex = currentIndex;
nextIndex = currentIndex + 1;
// the inner for-loop selects the best element from the rest of ary
for( i = nextIndex; i < numElements; i++)
if( ary[i] < min ){
min = ary[i];
foundIndex = i;
}
swap(& ary[currentIndex], & ary[foundIndex]);
}
}
void swap(float * a, float * b){
float temp;
temp = *a;
*a = *b;
*b =temp;
}
我的输出是285个数字,但它全部在第1列(按升序排列)。它在某种程度上应该分为前5列的50个数字,然后是最后一个的35个。
答案 0 :(得分:1)
那么,我想,你只需要用六列来格式化输出,比如
for (i = 0; i < 50; ++i){
printf("%d\t%d\t%d\t%d\t%d",
arr[i], arr[i + 50], arr[i + 100], arr[i + 150], arr[i + 200]);
if(i < 35)
printf("\t%d", arr[i + 250]);
printf("\n");
}
编辑:根据Jonathan Leffler的评论更新。
答案 1 :(得分:0)
这对我有用(根据我对问题的理解)。我已经参数化了代码,因此值的数量,列数和完整行数被命名为常量。有一个断言确保Column6包含一些数据。代码不会动态生成列标题,但要做到这一点并不困难。
#include <assert.h>
#include <stdio.h>
enum { NUM_VALUES = 285 };
enum { NUM_COLUMNS = 6 };
enum { NUM_ROWS = 50 };
int main(void)
{
double data[NUM_VALUES];
int i;
assert(NUM_VALUES > (NUM_COLUMNS - 1) * NUM_ROWS &&
NUM_VALUES <= (NUM_COLUMNS) * NUM_ROWS);
for (i = 0; i < NUM_VALUES; i++)
data[i] = i * 10.0 + i / 100.0;
printf("Column1 Column2 Column3 Column4 Column5 Column6\n");
for (i = 0; i < NUM_ROWS; i++)
{
char *pad = "";
for (int j = 0; j < NUM_COLUMNS; j++)
{
int index = j * NUM_ROWS + i;
if (index < NUM_VALUES)
printf("%s%7.2f", pad, data[index]);
pad = " ";
}
putchar('\n');
}
return 0;
}
示例输出:
Column1 Column2 Column3 Column4 Column5 Column6
0.00 500.50 1001.00 1501.50 2002.00 2502.50
10.01 510.51 1011.01 1511.51 2012.01 2512.51
20.02 520.52 1021.02 1521.52 2022.02 2522.52
30.03 530.53 1031.03 1531.53 2032.03 2532.53
40.04 540.54 1041.04 1541.54 2042.04 2542.54
50.05 550.55 1051.05 1551.55 2052.05 2552.55
60.06 560.56 1061.06 1561.56 2062.06 2562.56
70.07 570.57 1071.07 1571.57 2072.07 2572.57
80.08 580.58 1081.08 1581.58 2082.08 2582.58
90.09 590.59 1091.09 1591.59 2092.09 2592.59
100.10 600.60 1101.10 1601.60 2102.10 2602.60
110.11 610.61 1111.11 1611.61 2112.11 2612.61
120.12 620.62 1121.12 1621.62 2122.12 2622.62
130.13 630.63 1131.13 1631.63 2132.13 2632.63
140.14 640.64 1141.14 1641.64 2142.14 2642.64
150.15 650.65 1151.15 1651.65 2152.15 2652.65
160.16 660.66 1161.16 1661.66 2162.16 2662.66
170.17 670.67 1171.17 1671.67 2172.17 2672.67
180.18 680.68 1181.18 1681.68 2182.18 2682.68
190.19 690.69 1191.19 1691.69 2192.19 2692.69
200.20 700.70 1201.20 1701.70 2202.20 2702.70
210.21 710.71 1211.21 1711.71 2212.21 2712.71
220.22 720.72 1221.22 1721.72 2222.22 2722.72
230.23 730.73 1231.23 1731.73 2232.23 2732.73
240.24 740.74 1241.24 1741.74 2242.24 2742.74
250.25 750.75 1251.25 1751.75 2252.25 2752.75
260.26 760.76 1261.26 1761.76 2262.26 2762.76
270.27 770.77 1271.27 1771.77 2272.27 2772.77
280.28 780.78 1281.28 1781.78 2282.28 2782.78
290.29 790.79 1291.29 1791.79 2292.29 2792.79
300.30 800.80 1301.30 1801.80 2302.30 2802.80
310.31 810.81 1311.31 1811.81 2312.31 2812.81
320.32 820.82 1321.32 1821.82 2322.32 2822.82
330.33 830.83 1331.33 1831.83 2332.33 2832.83
340.34 840.84 1341.34 1841.84 2342.34 2842.84
350.35 850.85 1351.35 1851.85 2352.35
360.36 860.86 1361.36 1861.86 2362.36
370.37 870.87 1371.37 1871.87 2372.37
380.38 880.88 1381.38 1881.88 2382.38
390.39 890.89 1391.39 1891.89 2392.39
400.40 900.90 1401.40 1901.90 2402.40
410.41 910.91 1411.41 1911.91 2412.41
420.42 920.92 1421.42 1921.92 2422.42
430.43 930.93 1431.43 1931.93 2432.43
440.44 940.94 1441.44 1941.94 2442.44
450.45 950.95 1451.45 1951.95 2452.45
460.46 960.96 1461.46 1961.96 2462.46
470.47 970.97 1471.47 1971.97 2472.47
480.48 980.98 1481.48 1981.98 2482.48
490.49 990.99 1491.49 1991.99 2492.49