繁体   English   中英

C中的单个数字乘以多维数组

[英]Multiply a single number to a multidimensional array in C

我正在尝试将我选择的数字与我选择的值相乘,但我的代码没有这样做。

这里是乘法 function:

int product_array(){
i = 0;
j = 0;
int number;
int product[i][j];
printf("Insert the number\n");
scanf("%d", &number);
while (i < rows) {
    printf("\n");
    j = 0;
    while (j < columns) {
        product[i][j] = number + product[0][0] * m_array[i][j];
        j++;
    }
    i++;
}
i = 0;
j = 0;
printf("Here it is the product\n");
while (i < rows) {
    printf("\n");
    j = 0;
    while (j < columns) {
        printf("\t%d ", product[i][j]);
        j++;
    }
    i++;
    printf("\n");
}
return product[i][j];
}

图注: m_array表示multidimensional array

输入广告示例 output

输入

  • m_array 值 = 2 2 2 2
  • 数字 = 2

Output

  • 4 4 4 4

为了更清楚,这是用户可以将值插入m_array的一段代码:

do {
    printf("How many rows will be in the array?\n");
    scanf("%d", &righe);
} while (rows > Row || rows < 1);

do {
    printf("How many columns will be in the array?\n");
    scanf("%d", &columns);
} while (columns > Column || columns < 1);

printf("Data input\n");
while (i < rows) {
    j=0;
    while (j < columns) {
        printf("Insert the element in the %d column and %d row\n", i, j);
        scanf("%d", &m_array[i][j]);
        j++;
    }
    i++;
}

好的,所以我弄清楚了问题所在(很简单),这是解决方案的代码:

int product_array(){
i = 0;
j = 0;
int number;
printf("Insert the number\n");
scanf("%d", &number);
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
    m_array[i][j] = number * m_array[i][j];
    j++;
}
i++;
}
i = 0;
j = 0;
printf("Here it is the product\n");
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
    printf("\t%d ", m_array[i][j]);
    j++;
}
i++;
printf("\n");
}
return m_array[i][j];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM