传递矩阵作为一个参数 [英] Passing matrix as a parameter

查看:210
本文介绍了传递矩阵作为一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C,我使用CUDA编译器。我要声明5矩阵,从[1] [1]到[5] [5]。使用随机值初始化。打印这些值。我已经表明了整个code我写的。我面对的问题。


  1. 传递矩阵参数


  2. 初始化矩阵(应该是常数)

     的#include<&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;无效printMatrix(INT **一,诠释行,诠释COLS)
    {
        的for(int i = 0; I<行;我++){
            对于(INT J = 0; J< COLS; J ++){
                的printf(%d个\\ t的,一个[I] [J]);
            }
            的printf(\\ n);
        }
    }诠释主(){
        INT N;    对于(INT N = 0; N< = 5; N ++)
            N = N;
            诠释一个[N] [N];        的for(int i = 0; I< N;我++)
                对于(INT J = 0; J< N; J ++){
                    一个[I] [J] =兰特()%256;
                }        的printf(Matrix一个\\ n);
            printMatrix(一,10,10);
        }
    }

    这工作正常,如果我在上面定义N种。如果我这样做,我不能使用循环改变N值。我怎样才能纠正。



解决方案

对于初学者有程序中的错字。您在循环语句后省略了开括号

 的for(int N = 0; N< = 5; N ++)
                        ^^^^
    N = N;
    诠释一个[N] [N];
    // ...

有应

 的for(int N = 0; N< = 5; N ++){
                        ^^^^
    N = N;
    诠释一个[N] [N];
    // ...

阵列 A 是一个可变长度的数组。它的尺寸可能不等于0。所以变量 N 必须从1开始,因为它是写在作业

 的for(int N = 1; N< = 5; N ++){
         ^^^^

这个函数调用

  printMatrix(A,10,10);
               ^^ ^^

没有意义,因为10号没有任何共同的数组。

和函数声明

 无效printMatrix(INT **一,诠释行,诠释COLS);
                 ^^^^^^^

是无效的。还有就是参数类型和参数类型之间的不匹配并没有从一种类型到另一种类型的隐式转换。

该程序可以像

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;无效printMatrix(为size_t行,为size_t COLS,诠释一个[] [COLS])
{
    用于(为size_t我= 0; I<行;我++)
    {
        用于(为size_t J = 0; J< COLS; J ++)的printf(%3D中,[I] [J]);
        的printf(\\ n);
    }
}INT主要(无效)
{
    常量为size_t N = 5;
    const int的UPPER_VALUE = 256;    函数srand((unsigned int类型)时间(NULL));    用于(为size_t N = 1; N< = N; N ++)
    {
        诠释一个[N] [N];        用于(为size_t我= 0; I< N;我++)
        {
            用于(为size_t J = 0; J< N; J ++)一[I] [J] =兰特()%UPPER_VALUE;
        }        的printf(矩阵A [%祖] [%祖]:\\ N,N,N);
        printMatrix(N,N,一);
        的printf(\\ n);
    }    返回0;
}

它的输出可能是

 矩阵A [1] [1]:
117矩阵A [2] [2]:
 57 216
 50 233矩阵A [3] [3]:
 42 117 215
177 218 26
202 81 163矩阵A [4] [4]:
205 178 157 2
229 165 93 94
 91 160 39 205
 26 242 131 69矩阵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

如果编译器不支持可变长度数组,那么你必须动态分配的数组。例如:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;无效printMatrix(INT **一,为size_t行,为size_t COLS)
{
    用于(为size_t我= 0; I<行;我++)
    {
        用于(为size_t J = 0; J< COLS; J ++)的printf(%3D中,[I] [J]);
        的printf(\\ n);
    }
}INT主要(无效)
{
    常量为size_t N = 5;
    const int的UPPER_VALUE = 256;    函数srand((unsigned int类型)时间(NULL));    用于(为size_t N = 1; N< = N; N ++)
    {
        INT ** A =的malloc(N * sizeof的为(int *));        用于(为size_t我= 0; I< N;我++)
        {
            一个由[i] =的malloc(N * sizeof的(INT));
            用于(为size_t J = 0; J< N; J ++)一[I] [J] =兰特()%UPPER_VALUE;
        }        的printf(矩阵A [%祖] [%祖]:\\ N,N,N);
        printMatrix(A,N,n)的;
        的printf(\\ n);        用于(为size_t我= 0; I< N;我++)免费(A []​​);
        自由(一);
    }    返回0;
}

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

  1. Passing matrix as parameter

  2. 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.

解决方案

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

这篇关于传递矩阵作为一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆