问题
I am new to c, I am using CUDA compiler. I want to declare five matrices, from a[1][1] to a[5][5]. Initialize them with random values. Print these values. I have shown the whole code I wrote. I face problems
Passing matrix as parameter
Initialize a matrix (should be constant)
#include <stdio.h> #include <stdlib.h> void printMatrix(int **a, int rows, int cols) { for(int i=0; i<rows; i++){ for (int j=0; j<cols; j++){ printf("%d\t", a[i][j] ); } printf("\n"); } } int main() { int N; for (int n=0; n<=5; n++) N = n; int a[N][N]; for(int i=0; i<N; i++) for (int j=0; j<N; j++){ a[i][j] = rand() % 256; } printf("Matrix A\n"); printMatrix(a, 10, 10); } }This works fine if i define N on the top. If I did that, I couldn't change N value using for loop. How can I correct.
回答1:
For starters there is a typo in the program. You omitted the open brace after the loop statement
for (int n=0; n<=5; n++)
^^^^
N = n;
int a[N][N];
//...
There should be
for (int n=0; n<=5; n++) {
^^^^
N = n;
int a[N][N];
//...
Array a is a variable length array. Its dimension may not be equal to 0. So the variable n must to start from 1 as it is written in the assignment
for (int n=1; n<=5; n++) {
^^^^
This function call
printMatrix(a, 10, 10);
^^ ^^
does not make sense because the number 10 has nothing common with the array.
And the function declaration
void printMatrix(int **a, int rows, int cols);
^^^^^^^
is invalid. There is a mismatch between the argument type and the parameter type and there is no implicit conversion from one type to another type.
The program can look like
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printMatrix( size_t rows, size_t cols, int a[][cols] )
{
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ ) printf( "%3d ", a[i][j] );
printf( "\n" );
}
}
int main( void )
{
const size_t N = 5;
const int UPPER_VALUE = 256;
srand( ( unsigned int )time( NULL ) );
for ( size_t n = 1; n <= N; n++ )
{
int a[n][n];
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) a[i][j] = rand() % UPPER_VALUE;
}
printf( "Matrix A[%zu][%zu]:\n", n, n );
printMatrix( n, n, a );
printf( "\n" );
}
return 0;
}
Its output might be
Matrix A[1][1]:
117
Matrix A[2][2]:
57 216
50 233
Matrix A[3][3]:
42 117 215
177 218 26
202 81 163
Matrix A[4][4]:
205 178 157 2
229 165 93 94
91 160 39 205
26 242 131 69
Matrix A[5][5]:
147 248 126 107 42
103 149 160 62 70
122 89 17 203 252
222 125 154 224 98
63 61 192 155 222
If the compiler does not support variable length arrays then you have to allocate the arrays dynamically. For example
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printMatrix( int **a, size_t rows, size_t cols )
{
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ ) printf( "%3d ", a[i][j] );
printf( "\n" );
}
}
int main( void )
{
const size_t N = 5;
const int UPPER_VALUE = 256;
srand( ( unsigned int )time( NULL ) );
for ( size_t n = 1; n <= N; n++ )
{
int **a = malloc( n * sizeof( int * ) );
for ( size_t i = 0; i < n; i++ )
{
a[i] = malloc( n * sizeof( int ) );
for ( size_t j = 0; j < n; j++ ) a[i][j] = rand() % UPPER_VALUE;
}
printf( "Matrix A[%zu][%zu]:\n", n, n );
printMatrix( a, n, n );
printf( "\n" );
for ( size_t i = 0; i < n; i++ ) free( a[i] );
free( a );
}
return 0;
}
回答2:
for (int n=0; n<=5; n++){ /* <--missing { which causes N to be 6 after last iteration */
//your code
}
You missed a { after first for loop in main that's why int a[N][N] and other loops are not inside its body (which you probably want "changing value of N ")
回答3:
It seems that the number of matrices is constant, so just #define it at the top. Without touching your printMatrix method, you can have a main body as follows:
#define N 5
int main(int argc, char ** argv)
{
int **data[N]; // Array of length N of int **, where each int ** will store a matrix.
for (int i = 0; i < N; i++) {
int matrixSize = i + 1; // creating the ith matrix
data[i] = malloc(matrixSize * sizeof *data[i]);
for (int j = 0; j < matrixSize; j++) {
data[i][j] = malloc(matrixSize * sizeof *data[i][j]);
for (int k = 0; k < matrixSize; k++) {
data[i][j][k] = rand() % 256;
}
}
}
// Printing the first one
printMatrix(data[0], 1, 1);
// don't forget to loop again to free the buffers allocated...
return 0;
}
来源:https://stackoverflow.com/questions/35548901/passing-matrix-as-a-parameter