2015-11-06 36 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