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

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

本文关键字:一个 数字 运行时错误 产生了 读取 程序 c++ 我的 打印      更新时间:2023-10-16

这是一个从命令行读取矩阵的简单程序。它读入的前两个数字代表行和列,然后读入一个包含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;
}