存储一组数据到一个二维数组中,该如何解决
存储一组数据到一个二维数组中
有这样一组数据:
1.21989
1.21996
1.21972
1.21995
1.22001
1.21971
1.21996
1.21991
1.22
1.21969
1.21997
1.21991
1.21988
1.21996
1.21982
1.21981
1.21992
1.21997
1.21986
1.21999
0.611675
0.611824
0.611855
0.611777
0.611914
0.611867
0.611741
0.611862
0.611821
0.611761
0.611831
0.611898
0.611762
0.611739
0.611961
0.611646
0.611869
0.611757
0.611838
0.611877
0.611871
0.611776
0.611841
0.611737
0.611907
0.611754
0.61188
0.611756
0.611871
0.611798
0.611722
0.611902
0.611766
0.61189
0.611852
0.611828
0.611874
0.611657
0.611947
0.611783
0.611949
0.611658
0.61188
0.611834
0.611858
0.611897
0.611771
0.611911
0.611732
0.61181
0.611882
0.611768
0.611893
0.611767
0.611921
0.611752
0.611857
0.611792
0.611887
0.611893
0.611852
0.611771
0.611883
0.611663
0.611976
0.611752
0.611774
0.611909
0.611843
0.611771
0.611833
0.611869
0.611748
0.611876
0.611918
0.61178
0.611856
0.611825
0.61187
这组数据是这样的。
现在,我们需要把数据这样存储在一个二维数组中,存储的状态是这样的: 第一列是序号,第二列是数据的累加
array [0] [1]
[0] 1 1.21989
[1] 2 1.21989+1.21996(也就是前两个数据之和)
[2] 3 21989+1.21996+1.21972(前三个数据之和)
. . .
. . .
. . .
以此类推
大概就是需要实现这样一个功能。
------解决方案--------------------
有这样一组数据:
1.21989
1.21996
1.21972
1.21995
1.22001
1.21971
1.21996
1.21991
1.22
1.21969
1.21997
1.21991
1.21988
1.21996
1.21982
1.21981
1.21992
1.21997
1.21986
1.21999
0.611675
0.611824
0.611855
0.611777
0.611914
0.611867
0.611741
0.611862
0.611821
0.611761
0.611831
0.611898
0.611762
0.611739
0.611961
0.611646
0.611869
0.611757
0.611838
0.611877
0.611871
0.611776
0.611841
0.611737
0.611907
0.611754
0.61188
0.611756
0.611871
0.611798
0.611722
0.611902
0.611766
0.61189
0.611852
0.611828
0.611874
0.611657
0.611947
0.611783
0.611949
0.611658
0.61188
0.611834
0.611858
0.611897
0.611771
0.611911
0.611732
0.61181
0.611882
0.611768
0.611893
0.611767
0.611921
0.611752
0.611857
0.611792
0.611887
0.611893
0.611852
0.611771
0.611883
0.611663
0.611976
0.611752
0.611774
0.611909
0.611843
0.611771
0.611833
0.611869
0.611748
0.611876
0.611918
0.61178
0.611856
0.611825
0.61187
这组数据是这样的。
现在,我们需要把数据这样存储在一个二维数组中,存储的状态是这样的: 第一列是序号,第二列是数据的累加
array [0] [1]
[0] 1 1.21989
[1] 2 1.21989+1.21996(也就是前两个数据之和)
[2] 3 21989+1.21996+1.21972(前三个数据之和)
. . .
. . .
. . .
以此类推
大概就是需要实现这样一个功能。
------解决方案--------------------
- C/C++ code
//源数据在3.26.txt里面; //处理后的数据在3.26__.txt里面; #include <fstream> #include <string> #include <iostream> #include <assert.h> using namespace std; int main() { const int Row=100; const int Coll=0; double dDest[Row]; ifstream inFile("3-26.txt"); ofstream outFile("3-26__.txt"); assert(inFile!=NULL); assert(outFile!=NULL); string line; int i=0, count=0; double sum=0.0; while ( !inFile.eof() ) { getline(inFile, line, '\n'); sum+=atof( line.c_str() ); dDest[count++]=sum; } inFile.close(); for (int i=0; i<Row; i++) { outFile<<dDest[i]; if( (i+1)%10==0 ) outFile<<endl; else outFile<<" "; } outFile.close(); return 0; }