读取矩阵并打印出非零数字的我的c ++程序正在生成运行时错误

时间:2015-10-16 21:20:31

标签: c++ matrix

这是一个从命令行读取矩阵的简单程序。它读入的前2个数字代表行和列,然后读入包含0的浮点数矩阵。它逐行读取这些并将该行暂时存储在浮点数组中。当它在行中读取时,它检查非零数字和增量nz,其存储每行的非零数字的数量和nztotal,即非零数字的总数。然后它返回通过此数组并打印出每个非零数字的索引和值。它必须通过数组返回,以便它可以先打印出nz。这是代码:

     #include <iostream>
        #include <stdlib.h>
        using namespace std;

        int main(int argc, char* argv[]) {
            //puting the rows and columns into ints
            int ar = atoi(argv[1]);
            int ac = atoi(argv[2]);
            //printing out the rows;
            cout<< ar;
            int nz = 0;
            int nztotal = 0;
            //creating an array of floats
            float* arr = new float[ac];
            //reading through line by line
            for(int i = 0; i < ar;i++)
            {
                cout << endl;
                nz = 0;
                //reading through number by number
                for(int j = 0;j < ac; j++)
                {
                    //storing each number in the array
                    cin>> arr[j];
                    //checking if the number is non-zero and incrementing nz and nztotal
                    if(arr[j] != 0)
                    {
                        nz++;
                        nztotal++;
                    }
                }
                //printing out nz
                cout<< nz;
                //reading through the array and printing out the values and index of each non-zero number
                for(int j = 0;j < ac; j++)
                {
                    if(arr[j] != 0)
                    {
                        int temp = j + 1;
                        cout<< temp << arr[j];
                    }
                }
            }
            cout<< nztotal;
        }

这是一个示例输入:

4 4 2 0 3 0 2 0 3 0 2 0 3 0 2 0 3 0

这应该产生这个作为输出:

4
2 1 2 3 3
2 1 2 3 3
2 1 2 3 3
2 1 2 3 3
8

但它正在生成运行时错误,并且没有任何内容被打印出来。我很确定这只是我做了一些愚蠢的事情

2 个答案:

答案 0 :(得分:1)

您检查过argc了吗? argv[0]是C ++中程序的名称。有关详细信息,请参阅this post

答案 1 :(得分:0)

这样做效果更好,主要的变化是更正了ar和ac的初始化,以及内循环的第一行。

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, const char* argv[]) {
    //puting the rows and columns into ints
    int ar = atoi(argv[1]);
    int ac = atoi(argv[2]);

    //printing out the rows;
    cout<< ar;
    int nz = 0;
    int nztotal = 0;

    //creating an array of floats
    float* arr = new float[ac];

    //reading through line by line
    for(int i = 0; i < ar;i++)
    {
        cout << endl;
        nz = 0;

        //reading through number by number
        for(int j = 0;j < ac; j++)
        {
            //storing each number in the array
            arr[j] = atoi(argv[(ar * i) + j + 3]);

            //checking if the number is non-zero and incrementing nz and nztotal
            if(arr[j] != 0)
            {
                nz++;
                nztotal++;
            }
        }

        //printing out nz
        cout<< nz;

        //reading through the array and printing out the values and index of each non-zero number
        for(int j = 0;j < ac; j++)
        {
            if(arr[j] != 0)
            {
                int temp = j + 1;
                cout<< temp << arr[j];
            }
        }
    }

    cout<< nztotal;
}