我只想知道如何在給定列索引的情況下對特定列求和。我已經做了總和,但對于所有列
#include <stdio.h>
#define LINE 4
#define COLUMN 3
int main()
{
int arr[LINE][COLUMN] = {
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12 };
int csum = 0;
printf("\nColumn Sum....\n");
for (int i = 0; i < LINE; i )
{
for(int j = 0; j < COLUMN; j )
{
csum = csum arr[j][i];
printf("%d | csum: %d \n", i, csum);
}
}
printf("\nSum of all the elements in column is %d\n",csum);
return 0;
}
我想為該列提供 ie“0”,它回傳 22(1 4 7 10)。我嘗試對 in 中的“j”進行硬編碼csum = csum arr[j][i],csum = csum arr[0][i]但它不起作用。
uj5u.com熱心網友回復:
擺脫列回圈。
#include <stdio.h>
#define LINE 4
#define COLUMN 3
int main()
{
int arr[LINE][COLUMN] = {1,2,3,
4,5,6,
7,8,9,
10,11,12};
int csum = 0;
int column = 0; // desired column
printf("\nColumn Sum....\n");
for(int i = 0 ; i < LINE ; i )
{
csum = arr[i][column];
}
printf("\nSum of all the elements in column %d is %d\n", column, csum);
return 0;
}
您還在arr[j][i].
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354281.html
上一篇:laravel8-我可以在我的配置中訪問一個常量中的變數嗎
下一篇:“串列物件”沒有屬性“名稱”
