您好我已经编写了这个代码来读取具有给定维度的矩阵。但是我想修改它以使用cin从命令行的文件中读取矩阵。然后我希望它逐行打印非零元素后跟该值的位置。有人可以帮我修改这个。感谢。
#include <fstream>
#include <iostream>
#include <stdlib.h>
//using std:#include <fstream>
//#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
void readArray(int, int, double **);
int nz=0;
main(int argc, char *argv[])
{
int rowCT = atoi(argv[1]);
int colCT = atoi(argv[2]);
// here you reserve the space for the array
double **A = new double*[rowCT];
for (int i = 0; i < rowCT; i++)
A[i] = new double[colCT];
readArray(rowCT, colCT, A);
}
//int count = new int[rowCT];
void readArray(int r, int c, double **arr)
{
//here r rows of c elements are read in
int count[r];
int total=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>> arr[i][j];
if(arr[i][j]>0)
{
nz++;
}
}
count[i]=nz;
nz = 0;
}
cout<< r << endl;
for(int i=0; i<r; i++)
{
cout<<count[i]<< " ";
for(int j=0; j<c; j++)
{
if(arr[i][j]>0)
{
cout<< j+1 << " " << arr[i][j] << " ";}
}
cout<< endl;
}
for(int i =0; i<r; i++)
{
total+= count[i];
}
cout << total<< endl;
}