将 header 添加到二维数组
Adding a header to a 2d array
试过这个但没有按预期工作:
#include <stdio.h>
int main()
{
int arr[2][2] = {10,11,12,13};
int i,j;
for(i = 0; i<2; i++)
{
printf("ds%d",i+1);
printf("\n");
for(j = 0; j<2; j++)
{
printf("%d\t", arr[i][j]);
}
}
return 0;
}
出现的结果:
ds1
10 11 ds2
12 13
所需结果应如下所示:
ds1 ds2
10 11
12 13
您需要先单独打印表头。
int main()
{
int arr[2][2] = {10,11,12,13};
int i,j;
/* This loop prints the headers */
for(i = 0; i<2; i++)
{
printf("ds%d\t",i+1);
}
printf("\n"); /* new line to start printing data */
/* This loop prints the data */
for(i = 0; i<2; i++)
{
for(j = 0; j<2; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n"); /* new line after each row of data */
}
return 0;
}
试过这个但没有按预期工作:
#include <stdio.h>
int main()
{
int arr[2][2] = {10,11,12,13};
int i,j;
for(i = 0; i<2; i++)
{
printf("ds%d",i+1);
printf("\n");
for(j = 0; j<2; j++)
{
printf("%d\t", arr[i][j]);
}
}
return 0;
}
出现的结果:
ds1
10 11 ds2
12 13
所需结果应如下所示:
ds1 ds2
10 11
12 13
您需要先单独打印表头。
int main()
{
int arr[2][2] = {10,11,12,13};
int i,j;
/* This loop prints the headers */
for(i = 0; i<2; i++)
{
printf("ds%d\t",i+1);
}
printf("\n"); /* new line to start printing data */
/* This loop prints the data */
for(i = 0; i<2; i++)
{
for(j = 0; j<2; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n"); /* new line after each row of data */
}
return 0;
}