如何在C ++中使用指针和数组

时间:2018-08-18 21:21:30

标签: c++ arrays pointers

在此程序中使用指针时遇到麻烦。该程序必须创建一个矩阵,该矩阵的随机数在1到用户写的“最大”数之间。这是我的代码:

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

int get_n();
float** create_matrix(int &n, float** m);

int main (int argc, char *argv[]) {

    int n;
    n=get_n();
    float** m[n][n];
    m=create_matrix(n, m);

    return 0;
}

int get_n(){
    int n;
    do
    {
        cout<<"Write N between 3 and 10: ";
        cin>>n;
    } while (n<11&&n>2);
    cout<<endl;
    return n;
}

float** create_matrix(int &n, float** m){
    int maxi;

    srand(time(NULL));      
    cout<<"Insert max term: ";
    cin>>maxi;
    for (int i=0; i<n; i++){
        for (int j=0; j<n; j++){
            float aux=0;
            aux=1+rand()%((1+maxi)-1);  
            m[i][j]=aux;
        }
    }
    return m;
}

运行它时,出现此错误:

  

ej2.cpp:19:21:错误:无法将参数'2'的'float **(*)[n]'转换为'float **'到'float ** crear_matriz(int&,float **) '

我没有弄清楚如何正确修复代码以避免此问题?

1 个答案:

答案 0 :(得分:1)

程序已通过编译并运行正常:

const bots = ['0_applebadapple_0','bananennanen','commanderroot', 'decafsmurf', 'electricallongboard','electricalskateboard','lanfusion','skinnyseahorse','slocool', 'woppes'] //list to check against
const hide = () =>{
    const buttons = document.getElementsByClassName('chat-viewers-list__button') //gets HTMLCollection of elements
    Array.prototype.forEach.call(buttons, button => {
        if (bots.includes(button.dataset.username)) {
            button.parentNode.parentNode.removeChild(button.parentNode)
        }} //runs a for each through the HTMLCollection, remove parent element if username is in list
    )}
hide()