存储一组数据到一个二维数组中

xiaokarui1983 2008-03-24 03:41:20
有这样一组数据:
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(前三个数据之和)
. . .
. . .
. . .
以此类推
大概就是需要实现这样一个功能。
...全文
239 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaokarui1983 2008-03-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dizuo 的回复:]
C/C++ code二维数组,lz你的第二维有几个元素了?
你没有说清楚啊,
lz是不是搞三维的?呵呵
可以:
float sum=0;
ifstream inFile("input.txt");
assert(inFile!=NULL);
string line;
int i=0;
while( !inFile.eof() )
{
getline(inFile, line, '\n');
sum+=atof(line.c_str());
array[i++]=sum;
}
atof将字符串转化为float函数,累加就可以的;
[/Quote]

恩,其实就是一个n行2列的数组,第1列放的是索引号,比如1,2,3,等等;第二列放的就是这些数据的累加存储。但是我看你的程序,好像不是我药的那种结果,我需要的其实就是把这些数据累加存储就好了。比如第一行第二列放第1个数,第二行第二列放第1个数和第2个数之和,第三行第2列放第1个数,第2个数,第3个数之和,第三行第2列放第1个数,第2个数,第3个数,第4个数之和,第四行第2列放第1个数,第2个数,第3个数,第4个数,第5个数之和,以此类推。
ryfdizuo 2008-03-26
  • 打赏
  • 举报
回复

//源数据在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;
}
IT_lau 2008-03-25
  • 打赏
  • 举报
回复
qiang
ryfdizuo 2008-03-25
  • 打赏
  • 举报
回复

//输出数据:
1.21989 2.43985 3.65957 4.87952 6.09953 7.31924 8.5392 9.75911 10.9791 12.1988
1.21997 2.43988 3.65976 4.87972 6.09954 7.31935 8.53927 9.75924 10.9791 12.1991
0.611675 1.2235 1.83535 2.44713 3.05904 3.67091 4.28265 4.89452 5.50634 6.1181
0.611831 1.22373 1.83549 2.44723 3.05919 3.67084 4.28271 4.89446 5.5063 6.11818
0.611871 1.22365 1.83549 2.44723 3.05913 3.67089 4.28277 4.89452 5.50639 6.11819
0.611722 1.22362 1.83539 2.44728 3.05913 3.67096 4.28283 4.89449 5.50644 6.11822
0.611949 1.22361 1.83549 2.44732 3.05918 3.67108 4.28285 4.89476 5.50649 6.1183
0.611882 1.22365 1.83554 2.44731 3.05923 3.67098 4.28284 4.89463 5.50652 6.11841
0.611852 1.22362 1.83551 2.44717 3.05914 3.6709 4.28267 4.89458 5.50642 6.11819
0.611833 1.2237 1.83545 2.44733 3.05924 3.67102 4.28288 4.8947 5.50657 -9.25596e+061
ryfdizuo 2008-03-25
  • 打赏
  • 举报
回复
#include <fstream>
#include <string>
#include <iostream>
#include <assert.h>
using namespace std;


int main()
{
const int Row=10;
const int Coll=10;
double dDest[Row][Coll];

ifstream inFile("3-25.txt");
assert(inFile!=NULL);
string line;
int i=0, j=0, count=0;
double sum=0.0;
while ( !inFile.eof() )
{
j = count%Coll;
if(j == 0)
sum=0.0;
i = count/Row;
getline(inFile, line, '\n');
sum+=atof( line.c_str() );
dDest[i][j]=sum;
count++;
}
inFile.close();
for (int i=0; i<Row; i++)
for(int j=0; j<Coll; j++)
{
cout<<dDest[i][j];
if( (j+1)%Coll==0 )
cout<<endl;
else
cout<<" ";
}

return 0;
}
ryfdizuo 2008-03-25
  • 打赏
  • 举报
回复
二维数组,lz你的第二维有几个元素了?
你没有说清楚啊,
lz是不是搞三维的?呵呵
可以:
float sum=0;
ifstream inFile("input.txt");
assert(inFile!=NULL);
string line;
int i=0;
while( !inFile.eof() )
{
getline(inFile, line, '\n');
sum+=atof(line.c_str());
array[i++]=sum;
}
atof将字符串转化为float函数,累加就可以的;
great3779 2008-03-25
  • 打赏
  • 举报
回复
仅举含有5个元素的数组作答,其他类推。

#include <iostream>
using namespace std;

int main()
{
double daArray[5] = {1.21989, 1.21996, 1.21972, 1.21995, 1.22001};
double daArrayNew[5][2];

double dSum = 0;
int i, j;
//fill daArrayNew
for(i = 0; i < 5; i++)
{
dSum += daArray[i];
for(j = 0; j < 2; j++)
{
if(j == 0)
daArrayNew[i][j] = i+1;
else
daArrayNew[i][j] = dSum;
}
}

//output daArrayNew
for(i = 0; i < 5; i++)
{
for(j = 0; j < 2; j++)
{
cout<<daArrayNew[i][j]<<" ";
}
cout<<endl;
}

return 0;
}
xiaokarui1983 2008-03-25
  • 打赏
  • 举报
回复
恩,这个问题没有其他的回答了吗?期待中~
Supper_Jerry 2008-03-24
  • 打赏
  • 举报
回复
可以一次性读入一个一维数组a[]中。然后对数组处理。
array[0][1] = a[0];
for(i=1;i<nCount;i++)
{
array[i][1] = array[i-1][1]+a[i];
}
ps:你的签名好亲切!

64,677

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧