-1

I am pretty new to c++ so please pardon my incompetence. This is the code, it's about trying to find the sum of 2 matrices:

#include <iostream>
#include "functions.h"
using namespace std;

void saisir_matrice(int l, int c, int matriceA[][colonnes], int matriceB[][colonnes])
{
    cout << "Entrez vos numéros" << endl;
    for (int i = 0; i < l; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin >> matriceA[i][j];
        }
    }
}

I'm not sure what the problems are, but I have several errors on the first line, such as:

  • expected qualified id before int

  • symbol colonnes could not be resolved

  • expected ')' before ',' token

I know what these errors mean, it means there's something wrong with the way I wrote the function, but I can't seem to find them in my first line.

edit here is what functions.h looks like so far:



#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

void saisir_matrice(int lignes, int colonnes, int matriceA[][colonnes], int matriceB[][colonnes]);



#endif /* FUNCTIONS_H_ */

and here is what my main function looks like:


#include <iostream>
#include "functions.h"
using namespace std;

int main()
{
    int COLONNES;
    int LIGNES;
    cout << "entrez 2 nombres" << endl;
    cin >> COLONNES;
    cin >> LIGNES;
    int matriceA[LIGNES][COLONNES];
    int matriceB[LIGNES][COLONNES];
    saisir_matrice(LIGNES, COLONNES, matriceA, matriceB);



    return 0;

}

The point was to try and pass the arrays as variables, but there are a ton of syntax errors I can't find. Here are the error messages:

- Symbol 'colonnes' could not be 
 resolved

- Symbol 'colonnes' could not be 
 resolved

- expected ‘)’ before ‘,’ token

- expected unqualified-id before ‘int’
  • `colonnes` has not been defined in the code you’ve shown. Where is it’s definition? – Pete Becker Sep 19 '19 at 01:58
  • Please copy the exact error messages. They tend to say exactly what the problem is, as opposed to interpretations by someone who doesn't know what the problem is. – JaMiT Sep 19 '19 at 02:28
  • Also, we would need the contents of `functions.h` to have a complete example to work with. A [mcve] would be even better, both for us and for your learning. – JaMiT Sep 19 '19 at 02:29
  • remove 'colonnes' from you function declaration. First, it's undefined; Second, if even it's defined, its value wouldn't be passed to the function in C++, alas.. – AndreyS Scherbakov Sep 19 '19 at 02:40
  • You cannot create arrays with variable dimensions. Also, the 2nd dimension (colonnes) must be known at compile time in the function declaration and definition. – Eugene Sep 19 '19 at 03:03
  • `void saisir_matrice(int lignes, int colonnes, int matriceA[][colonnes], int matriceB[][colonnes]);` is not allowed in Standard C++, the array dimension must be a constant expression – M.M Sep 19 '19 at 04:35
  • Possible duplicate of [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – Alan Birtles Sep 19 '19 at 07:28

1 Answers1

0

One way of doing this can be

#include <iostream>
using namespace std;

void saisir_matrice(int l, int c, int *pmatriceA, int *pmatriceB)
{
    cout << "Entrez vos numéros" << endl;
    for (int i = 0; i < l; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin >> *(pmatriceA + (i * c) + j);
        }
    }
    for (int i = 0; i < l; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cout << *(pmatriceA + (i * c) + j) << endl;
        }
    }
}



int main()
{
    int COLONNES;
    int LIGNES;
    cout << "entrez 2 nombres" << endl;
    cin >> COLONNES;
    cin >> LIGNES;
    int matriceA[LIGNES][COLONNES];
    int matriceB[LIGNES][COLONNES];
    saisir_matrice(LIGNES, COLONNES, &matriceA[0][0], &matriceB[0][0]);
    return 0;

}
Darshan b
  • 157
  • 6