简体   繁体   English

具有几何级数的C ++用户定义的二维数组

[英]C++ User-defined 2 dimensional array with geometrical progression

I am learning C++ by myself,by solving different problems. 我正在通过解决不同的问题自己学习C ++。 I am trying to solve problem which was originally designed for Pascal in C++. 我正在尝试解决最初为C ++中的Pascal设计的问题。 It should ask user to input 3 integers M,N and q. 它应该要求用户输入3个整数M,N和q。 Then it should make 2d array of integers with size MxN, where all the elements of (I=I,...M) line will be the members of geometrical progression with first element equal to number of line (I) and denominator q. 然后,它应该使尺寸为MxN的2d整数数组组成,其中(I = I,... M)线的所有元素都是几何级数的成员,第一个元素等于线(I)的数量和分母q。

I wanted to create a dynamic massive, but I realized that it won't really work with two undefined integers. 我想创建一个动态质量块,但是我意识到它实际上不能用于两个未定义的整数。 So, I tried vectors. 因此,我尝试了矢量。 I guess that I created them in a right way, but I've got no idea how to make a geometrical progression. 我想我以正确的方式创建了它们,但是我不知道如何进行几何级进。

Here is my code: 这是我的代码:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int m, n, q;

    cout << "Enter the number for M \n";
    cin >> m;
    if (cin.fail())
    {
        cin.clear();
        cin.ignore();
        cout << "This is not a number! " << endl;
        system("pause");
        return 0;
    }
    cout << "Enter the  number for N \n";
    cin >> n;
    if (cin.fail())
    {
        cin.clear();
        cin.ignore();
        cout << "This is not a number! " << endl;
        system("pause");
        return 0;
    }
    cout << "Enter the number  for Q \n";
    cin >> q;
    if (cin.fail())
    {
        cin.clear();
        cin.ignore();
        cout << "This is not a number! " << endl;
        system("pause");
        return 0;
    }
    int** matrix;
    matrix = new int*[m];
    for (int i = 0; i < m; i++)
        matrix[i] = new int[n];


    for (int i = 0; i < m; i++)
    {
        matrix[i][0] = i + 1;
    }
    for (int i = 0; i < m; i++)
    {
        for (int j = 1; j < n; j++)
        {
            matrix[i][j] = (i + 1)*pow(i, j);
            cout << matrix[i][j];

        }
    }
    system("pause");
    return 0;





}

Note: You can create a two dimensional array of a variable size, although it involves memory allocation and is slightly ugly. 注意:您可以创建一个大小可变的二维数组,尽管它涉及内存分配并且有点难看。

int** matrix;
matrix = new int*[M];
for (int i = 0; i < M; i++)
    matrix[i] = new int[N];

That's the code to create an array of size MxN. 这是创建大小为MxN的数组的代码。 Don't forget to deallocate your memory like so: 不要忘记像这样释放内存:

for (int i = 0; i < M; i++)
    delete matrix[i];
delete matrix;

As far as your question about the geometric progression, I am unsure of what you are asking. 至于您关于几何级数的问题,我不确定您要问什么。 When you say geometric progression do you refer to something along the lines of 2 10 50 250 etc.? 当您说几何级数时,您是否指的是2 10 50 250等。 I am not sure what you mean by "lines" as you don't refer to any such variable in your code. 我不确定您所说的“行”是什么意思,因为您没有在代码中引用任何此类变量。

EDIT 编辑

So once the MxN matrix is created, iterate through the rows and initialize the rows like so: 因此,一旦创建了MxN矩阵,就可以遍历行并初始化行,如下所示:

for (int i = 0; i < M; i++)
{
    matrix[i][0] = i+1;
}

This should set the first column of each row to the correct number. 这应将每一行的第一列设置为正确的数字。 Then something along the lines of this should fill out the rest of the geometric progression: 然后,遵循此思路的东西应该填充其余的几何级数:

for (int i = 0; i < M; i++)
{
    for (int j = 1; j < N; j++)
    {
        matrix[i][j] = (i+1)*pow(r,j); 
        //note that you'll probably have to do some typecasting
        //p.s. I'm not 100% sure that this is the correct formula
    }
}

I think this is what you are looking for. 我认为这就是您想要的。 Let me know if it works because I haven't tested it myself. 让我知道它是否有效,因为我自己还没有测试过。

Print the matrix like this: 像这样打印矩阵:

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        std::cout << matrix[i][j] << " ";
    }
    std::cout << "\n";
}

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

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