编写一个程序,初始化一个3x5的二维double数组,并利用一个基于变长数组的函数把该数组复制到另一个二维数组。还要编写。个基于变长数组的函数来显示两个数组的内容。这两个函数应该能够处理任意的NxM数组(如果没有可以支持变长数组的编译器,就使用传统C中处理Nx5数组的函数方法)。
#include <stdio.h> #define COLS 5 void copy(double (*)[COLS], double (*)[COLS], int ); void display(double (*)[COLS], int ); int main(void) { double source[3][COLS] = { {1.1, 2.2, 3.3, 4.4, 5.5}, {6.6, 7.7, 8.8, 9.9, 10.10}, { 11.11, 12.12, 13.13, 14.14, 15.15} }; double target[6][COLS] = {0}; copy(source, target+1, 2); puts("source:"); display(source,3); puts("\ntarget:"); display(target,5); return 0; } void copy(double ( *source )[COLS], double target[][COLS], int rows) { int i,j; for(i=0;i<rows;i++) for (j=0; j<COLS; j++) target[i][j] = source[i][j]; } void display(double ( *p )[COLS], int rows) { int i,j; for(i=0;i<rows;i++) { for (j=0; j<COLS; j++) printf("%g\t",p[i][j]); printf("\n"); } }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题