这是该计划的输出:
*** 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];
{
答案 0 :(得分:1)
我不会为你做功课,但也许这会对你有所帮助:
你有一个数组“计数”...
该数组中每个元素的索引对应于您的值...
如果您对值进行检测,则可以轻松找到当前值的相应数组元素
请记住,数组从0开始计数,但是您的值从1开始计数
答案 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];
}
}
顺便说一下,我为你写了一些评论,所以你可以删除你最后的作品