0

This is the error: cannot convert 'double ( * )[3]' to 'int (*)[3] transpose(matrixA, matrixB); but the same code is working when I'm changing the data type to int. How can I fix my code?

#include <iostream>
using namespace std;
#define M 3
#define N 4

// This function stores transpose of A[][] in B[][]
void transpose(double A[][N], int B[][M])
{
    double i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < M; j++)
            B[i][j] = A[j][i];
}

// This function prints the elements of the matrix.
void print(double matrix[][M])
{
    cout << "The resultant matrix is" << endl;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
}

int main()
{
    double matrixA[M][N] = {{1.3, 2, 3, 4.5},
                         {5, 6.6, 7.8, 8},
                         {4.7, 5, 3, 2.9}};

    double matrixB[N][M], i, j;

    transpose(matrixA, matrixB);
    print(matrixB);
    return 0;
}

Thank You

  • 3
    `void transpose(double A[][N], int B[][M])` Your second parameter should be `double`, should it not? – sweenish Jun 08 '21 at 04:52
  • 4
    `for (double i = 0; i < N; i++)` Although your array contains doubles the indices are still integers. You should use int for your index variables as done in the other function. – Retired Ninja Jun 08 '21 at 04:53
  • 1
    why do you edit the whole code? that makes existing answers invalid – phuclv Jun 08 '21 at 05:12
  • There are no decimals here. Decimal is a radix. Floating-point is used for *real numbers,* and the part after the decimal point is the fractional part. Please don't misuse standard terminology. – user207421 Jun 08 '21 at 05:20
  • Sorry for the inconvenience guys I changed the whole code accidently, but now I've undone my mistake. – Nishant Sagar Jun 08 '21 at 05:28

2 Answers2

3

Firstly, in void transpose(double A[][N], int B[][M]) the type of B should be double , as it is declared in main : double matrixB[N][M].

Secondly, while using double as loop counter isn't necessarily going to raise an error, accessing array requires indexes to be integers.

As @ivan.ukr mentioned:

Standard C header file stddef.h (or cstdddef in C++) defines (typically, via typedef) special unsigned integer data type size_t (or std::size_t in C++) which should be normally used as size of memory are or array or memory / array index.

So your code now is :

#include <iostream>
using namespace std;
#define M 3
#define N 4

// This function stores transpose of A[][] in B[][]
void transpose(double A[][N], double B[][M])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < M; j++)
            B[i][j] = A[j][i];
}

// This function prints the elements of the matrix.
void print(double matrix[][M])
{
    cout << "The resultant matrix is" << endl;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
}

int main()
{
    double matrixA[M][N] = {{1.3, 2, 3, 4.5},
                         {5, 6.6, 7.8, 8},
                         {4.7, 5, 3, 2.9}};

    double matrixB[N][M], i, j;

    transpose(matrixA, matrixB);
    print(matrixB);

    return 0;
}

Result:

The resultant matrix is
1.3 5 4.7
2 6.6 5
3 7.8 3
4.5 8 2.9
silverfox
  • 1,568
  • 10
  • 27
1

In C and C++ you should normally use integer types for expressing array sizes ans indexes. Because memory addresses and as result, differences of memory addresses, and offsets from some starting address are all integer numbers, not floating point ones. That's why compiler gives you such an error, and int works but double does not. Furthermore, standard C header file stddef.h (or cstdddef in C++) defines (typically, via typedef) special unsigned integer data type size_t (or std::size_t in C++) which should be normally used as size of memory area or array or memory / array index. There is also another typedefed type called ptrdiff_t (and std::ptrdiff_t in C++) which represent difference between two memory addresses, but ptrdiff_t is signed type and can be negative.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41