2011-11-19 96 views
-2

這是程序的輸出:如何獲得一個數組來計算有多少個數字,而不是統計每個數字的值?

*** start of 276 2D Arrays_03.cpp program *** 

Number Count  Total 
    1  3   3 
    2  6   9 
    3  15  24 
    4  6  30 
    5  9  39 

*** end of 276 2D Arrays_03.cpp program *** 

這是代碼:

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

const int COLUMN_SIZE = 13; 

int main(void) 
{ 
    const int ROW_SIZE = 3; 
    const int COUNT_SIZE = 5; 

    void countValues(const int[][COLUMN_SIZE], const int, int[]); 
    void display(const int [], const int); 

    int numbers[ROW_SIZE][COLUMN_SIZE] = {{1, 3, 4, 5, 3, 2, 3, 5, 3, 4, 5, 3, 2}, 
            {2, 1, 3, 4, 5, 3, 2, 3, 5, 3, 4, 5, 3}, 
       {3, 4, 5, 3, 2, 1, 3, 4, 5, 3, 2, 3, 5}}; 

    int counts[COUNT_SIZE] = {0}; 
    string choice; 

    cout << "*** start of 276 2D Arrays_03.cpp program ***" << endl; 
    cout << endl; 

    countValues(numbers, ROW_SIZE, counts); 

    display(counts, COUNT_SIZE); 

    cout << endl; 
    cout << endl; 
    cout << "*** end of 276 2D Arrays_03.cpp program ***" << endl << endl; 

    cin.get(); 
    return 0; 
} // end main() 

這是我需要計數的每個值的功能。我知道如何對行和列進行求和,但我不太清楚代碼是否能夠自己計算值。

void countValues(const int numbers[][COLUMN_SIZE], const int ROW_SIZE, int counts[]) 

這是我到目前爲止。

{ 
for (int index = 0; index < ROW_SIZE; index++) 
    counts[index]; 
{ 
+6

這是功課?格林威治標準時間午夜如何突然出現家庭作業?嗯...... –

+0

是的,但是直到下週才推出,只是想把它弄明白。 –

+0

到目前爲止您嘗試過什麼?它可以幫助我們(和你)向我們展示你有多遠以及你正在採取什麼方法。 – M3NTA7

回答

1

我不會做你的功課你,但也許這將幫助你:

你有一個數組「罪狀」 ...
在陣列對應於每個元素的索引你的價值觀......

如果你itterate在你的價值觀,你可以很容易地找到你的當前值

相應的數組元素記住,數組從0開始計數,但你的價值從1開始計數

1

看起來這是你的家庭作業,它似乎並不像它值得你試着去​​編寫好的代碼,而你處於這個級別,所以我只會發布代碼:

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

// if you declare these here, you don't need to pass ROW_SIZE as a parameter 
const int COLUMN_SIZE = 13; 
const int ROW_SIZE = 3; 
const int COUNT_SIZE = 5; 

// you should declare functions in the global scope 
void countValues(const int[][COLUMN_SIZE], int[]); 
void display(const int [], const int); 


int main(void) 
{  
    int numbers[ROW_SIZE][COLUMN_SIZE] = {{1, 3, 4, 5, 3, 2, 3, 5, 3, 4, 5, 3, 2}, 
            {2, 1, 3, 4, 5, 3, 2, 3, 5, 3, 4, 5, 3}, 
       {3, 4, 5, 3, 2, 1, 3, 4, 5, 3, 2, 3, 5}}; 

    int counts[COUNT_SIZE] = {0, 0, 0, 0, 0}; // <-- you should init all the five elements since COUNT_SIZE is 5 in your code 
    string choice; 

    cout << "*** start of 276 2D Arrays_03.cpp program ***" << endl; 
    cout << endl; 

    countValues(numbers, counts); 

    display(counts, COUNT_SIZE); 

    cout << endl; 
    cout << endl; 
    cout << "*** end of 276 2D Arrays_03.cpp program ***" << endl << endl; 

    cin.get(); 
    return 0; 
} // end main() 

void countValues(const int numbers[][COLUMN_SIZE], int counts[]) 
{ 
    for (int i = 0; i < ROWSIZE; ++ i) 
     for (int j = 0; j < COLUMN_SIZE; ++ j) 
     { 
      ++ counts[numbers[i][j] + 1]; 
     } 
} 

順便說一下,我給你們寫一些評論,所以你刪除前人的精力來自你的最後工作

相關問題