I have a code something like this, but I want to display it in two dimensional array like 4*10. I'm thinking of copying the elements of one dimensional array to two dimensional. But how can i edit this below code. Thank you.
我有一个像这样的代码,但我想用二维数组显示它,如4 * 10。我正在考虑将一维数组的元素复制到二维。但是我如何编辑以下代码。谢谢。
long int arr[40];
printf("The Fibonacci range is: ");
arr[0]=0;
arr[1]=1;
for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
for(i=0;i<range;i++)
printf("%ld ",arr[i]);
3 个解决方案
#1
check out this
看看这个
#include <stdio.h>
int main(void) {
long int arr[40];
long int twoD[4][10];
int i,range,j,k;
printf("The Fibonacci range is:\n ");
scanf("%d",&range); // getting range value from user
arr[0]=0;
arr[1]=1;
for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
i=0; // reinitialize i to 0.
for(j=0;j<4;j++){
for(k=0;k<10;k++){
twoD[j][k]=arr[i++]; // coping 1D to 2D array
}
}
for(j=0;j<4;j++){
for(k=0;k<10;k++){
printf("%ld ",twoD[j][k]); // printing 2D array
}
printf("\n");
}
return 0;
}
#2
You have all of the one dimensional completed. Using the same process you can add it to array[x][y], and loop through. The only thing is you would need to keep track of two indexes instead of one. Code it all and you will get it.
你已经完成了所有的一维。使用相同的过程,您可以将其添加到数组[x] [y],并循环。唯一的问题是你需要跟踪两个索引而不是一个索引。编码所有,你会得到它。
#3
If its only for display reasons you don't have to copy the array just fix it in the print:
如果仅出于显示原因,您不必复制数组只需将其修复为打印:
for(i=0;i<range;i++) {
if (i%10 == 0)
printf("\n");
printf("%ld ",arr[i]);
}
Thats will print the array in 4 diff lines as I guess you wanted.
多数民众赞成将按照我猜你想要的4条差异线打印阵列。
Hope that's help
希望有所帮助
#1
check out this
看看这个
#include <stdio.h>
int main(void) {
long int arr[40];
long int twoD[4][10];
int i,range,j,k;
printf("The Fibonacci range is:\n ");
scanf("%d",&range); // getting range value from user
arr[0]=0;
arr[1]=1;
for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
i=0; // reinitialize i to 0.
for(j=0;j<4;j++){
for(k=0;k<10;k++){
twoD[j][k]=arr[i++]; // coping 1D to 2D array
}
}
for(j=0;j<4;j++){
for(k=0;k<10;k++){
printf("%ld ",twoD[j][k]); // printing 2D array
}
printf("\n");
}
return 0;
}
#2
You have all of the one dimensional completed. Using the same process you can add it to array[x][y], and loop through. The only thing is you would need to keep track of two indexes instead of one. Code it all and you will get it.
你已经完成了所有的一维。使用相同的过程,您可以将其添加到数组[x] [y],并循环。唯一的问题是你需要跟踪两个索引而不是一个索引。编码所有,你会得到它。
#3
If its only for display reasons you don't have to copy the array just fix it in the print:
如果仅出于显示原因,您不必复制数组只需将其修复为打印:
for(i=0;i<range;i++) {
if (i%10 == 0)
printf("\n");
printf("%ld ",arr[i]);
}
Thats will print the array in 4 diff lines as I guess you wanted.
多数民众赞成将按照我猜你想要的4条差异线打印阵列。
Hope that's help
希望有所帮助