c++ 双维数组的问题
通宵做了半天,还是没有成功,只好来请教一下~~~~~~输入N行10列double数字,然后输入。 输入和输入都要用函数,而且必须要用到动态分配数组。想了半宿。。。。无奈啊~~
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef double* doubleP;
void fillArray(double a[][],int row,int column);
void echoing(double a[][]);
int main()
{
int column=10,row;
cout << "enter the number of rows : \n";
cin >> row;
doubleP p;
p=new double[row];
fillArray(p,row,column)
echoing(p,row,10);
}
fillArray(double a[][],int row,10)
{
int i,j;
for(i=0;i<row;i++)
a[i]=new double[10];
cout << "enter "<< column<<"double number per row,";
cout <<"total " <<row <<"rows.\n";
for(i=0;i<row;i++)
for(j=0;j<10;j+=)
cin>>a[i][j];
}
void echoing(double a[][],int row,int column)
{
int i,j;
for(i=0,i<row;i++)
for(j=0;j<column;j++)
cout << a[i][j]<<endl;
}
[解决办法]
int row;
int column=10,row;
cout << "enter the number of rows : \n";
cin >> row;
vector< vector<double> > a(row);
vector<double> b;
int i,j;
double c;
cout << "enter "<< column<<"double number per row,";
cout <<"total " <<row <<"rows.\n";
for(i=0;i<row;i++)
{
for(j=0;j<10;j++)
{
cin>>c;
if (b.size() <= j)
b.push_back(0);
else
b[j] = c;
}
if (a.size() <= i)
a.push_back(b);
else
a[i] = b;
}
[解决办法]
- C/C++ code
#include "stdafx.h"#include <iostream>using namespace std;#define COL 3void FillArray(double a[][COL], int row);void PrintArray(double a[][COL], int row);void FillArray(double** a, int row, int col);void PrintArray(double** a, int row, int col);int _tmain(int argc, _TCHAR* argv[]){ int rowNum = 0; std::cout<<"请输入行数 :"<<std::endl; std::cin>>rowNum; if (rowNum <= 0) { return 0; } //方式1 double (*pArrayA)[COL] = NULL; pArrayA = new double[rowNum][COL](); if (pArrayA == NULL) { return 0; } FillArray(pArrayA, rowNum); PrintArray(pArrayA,rowNum); delete [] pArrayA; //方式2 int colNum = 0; std::cout<<"请输入列数 :"<<std::endl; std::cin>>colNum; double* *pArrayB = NULL; pArrayB = new double*[rowNum]; if (pArrayB == NULL) { return 0; } FillArray(pArrayB, rowNum, colNum); PrintArray(pArrayB,rowNum,colNum); for (int i = 0; i < rowNum; i++) { delete [] pArrayB[i]; } delete [] pArrayB; return 0;}void FillArray(double a[][COL], int row){ double* pArray = NULL; for (int i = 0; i < row; i++) { pArray = *(a+i); std::cout<<"请输入第 "<<i+1<<" 行的数据 : "<<std::endl; for (int j= 0; j < COL; j++) { std::cin>>*(pArray+j); } }}void PrintArray(double a[][COL], int row){ std::cout<<"已保存的数据为:"<<std::endl; double* pArray = NULL; for (int i = 0; i < row; i++) { pArray = *(a+i); for (int j= 0; j < COL; j++) { std::cout<<*(pArray+j)<<" "; } std::cout<<std::endl; } std::cout<<std::endl;}void FillArray(double** a, int row, int col){ for (int i = 0; i < row; i++) { *(a+i) = new double[col](); } for (int j = 0; j < row; j++) { std::cout<<"请输入第 "<<j+1<<" 行的数据 : "<<std::endl; for (int k = 0; k < col; k++) { std::cin>>*(*(a+j)+k); } }}void PrintArray(double** a, int row, int col){ std::cout<<"已保存的数据为:"<<std::endl; for (int j = 0; j < row; j++) { for (int k = 0; k < col; k++) { std::cout<<*(*(a+j)+k)<<" "; } std::cout<<std::endl; }}
[解决办法]
太长没仔细看 动态数组我倒是懂
[解决办法]
刚学的动态数组
cin>>len;
int*p=new
int[len];
..........
delete[]p;
return0