Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的数组和_C_Arrays_Math_Sum_Dev C++ - Fatal编程技术网

C中的数组和

C中的数组和,c,arrays,math,sum,dev-c++,C,Arrays,Math,Sum,Dev C++,各位,下面是我代码的一部分,我需要求和每一列中的内容,求和整个表中的内容,你们能帮助我如何在代码中使用数组吗?只是说说对我没有帮助,我想在代码中看到它,以便更好地理解它 void main(void){ //Matrix Declaration and Initialization with the Production Data of each Branch; //Format: productionXX[shift][week]; int productionSP[3][4] =

各位,下面是我代码的一部分,我需要求和每一列中的内容,求和整个表中的内容,你们能帮助我如何在代码中使用数组吗?只是说说对我没有帮助,我想在代码中看到它,以便更好地理解它

void main(void){
//Matrix Declaration and Initialization with the Production Data of each Branch; 
//Format: productionXX[shift][week];
    int productionSP[3][4] = {{1000, 1030,  900,  990},
                              {1010, 1045, 1100, 1015},
                              {1050, 1065, 1075, 1100}};

您可以使用如下循环来完成-

#include <stdio.h>

int main() {
    int productionSP[3][4] = {{1000, 1030,  900,  990},
                              {1010, 1045, 1100, 1015},
                              {1050, 1065, 1075, 1100}};
    int column_sum[4]={0};
    int final_sum=0;

    // i denotes iterating over each of the rows
    for(int i=0;i<3;i++){
        // j denotes iterating over each column of each row
        for(int j=0;j<4;j++){
            final_sum+=productionSP[i][j];
            column_sum[j] +=  productionSP[i][j];
        }
    }
    printf("column sums - \n");
    for(int i=0;i<4;i++){
        printf("Column #%d - %d\n",i+1,column_sum[i]);
    }
    printf("final_sum = %d",final_sum);
}
您可以根据
productionSP
数组更改循环中断条件。目前,它有3行4列的静态数据。当矩阵的大小不同时,可以相应地更改循环条件


希望这有帮助

int-sumofclumn0=productionSP[0][0]+productionSP[1][0]+productionSP[2][0]
用循环变量替换最后一个
0
s,并进行其他适当的更改:)@pmg非常感谢,这比我想象的要容易。非常感谢,补充了这个问题,它已经给了你敞开心扉的机会
column sums -                                                                                                                                                                               
Column #1 - 3060                                                                                                                                                                            
Column #2 - 3140                                                                                                                                                                            
Column #3 - 3075                                                                                                                                                                            
Column #4 - 3105                                                                                                                                                                            
final_sum = 12380