-3

I am having trouble using pointers in this program. The program has to create a matrix with random numbers in a range of 1 and a 'max' number the user writes. This is my code:

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

When I run it, I get this error:

ej2.cpp:19:21: error: cannot convert 'float** (*)[n]' to 'float**' for argument '2' to 'float** crear_matriz(int&, float**)'

I am not figuring out how to properly fix the code to avoid this problem?

Daniel
  • 10,641
  • 12
  • 47
  • 85
Mark Johnson
  • 101
  • 1
  • 12
  • Are you required to use raw pointers? It will be easier if you are allowed to use `std::vector`. – R Sahu Aug 18 '18 at 21:23
  • 5
    You seem to be writing code at random, not much of which is legal C++. What textbook are you learning this from? –  Aug 18 '18 at 21:27
  • Assuming you fixed errors, your program wouldn’t leave the `Write N....` loop – Killzone Kid Aug 18 '18 at 21:38
  • The good C++ books are [listed here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Aug 18 '18 at 21:48
  • Aside from some code that should be kept secret because it could be used as a cheap substitute for Chinese torture...your use of "n" requires it to be a constant, but again by your use it is not a constant...the compiler is not going to go along with that, ever. Circular design errors come from the extremely bad habit of trying to write code without doing any up-front analysis and design. Get out a nice clean sheet of paper (large size) and trace your code line by line, iteration by iteration, while keeping in mind exactly what the compiler is being told to do at every instant... – Dr t Aug 18 '18 at 22:10

1 Answers1

1

The program has been passed compilation and run OK:

#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 = new float*[n];

    for ( int r = 0 ; r < n ; ++r )
    {
        m[r] = new float[n];
    }

    m=create_matrix(n, m);

    for ( int r = 0 ; r < n ; ++r )
    {
        if ( NULL != m[r] )
        {
            for ( int c = 0 ; c < n ; ++c )
            {
                cout << m[r][c] <<"\t";
            }
            cout << endl;
            delete [] m[r];
            m[r] = NULL;
        }
    }
    m = NULL;

    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;
}
jackw11111
  • 1,457
  • 1
  • 17
  • 34
asdf
  • 221
  • 1
  • 10
  • 1
    You have memory leak and infinite loop if you enter N between 3 and 10 as asked. So I wouldn't call it exactly `run OK` – Killzone Kid Aug 19 '18 at 11:20