Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++中从矩阵中打印非0和位置?_C++_Arrays_Matrix - Fatal编程技术网

在C++中从矩阵中打印非0和位置?

在C++中从矩阵中打印非0和位置?,c++,arrays,matrix,C++,Arrays,Matrix,嗨,我写了这个代码来读取给定维数的矩阵。但是我想修改它,使用cin从命令行的文件中读取矩阵。然后我希望它逐行打印非零元素的位置,后跟该值。有人能帮我修改一下吗。谢谢 #include <fstream> #include <iostream> #include <stdlib.h> //using std:#include <fstream> //#include <iostream> using std::cin; using std

嗨,我写了这个代码来读取给定维数的矩阵。但是我想修改它,使用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;
}