如何获得一个数组来计算有多少个数字,而不是统计每个数字的值?
问题描述:
这是程序的输出:如何获得一个数组来计算有多少个数字,而不是统计每个数字的值?
*** 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];
{
答
我不会做你的功课你,但也许这将帮助你:
你有一个数组“罪状” ...
在阵列对应于每个元素的索引你的价值观......
如果你itterate在你的价值观,你可以很容易地找到你的当前值
相应的数组元素记住,数组从0开始计数,但你的价值从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];
}
}
顺便说一下,我给你们写一些评论,所以你删除前人的精力来自你的最后工作
这是功课?格林威治标准时间午夜如何突然出现家庭作业?嗯...... –
是的,但是直到下周才推出,只是想把它弄明白。 –
到目前为止您尝试过什么?它可以帮助我们(和你)向我们展示你有多远以及你正在采取什么方法。 – M3NTA7