我必须创建一个程序,打印一个具有用户指定行数和列数的数组,以及具有奇数索引的打印值(行#和列#)
这是我设法写的:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int r, c, i, j;
printf("Number of rows: ");
scanf("%d", &r);
printf("Number of columns: ");
scanf("%d", &c);
int A[r][c];
printf("Enter your numbers\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
scanf("%d", &A[i][j]);
}
for (i = 0; i < r; i++) {
printf("\n\t");
for (j = 0; j < c; j++)
printf("%d ", A[i][j]);
}
}
如果用户指定他想要4行4列,程序将打印如下内容:
X X X X
X X X X
X X X X
X X X X
如何打印程序:
X X // prints only for odd columns (1st and 3rd)
// prints nothing as the index of row is even(2nd)
X X // same as 1st row
// smae as 2nd row
答案 0 :(得分:1)
从您的示例看起来,您正在考虑第一行和第一列(索引0s
)到第一个奇数......也可能在行{和列1
中。
在这种情况下,在您的打印功能中,您可以简单地将增量字段更改为...
for (i = 0; i < r; i++) {
printf("\n\t");
if (i % 2 == 0) {
for (j = 0; j < c; j += 2)
printf("%d ", A[i][j]);
}
}
...或者您可以使用modulus (%)
运算符来检查j
的值是偶数还是奇数的状态...
for (i = 0; i < r; i++) {
printf("\n\t");
if (i % 2 == 0) {
for (j = 0; j < c; j++) {
if (j % 2 == 0) //will print columns at index 0, 2, 4, 6, ...
printf("%d ", A[i][j]);
}
}
}
我更喜欢第一个......
修改强>
如果要在输出中保留行不是奇数的空格,请不要更改i
的增量,这样每次都会打印\n
以保持这些空白空格。
答案 1 :(得分:0)
好吧,因为你想打印1..3 ... 5 ... 7索引,而不是每次在for循环中递增一个,同样,在1开始索引
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int r, c, i, j;
printf("Number of rows: ");
fflush(stdout);
scanf("%d", &r);
printf("Number of columns: ");
scanf("%d", &c);
int A[r][c];
printf("Enter your numbers\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
scanf("%d", &A[i][j]);
}
for (i = 1; i < r; i+=2) {
printf("\n\t");
for (j = 1; j < c; j+=2)
printf("%d ", A[i][j]);
}
}
答案 2 :(得分:0)
首先,如果您只是在学习C,那么您可以学习的最有价值的课程之一就是总是验证用户输入。这不需要做太多工作,只需检查scanf
的返回,如果转换成功,则验证收到的值是否在您允许的范围内。例如,由于您的rows
和column
输入用于数组边界,因此它们必须大于0
,因此您可以这样做:
int rtn = 0;
printf ("Number of rows: ");
/* VALIDATE all user input */
if ((rtn = scanf ("%d", &r)) != 1 || r < 1) {
if (rtn == EOF) { /* respect cancellation of input with EOF */
putchar ('\n'); /* tidy up */
return 1;
}
fprintf (stderr, "error: invalid number of rows provided.\n");
/* handle error as desired */
}
现在,如果您想再次显示提示,如果输入了无效的row
值,您可以简单地将整个例程置于无限循环中,并在输入有效输入时break
,例如< / p>
for (;;) {
int rtn = 0;
printf ("Number of rows: ");
/* VALIDATE all user input */
if ((rtn = scanf ("%d", &r)) != 1 || r < 1) {
if (rtn == EOF) { /* respect cancellation of input with EOF */
putchar ('\n'); /* tidy up */
return 1;
}
fprintf (stderr, "error: invalid number of rows provided.\n");
}
else
break;
}
接下来,无需使用昂贵的modulo
('%'
)测试,您真正关心的是打印奇数 row
和{{ 1}}值是您的col
值不是奇数值(因为数组索引在C中是从零开始,第一个,第三个,第五个, etc ..列都将具有 even 值),因此只需检查列的 LEAST SIGNIFICANT BIT ,例如:
col
如果你想要更短,尽管代码可读性稍差,你可以使用for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
if (!(j & 0x1)) /* check if the first bit of the column is 0 */
printf ("%2d ", A[i][j]); /* print the number */
else
printf ("__ "); /* print an equivalent blank */
putchar ('\n'); /* never use printf for a single char */
}
运算符来控制测试和打印,例如
ternary
为了适应只有奇数for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
!(j & 0x1) ? printf ("%2d ", A[i][j]) : printf ("__ ");
putchar ('\n');
}
的打印,只需检查rows
的是否有效位是否为奇数并完全跳过该行,例如
row
完全放弃,您可以执行以下操作:
for (i = 0; i < r; i++) {
if (i & 0x1) { /* skip printing of even rows */
putchar ('\n');
continue;
}
...
示例使用/输出
#include <stdio.h>
int main (void) /* make clear no arguments are expected */
{
int r, c, i, j;
for (;;) {
int rtn = 0;
printf ("Number of rows: ");
/* VALIDATE all user input */
if ((rtn = scanf ("%d", &r)) != 1 || r < 1) {
if (rtn == EOF) { /* respect cancellation of input with EOF */
putchar ('\n'); /* tidy up */
return 1;
}
fprintf (stderr, "error: invalid number of rows provided.\n");
}
else
break;
}
for (;;) {
int rtn = 0;
printf ("Number of columns: ");
if ((rtn = scanf ("%d", &c)) != 1 || c < 1) {
if (rtn == EOF) {
putchar ('\n');
return 1;
}
fprintf (stderr, "error: invalid number of columns provided.\n");
}
else
break;
}
int A[r][c];
printf ("Enter your numbers\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
if (scanf ("%d", &A[i][j]) != 1) {
fprintf (stderr, "error: invalid array input.\n");
return 1;
}
}
for (i = 0; i < r; i++) {
if (i & 0x1) { /* skip printing of even rows */
putchar ('\n');
continue;
}
for (j = 0; j < c; j++)
if (!(j & 0x1)) /* check if the first bit of the column is 0 */
printf ("%2d ", A[i][j]); /* print the number */
else
printf ("__ "); /* print an equivalent blank */
putchar ('\n'); /* never use printf for a single char */
}
}