如何在c中乘以表中的值
我是编程新手,被要求创建一个包含3个变量的表如何在c中乘以表中的值,c,C,我是编程新手,被要求创建一个包含3个变量的表x,y和z 为了创建x和y,我被要求使用for循环,并且已经这样做了。对于z,我必须将x和y的值相乘,但我不能完全确定如何计算z,以及如何将其放在表格中 请帮忙。我举了一个例子,说明我的结果应该是怎样的 到目前为止我所做的: int x, y, z; for (x = 1; x <= 4; x++) printf(" %d ", x); for (y = 2; y <= 5; y++) printf(" %d ", y
x
,y
和z
为了创建x
和y
,我被要求使用for循环,并且已经这样做了。对于z
,我必须将x
和y
的值相乘,但我不能完全确定如何计算z
,以及如何将其放在表格中
请帮忙。我举了一个例子,说明我的结果应该是怎样的
到目前为止我所做的:
int x, y, z;
for (x = 1; x <= 4; x++)
printf(" %d ", x);
for (y = 2; y <= 5; y++)
printf(" %d ", y);
return 0;
intx,y,z;
对于(x=1;x而言,数据结构应该不复杂
int matrix[3][5];
for(i=0; i<5;i++){
matrix[0][i]=i+1;
matrix[1][i]=i+2;
matrix[2][i]=matrix[0][i]*matrix[1][i];
}
int矩阵[3][5];
对于(i=0;iintx[]={1,2,3,4,5,…}如果任务只是打印一个表,如发布的表,则只需一个循环:
#include <stdio.h>
int main(void)
{
// print the header of the table
puts("======================\n x y z = x * y\n----------------------");
for ( int x = 1; // initialize 'x' with the first value in the table
x <= 5; // the last value shown is 5. 'x < 6' would do the same
++x ) // increment the value after each row is printed
{
int y = x + 1; // 'y' goes from 2 to 6
int z = x * y; // 'z' is the product of 'x' and 'y'
// print each row of the table, assigning a width to each column,
// numbers are right justified
printf("%3d %3d %3d\n", x, y, z);
}
puts("======================");
return 0;
}
#包括
内部主(空)
{
//打印表格的标题
puts(“=====================================\n x y z=x*y\n------------------------------”;
for(int x=1;//使用表中的第一个值初始化“x”
x在x
和y
之间有一个紧密的模式,这意味着如果你知道x
,那么你也知道y
,并且可以很容易地将这两个值相乘。这也意味着你只需要一个循环,在每个循环中调用一次printf
。
#include <stdio.h>
int main(void)
{
// print the header of the table
puts("======================\n x y z = x * y\n----------------------");
for ( int x = 1; // initialize 'x' with the first value in the table
x <= 5; // the last value shown is 5. 'x < 6' would do the same
++x ) // increment the value after each row is printed
{
int y = x + 1; // 'y' goes from 2 to 6
int z = x * y; // 'z' is the product of 'x' and 'y'
// print each row of the table, assigning a width to each column,
// numbers are right justified
printf("%3d %3d %3d\n", x, y, z);
}
puts("======================");
return 0;
}
======================
x y z = x * y
----------------------
1 2 2
2 3 6
3 4 12
4 5 20
5 6 30
======================