【发布时间】:2016-05-11 16:14:34
【问题描述】:
新手又来了,对于这篇文章的家庭作业质量,我深表歉意。
我不确定我的标题是否完全合理,但我正在尝试将数组放大(或缩小,如有必要)一个变量整数,并且我正在寻求帮助以了解其背后的数学和逻辑。 (这应该类似于将对象放大或缩小 n 倍。)
例如,如果有一个 2x2 数组,像这样:
0 1
2 3
我希望能够将它乘以 n = 2,并得到以下结果:
0 0 1 1
0 0 1 1
2 2 3 3
2 2 3 3
我希望能够将 n 设置为任何值,并让它按此值“调整”数组的大小(行和列应该适当地增长和缩小。) 我的代码没有像我想象的那样工作,我希望有人可以为我分解它(或指导我到哪里可以找到正确执行此操作的信息)。 到目前为止,这是我能够做的事情,但随着我增加或减少调整大小的值而失败。 (我正在尝试将数组写入单独的文件):
#define enn 2 //resize value.
//fill the infile with an array contents.
int inArray[SIZE][SIZE];
int count = 0, row, column;
//intialize the array.
for (row = 0; row < SIZE; row++)
{
for(column = 0; column < SIZE; column++)
{
inArray[row][column] = count;
count += 1;
}
}
//write to the infile.
for (row = 0; row < SIZE; row++)
{
for(column = 0; column < SIZE; column++)
{
fprintf(infileptr, "%i\t", inArray[row][column]);
}
fputs("\n", infileptr);
}
fclose(infileptr);
//write expanded array to outfile.
for (row = 0; row < SIZE; row++)
{
for(column = 0; column < SIZE; column++)
{
for(int m = 0; m < enn; m++)
{
fprintf(outfileptr, "%i\t", inArray[row][column]);
}
}
fputs("\n", outfileptr);
for(column = 0; column < SIZE; column++)
{
for(int m = 0; m < enn; m++)
{
fprintf(outfileptr, "%i\t", inArray[row][column]);
}
}
fputs("\n", outfileptr);
}
fclose(outfileptr);
感谢您的帮助!
【问题讨论】:
-
哇,这对 APL 来说是一份完美的工作。 :)