用户定义的数组大小用C [英] User defined array sizes in C

查看:128
本文介绍了用户定义的数组大小用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读通过画报C和第一个练习问题问:


  

程序MATMUL乘以固定大小的矩阵。使程序处理任何指定的大小。


所以下面是code,我想出迄今。但是我读了所有的属性所需要的主要功能前声明。所以,我怎么自定义尺寸的阵列,而不在主函数声明呢?

 的#define _CRT_SECURE_NO_DE preCATE
#包括LT&;&stdio.h中GT;诠释N,M,I,J,K;INT主要(无效)
{
    的printf(\\ n输入:行了A,对B对于A列和行,支持B \\ n列);
    scanf函数(%I%I%I,与功放I,功放;Ĵ,&安培; K);
    漂浮A [I] [J],B [J] [K],C [I] [K]; //不合法的,对不对?    / *读入数组* /
    为(N = 0; N<我++ N)
        为(M = 0; M<焦耳; ++ M)
            scanf函数(%F,&安培; A [N] [M]);    / *读取B中数组* /
    为(N = 0; N<焦耳; ++ N)
        为(M = 0; M< k ++ M)
            scanf函数(%F,和B [N] [M]);    / *计算数组c * /
    为(J = 0; J<我; ++ j)条
        对于(i = 0; I< k ++ I)
        {
            C [I] [J] = 0;
            对于(K = 0; K<焦耳; ++ K)
                C [I] [J] + = A [I] [K] * B [k]的[J]。
        }
    为(N = 0; N<我++ N)
         为(M = 0; M< k ++ M)
        的printf(\\ N%.2f \\ T,C [N] [M]);    返回0;
}


解决方案

  

漂浮A [I] [J],B [J] [K],C [I] [K]; //不合法的,对吧?


您的问题已被标记 C 沃拉斯是C99的一部分,所以漂浮A [I] [J],B [J] [K],C [I] [K]; 是合法的。

修改

如果你的编译器不支持C99,那么你只剩下比其它任何选项动态内存分配

例如:

 浮动** A;
  INT升;
  A =(浮点**)的malloc(我*的sizeof(浮动*));
  为(L = 0; L&下; I; ++升)
     A [1] =(浮点*)malloc的(J *的sizeof(浮动));


注意完成后不要忘记免费内存

I'm reading through "Illustrated C" and the first exercise question asks:

Program MATMUL multiplies matrices of fixed size. Make the program deal with any specified sizes.

So below is the code that I have come up with thus far. However I read that all attributes need to be declared before the main function. So how do I get custom sized arrays without declaring them in the main function?

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int n, m, i, j, k;

int main(void)
{
    printf("\nEnter:rows for A, columns for A and rows for B, columns for B\n");
    scanf("%i %i %i", &i, &j, &k);
    float A[i][j], B[j][k], C[i][k];    //Not legal, right?

    /*Read in A array*/
    for(n=0; n<i; ++n)
        for(m=0; m<j; ++m)
            scanf("%f", &A[n][m]);

    /*Read in B array*/
    for(n=0; n<j; ++n)
        for(m=0; m<k; ++m)
            scanf("%f", &B[n][m]);

    /*Calculate C array*/
    for(j=0; j<i; ++j)
        for(i=0; i<k; ++i)
        {
            C[i][j] = 0;
            for (k=0; k<j; ++k)
                C[i][j] += A[i][k] * B[k][j];
        }
    for(n=0; n<i; ++n)
         for(m=0; m<k; ++m)
        printf("\n%.2f\t", C[n][m]);

    return 0;
}

解决方案

float A[i][j], B[j][k], C[i][k]; //Not legal, right?

Your question has been tagged C and VLAs are part of C99, so float A[i][j], B[j][k], C[i][k]; is legal.

EDIT

If your compiler doesn't support C99 then you are left with no option other than dynamic memory allocation.

Example:

  float **A;
  int l;
  A= (float**)malloc(i*sizeof(float*));
  for(l=0;l<i;++l)
     A[l]= (float*)malloc(j*sizeof(float));


Note: Do not forget to free the memory when you are done.

这篇关于用户定义的数组大小用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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