简体   繁体   English

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

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

This is a simple program that reads in a matrix from the command line. 这是一个简单的程序,可从命令行读取矩阵。 The first 2 numbers it reads in represent the rows and columns and then it reads in a matrix of floating point numbers containing 0's. 它读取的前2个数字表示行和列,然后读取包含0的浮点数矩阵。 It reads these in line by line and stores the line temporarily in an array of floats. 它逐行读取这些内容,并将该行临时存储在浮点数组中。 While it reads in the line it checks for non zero numbers and increments nz which stores the number of non zero numbers for each row and nztotal which is the total number of non zero numbers. 当它在行中读取数据时,它会检查非零数字,并递增nz(存储每一行​​的非零数字的数目)和nztotal(非零数字的总数)。 It then goes back through this array and prints out the index and value of each non zero number. 然后返回该数组并打印出每个非零数字的索引和值。 It has to back through the array so that it can print out nz first. 它必须返回阵列,以便可以先打印出nz。 Here is the code: 这是代码:

     #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;
        }

This is a sample input: 这是一个示例输入:

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

This should produce this as an output: 这应该产生以下输出:

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

But instead it is generating a runtime error and nothing is getting printed out. 但是相反,它会生成运行时错误,并且什么都不会打印出来。 I am pretty sure it just me doing something stupid 我很确定只是我在做一些愚蠢的事情

Did you check argc ? 您检查过argc吗? argv[0] is the name of the program in C++. argv[0]是C ++中程序的名称。 See this post for some details. 有关更多详细信息,请参见这篇文章

This works better, with the main changes being the corrected initialization of ar and ac, and the first line of the inner loop. 这种方法效果更好,主要变化是对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