(C语言)编写一个程序,初始化一个3x5 的二维double 数组,并利用一个基于变长数组

#include<stdio.h>
void copy(int ,double(*)[], double(*)[], int);
void display(int, double(*)[], int);
int main(void)
{
double a[3][5] = { { 1, 2, 3, 4, 5 }, { 4, 5, 6, 7, 8 }, { 7, 8, 9, 10, 11 } };
double b[3][5] ={0};

copy(5,a, b, 3);
display(5,b, 3);
return 0;
}
void copy(int COLS, double(*source)[COLS], double target[][COLS], int rows)
{
编写一个程序,初始化一个3x5 的二维double 数组,并利用一个基于变长数组的函
数把该数组复制到另一个二维数组。还要编写。个基于变长数组的函数来显示两个数组的内
容。
int i, j;
for (i = 0; i<rows; i++)
for (j = 0; j<COLS; j++)
target[i][j] = source[i][j];
}
void display(int COLS1, double(*p)[COLS1], int rows)
{
int i, j;
for (i = 0; i<rows; i++)
{
for (j = 0; j<COLS1; j++)
printf("%g\t", p[i][j]);
printf("\n");
}
}
————————————————————————————————————

错误挺多的,求帮忙修改

// ArrayCopy.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

void ArrayCopy2(int cols, double(*source)[5], double(*p), int rows);
void DisplayArray(int n, int m, double(*p));

int _tmain(int argc, _TCHAR* argv[])
{
double a[3][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } };
double b[3][5] = { 0 };

printf("Display array    b:\n");
ArrayCopy2(5, a, (double*)b, 3);
DisplayArray(5, 3, (double*)b);

while (1);
return 0;
}



void ArrayCopy2(int cols, double(*source)[5], double(*p), int rows)
{
int m = 0, n = 0;
for (m = 0; m < rows; m++)
{
for (n = 0; n < cols; n++)
{
p[m*rows+n] = source[m][n];
}
}
}

void DisplayArray(int n, int m, double(*p))
{
int i = 0, j = 0;

for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%f\n", p[i*m+j]);
}
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-02-06

// 把a, b都改成动态数组就好了,抱歉我不是很会用c,用的c++

// 改一下new -> malloc应该就可以了

#include<stdio.h>

void copy(int cols, double**, double**, int);
void display(int cols, double**, int);

int main(void)
{
int row = 3, col = 5;
double **a, **b;
a = new double* [row]; b = new double* [row];
for (int r = 0; r < row; r ++) {
a[r] = new double[col];
b[r] = new double[col];
}

a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[0][3] = 4; a[0][4] = 5;
a[1][0] = 4; a[1][1] = 5; a[1][2] = 6; a[1][3] = 7; a[1][4] = 18;
a[2][0] = 7; a[2][1] = 8; a[2][2] = 9; a[2][3] = 10; a[2][4] = 11;

for (int i = 0; i < row; i ++)
for (int j = 0; j < col; j ++)
b[i][j] = 0;

copy(col, a, b, row);
display(col, b, row);
return 0;
}

void copy(int cols, double** source, double** target, int rows)
{
/*编写一个程序,初始化一个3x5 的二维double 数组,并利用一个基于变长数组的函
数把该数组复制到另一个二维数组。还要编写。个基于变长数组的函数来显示两个数组的内
容。*/
int i, j;
for (i = 0; i<rows; i++)
for (j = 0; j<cols; j++)
target[i][j] = source[i][j];
}


void display(int cols, double** p, 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");
}
}

第2个回答  2015-02-06
#include<stdio.h>
void copy(int ,double(*)[], double(*)[], int);
void display(int, double(*)[], int);
int main(void)
{
double a[3][5] = { { 1, 2, 3, 4, 5 }, { 4, 5, 6, 7, 8 }, { 7, 8, 9, 10, 11 } };
double b[3][5] ={0};

copy(5,a, b, 3);
display(5,b, 3);
return 0;
}
void copy(int COLS, 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(int COLS1, double p[][COLS1], int rows)
{
int i, j;
for (i = 0; i<rows; i++)
{
for (j = 0; j<COLS1; j++)
printf("%g\t", p[i][j]);
printf("\n");
}
}追问

依然报错

追答

你的编译器的问题,不支持省略参数形式吧。我gcc是可以编译通过的。你用的是什么编译器?

追问

vs2013 规定让用的没办法改 那需要怎么改改呢? 谢谢

追答

你都知道了数组的长度了,要不就直接定义得了
void copy(int COLS, double source[][5], double target[][5], int rows);

void display(int COLS1, double p[][5], int rows);

追问

他这个题是希望要可以改变m和n,算基于变长数组的题目,所以才这么麻烦定义。没有别的办法了吗?

追答

void copy(int COLS, double source[][COLS], double target[][COLS], int rows);
void display(int COLS1, double p[][COLS1], int rows);

本回答被提问者采纳
相似回答