Mam banalny dla doświadczonych programistów problem. W następującym programie:
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
map<int,int> CheckOccurrence (int X[], int Size, int Key)
{
map<int,int> RetMap;
int Cntr = 1;
for(int i = 0; i < Size; ++i)
if( X[i] == Key )
RetMap[Cntr++] = i;
RetMap[0] = Cntr-1;
return RetMap;
}
int main()
{
int TestArray[180] = {
3,10,11,14,22,23,32,35,43, 44,6,9,16,19,24,29,37,38,45,48, 2,5,13,18,25,30,31,40,41,0,
2,5,16,19,25,30,34,39,45,48, 1,9,12,13,21,24,33,36,44,49, 3,8,15,20,23,26,35,40,43,46,
6,9,13,20,28,29,33,40,43,48, 2,7,16,19,24,27,31,34,44,47, 5,8,14,17,21,30,32,37,41,46,
4,9,14,19,22,29,35,38,46,47, 5,8,13,16,21,26,33,40,41,48, 6,7,15,17,28,30,32,39,42,45,
6,9,15,20,23,24,37,38,42,45, 7,8,16,19,22,25,34,39,43,46, 1,2,17,18,26,29,31,40,47,0,
1,4,13,18,27,30,33,38,45,46, 2,3,12,15,22,23,39,40,44,47, 8,9,11,17,24,25,32,37,49,0};
int TestKey;
map<int,int> Occurrences;
for(TestKey = 1; TestKey < 50; ++TestKey)
{
Occurrences = CheckOccurrence(TestArray,180,TestKey);
if( Occurrences[0])
{
cout << TestKey << " occurs " << Occurrences[0] << endl;
for(int i = 1; i < Occurrences[0]+1; ++i);
while(Occurrences[0]>=6) // gdy usunie się petle "while" wyswietlane sa wystapienia
{ cout <<TestKey << " occurs " << Occurrences[0] << endl;}//wszystkich liczb;
// z ta petla wyswietla sie tylko wystapienie pierwszej liczby wystepujacej 6 razy
//a mianowicie liczby 9, a ponadto warunek ten spełnia jeszcze liczba 40, co zrobić?
}
else
cout << TestKey << " does not occur in the array"<< endl;
cout << endl;
}
system("PAUSE");
return 0;
}
który zlicza ilość wystąpień liczb w tablicy nie interesują mnie wszystkie wartości. Interesuje mnie tylko liczba wystąpień >= 6. Dodałem pętlę "while", która wyswietla tylko liczby wystepujące 6 lub wiecej razy. Teraz program wyłapuje tylko pierwszą z tych liczb tzn. 9, a jest jeszcze liczba 40, ktora też spełnia to kryterium. Co mam zrobić? Może zamiast "while" zastosować "do... while"?</cpp>