簡體   English   中英

在數組中搜索特定的數字並計數出現的次數

[英]search array for specific number and count how many times it appears

我的年齡介於0到100之間。 您一直輸入年齡,直到輸入前哨數據(-99)。

然后,我需要計算每個年齡出現多少次。

示例輸出:
5歲的人數是3
8歲的人數是1
9歲的人數是1
10歲的人數是1
17歲的人數是1
20歲的人數是2
100歲的人數是1

但是,當我這樣做時,我的輸出是這樣的:

5歲的人數是3
10歲的人數是1
100歲的人數是1
20歲的人數是2
5歲的人數是3
8歲的人數是1
20歲的人數是2
5歲的人數是3
9歲的人數是1
17歲的人數是1

我需要修復它,以便它只會顯示5一次,等等。

這是我的代碼:

    #include <iostream>
    using namespace std;



    const int MAXSIZE = 100;
    int ages[MAXSIZE];

    int findNumOfAges(int ages[], int pos, int i);

    int main()
    {
        int pos = 0;
        int age;

    for(int count = 0; count < MAXSIZE; count++)
{
    ages[count] = 0;
}

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ages[pos] = age;

    }
    pos++;

}while(age != -99);

    for(int i = 0; i < pos; i++)
    {
        int numOfAges;

        numOfAges = findNumOfAges(ages, pos, i);

        cout << "The number of people age " << ages[i] << " is " << numOfAges << endl;
    }

}

int findNumOfAges(int ages[], int pos, int i)
{
int numOfAges = 0;

for(int j = 0; j < pos; j++)
{
    if(ages[i] == ages[j])
    {
        numOfAges += 1;
    }
}
return numOfAges;
}

首先這個循環

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ages[pos] = age;

    }
    pos++;

}while(age != -99);

是無效的。 即使輸入的數字不屬於可接受的范圍,也會增加pos。

它應該看起來如下方式

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ages[pos++] = age;

    }

} while( age != -99 && pos < MAXSIZE );

同樣在函數中,您應該檢查元素是否已經計數。 我會這樣寫

int findNumOfAges( const int ages[], int pos, int i)
{
   int numOfAges = 0;

   int j = 0;
   while ( j < i && ages[i] != ages[j] ) j++;

   if ( j == i )
   {
       for( ; j < pos; j++ )
       {
          numOfAges += ages[i] == ages[j];
       }
   }

   return numOfAges;
}

並且主要,您必須檢查返回的值。 如果它等於0,那么您必須跳過此元素,因為已經對其進行了計數。

您也可以根據標准算法編寫函數。 例如

#include <algorithm>
//,,,

int findNumOfAges( const int ages[], int pos, int i)
{
   const int *first = std::find( ages, ages + i, ages[i] );

   if ( first != ages + i ) return 0;

   return ( std::count( first, ages + pos, ages[i] ) );  
}

另一種方法是使用std::map

在這種情況下,您只需根據數組構建地圖

#include <map>

//...

std::map<int, int> m;

for ( int i = 0; i < pos; i++ ) ++m[ages[i]];

for ( const auto &p : m )
{
   std::cout << p.first << '\t' << p.second << std::endl;
}

您也可以從一開始就使用std :: map而不是數組。:)

例如

#include <map>

//...

std::map<int, int> m;

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ++m[age];

    }
} while( age != -99 );

您需要第二個數組。

首先獲取年齡數組中的所有年齡。 (你做到了。)

然后再創建一個大小為100的int數組。

遍歷ages數組的每個int,直到達到-99。

對於每個年齡,將年齡用作第二個int數組的索引。 在該索引的第二個數組中添加一個。

然后通過第二個數組。

如果索引處的值為零,則不執行任何操作。 如果該值大於零,則輸出:“年齡的人” << << index <<“是“ << << value_at_index

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM