查找最大数字出现在阵列中的次数

时间:2017-01-19 15:21:38

标签: c

我不知道该功能的错误在于" int Count_largest_even"。它应该采用给定数组中找到的最大数字(通过函数" int find")并找出该数字出现在数组中的次数。

#include "stdafx.h" 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int Count_largest_even(int size, int *array, int large);
void ArrayPrint(int a[], int size);
int find(int array[], int size);
int arr1[16] = { 2, 22, 1, 3, 24, 94, 93, 12, 12, 66666, 21, 24, 8888, 21, 2, 33 };

int main() {
    int mount;
    int even;
    ArrayPrint(arr1,16);

    even = find(arr1, 16);
    mount = Count_largest_even(16, arr1, even);
    printf("\n The biggest even digit is : %d\n   %d", even,mount);
    system("pause");
    return 0;
}

int find(int array[], int size){
    int i = 0, digit, edigit = 0;
    for (i = 0; i<size; i++){

        while (array[i]!=0)
        {
            digit = abs(array[i] % 10);
            if (digit > edigit)//checking condition for large
            {
                if (digit % 2 == 0)
                {
                    edigit = digit;
                }
            }
            array[i] = array[i] / 10;
        }
    }

    return edigit;
}

void ArrayPrint(int a[], int size)
{
    int i;
    for (i = 0; i<size; i++){
        printf("%d\n", a[i]);
    }
}

int Count_largest_even(int size, int *array, int large)
{
    int i;
    int count = 0, digit;
    for (i = 0; i < size; i++){
        while ((array[i]!=0))
        {
            digit = abs(array[i] % 10);
            if (digit == large)
            {
                count++;
            }

            array[i] = array[i] / 10;
        }
    }
    return count;
}

1 个答案:

答案 0 :(得分:0)

正如Ian Abbott所说,你不应该修改循环中的数组。

但你也可以在一次传递中做到这一点 - 一些伪代码:

int count = 0;
int largest_digit = 0;

for each digit:
   if(digit > largest_digit) {
      largest_digit = digit;
      count = 1;
   }
   else if(digit == largest_digit)
      count++;
相关问题