我正在尝试编写一个函数,它接受一个数组的大小和一个由数字组成的int数组作为输入,并打印每个数字的频率。
示例输入和输出:
Input: [1,2,2,3,3,3]
Output:
1 occurs 1 times.
2 occurs 2 times
3 occurs 3 times.
这是我的尝试(不是最优雅的):
void freq(int size, int numArray[]) {
int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
int i, j;
for (i = 0; i < size; i++) {
for (j = 1; j < size; j++) {
if (numArray[i] == numArray[j] && numArray[i] == 1) {
one+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 2) {
two+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 3) {
thr+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 4) {
fou+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 5) {
fiv+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 6) {
six+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 7) {
sev+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 8) {
eit+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 9) {
nin+=1;
}
}
}
printf("1 occurs %d times.\n", one);
printf("2 occurs %d times.\n", two);
printf("3 occurs %d times.\n", thr);
printf("4 occurs %d times.\n", fou);
printf("5 occurs %d times.\n", fiv);
printf("6 occurs %d times.\n", six);
printf("7 occurs %d times.\n", sev);
printf("8 occurs %d times.\n", eit);
printf("9 occurs %d times.\n", nin);
}
这有问题。如果我使用与上面相同的例子,这就是我得到的:
Input: [1,2,2,3,3,3]
Output:
1 occurs 0 times.
2 occurs 4 times.
3 occurs 9 times.
答案 0 :(得分:5)
嵌套循环方法毫无意义,您只需要查看每个数字一次就可以计算它。当然,数组更有意义保留计数器:
void freq(int size, const int *numbers)
{
unsigned int counts[10] = { 0 };
for(int i = 0; i < size; ++i)
{
const int here = numbers[i];
if(here >= 1 && here <= 9)
counts[here]++;
}
for(int i = 1; i < 10; ++i)
printf("%d occurs %u times\n", i, counts[i]);
}
答案 1 :(得分:1)
你的逻辑有缺陷。你想要的是在数组上迭代一次并检查每个元素。像这样的东西:
for(int i = 0; i < size; i++){
if (numArray[i] == NUMBER)
NUMBER_COUNTER ++;
.
.
.
或者,使用原始代码:
void freq(int size, int numArray[]) {
int one=0, two=0, thr=0;
int i;
for (i = 0; i < size; i++) {
if (numArray[i] == 1) {
one+=1;
}
else if (numArray[i] == 2) {
two+=1;
}
else if (numArray[i] == 3) {
thr+=1;
}
}
printf("1 occurs %d times.\n", one);
printf("2 occurs %d times.\n", two);
printf("3 occurs %d times.\n", thr);
}
我认为更优雅的另一种解决方案是: 创建一个包含9个元素的新数组(假设您想要计算9位数的频率......)并增加找到的数字的位置......就像这样:
void freq(int size, int numArray[]) {
int freq_arr[size];
int i;
for(i = 0; i < size; i++)
freq_arr[ numArray[i] ] ++;
for(i = 0; i < size; i++)
printf("i: %d = %d\n", i, freq_arr[i]);
答案 2 :(得分:0)
你需要内循环什么?您需要的是在每次迭代中查看每个数字一次以便对其进行计数。它比你想象的要简单。将您的代码更改为:
void freq(int size, int numArray[]) {
int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
int i, j;
for (i = 0; i < size; i++) {
if (numArray[i] == 1)
one+=1;
else if (numArray[i] == 2)
two+=1;
else if (numArray[i] == 3)
thr+=1;
else if (numArray[i] == 4)
fou+=1;
else if (numArray[i] == 5)
fiv+=1;
else if (numArray[i] == 6)
six+=1;
else if (numArray[i] == 7) {
sev+=1;
else if (numArray[i] == 8)
eit+=1;
else if (numArray[i] == 9)
nin+=1;
}
printf("1 occurs %d times.\n", one);
printf("2 occurs %d times.\n", two);
printf("3 occurs %d times.\n", thr);
printf("4 occurs %d times.\n", fou);
printf("5 occurs %d times.\n", fiv);
printf("6 occurs %d times.\n", six);
printf("7 occurs %d times.\n", sev);
printf("8 occurs %d times.\n", eit);
printf("9 occurs %d times.\n", nin);
}
另一种方法不是将十个int
变量保留为计数器,而是保留十个int
的数组,如下所示:
void freq(int size, int numArray[]) {
int i;
int counters[10];
//initialize the array's elements to zero
for (i = 0; i < 10; i++)
counters[i] = 0;
for(i = 0; i < size; i++)
{
if (numArray[i] = i)
counters[i]++;
}
}
答案 3 :(得分:0)
void freq(int size, const int numArray[]) {
int cnt[10] = {0};
for (int i=0; i<size; i++) {
if (0 <= numArray[i] && numArray[i] <= 9)
cnt[numArray[i]]++;
}
// cnt = {0, 3, 1, 0, 0, 0, 0, 0, 0, 0}
}
// test
int main(int argc, char* argv[])
{
int arr[4] = {1,1,1,2};
freq(4, arr);
return 0;
}