2015-11-06 41 views
-1

如何總結的數字,在C++ 陣列中的一個排,以及如何添加所有的數字數組中如何總結在C++中的數組

我嘗試過很多辦法的一行數字和這是我能想到的最後一種可能性。 我是編程新手。

這是代碼的這個問題

const int NUM_SALESPEOPLE = 4; 
const int NUM_PRODUCTS = 3; 
const int NUM_TOTALS = 0; 

int main() 
{ 

    unsigned __int64 id_numbers[NUM_SALESPEOPLE]; //>>>>> usigned __int64 is used in case the company have id numbers that consest of 10 digits and startr's with a 9 
    double sales_amount[NUM_SALESPEOPLE][NUM_PRODUCTS]; 
    float sales_people[NUM_SALESPEOPLE][NUM_PRODUCTS]; 
    double sales_total[NUM_SALESPEOPLE][1]; 
    int total_counter = 0; 
    int sales_total_counter = 0; 

    //input 
    for (int id_counter = 0; id_counter < NUM_SALESPEOPLE; id_counter++) 
    { 
     cout << "Enter the ID # of Salesperson # " << id_counter + 1 << " :"; 
     cin >> id_numbers[id_counter]; 

     for (int sales_counter = 0; sales_counter < NUM_PRODUCTS; sales_counter++) 
     { 
      cout << "Enter the Dollar value for the Sale of Product # " << sales_counter + 1 << " :"; 
      cin >> sales_amount[id_counter][sales_counter]; 
     } 
    } 

    //Processing 

    sales_total[0][1] += sales_amount[0][0]; 
    sales_total[1][1] += sales_amount[0][1]; 
    sales_total[2][1] += sales_amount[0][1]; 


    return 0; 
} 
+0

該示例缺少所有上下文。您將添加到不同的條目中,而不是單個總額,並且您正在讀取'sales_amount [0] [1]'兩次。 – ShadowRanger

回答

0

將新行添加到您的二維數組。嘗試這樣做:

void addRow(int array[][MAX], int row, int col, int newRowArray[], int positionToAdd) 
{ 
    for (int i = row; i > positionToAdd); --i) 
    { 
     for (int j = col; j < col; ++j) 
     { 
      array[i][positionToAdd]= array[i-1][positionToAdd]; 
     } 
     row++; 
    } 
    for (int i = 0; i < col; ++i) 
    { 
     array[col][positionToAdd] = newRowArray[i]; 
    } 
} 
0

什麼是你的陣列相關的部分?

使用for-loop語句計算數組中的總和。

int array[row][col]; 
int sum = 0; 
for(int i=0; i < col; i++){ 
    sum += array[index-of-row][i]; 
} 

希望對您有所幫助!

+0

非常感謝你,我會盡力而爲。它是一個double類型的數組。我如何添加一行數組,以及如果你能幫助我。 – ashumrani