繁体   English   中英

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

[英]My c++ program that reads in a matrix and prints out the non-zero numbers is generating a runtime error

这是一个简单的程序,可从命令行读取矩阵。 它读取的前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

但是相反,它会生成运行时错误,并且什么都不会打印出来。 我很确定只是我在做一些愚蠢的事情

您检查过argc吗? argv[0]是C ++中程序的名称。 有关更多详细信息,请参见这篇文章

这种方法效果更好,主要变化是对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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM