Geburtstagsparadoxon Problem



  • Hallo,
    hab ein Problem und hoffe jemand kann mir weiterhelfen.
    Ich soll ein Programm schreiben mit dem das Geburtstagsparadoxon untersucht wird.
    Nun will ich vergleichen ob es 2 gleiche Geburtstage gibt. Es soll true zurückgegeben werden falls ja. Diese Methode habe ich geschrieben. MS Visual C++ gibt keine Fehlermeldung aus. Der Code wird normal geschluckt jedoch passiert nix. Sieht vielleicht jemand auf anhieb woran es liegen könnte?
    Methode:

    bool group::birthdayOnTheSameDay()
    {
    	for(int i = 0; i <= size; i++)
    	{ 
    		for(int j = i+1; j <= size; j++)
    		{
    			if(birthdays[i].getDayInYear() == birthdays[j].getDayInYear())
    			{
    				return true;
    				cout << "\nMehrere Personen haben am gleichen Tag Geburtstag" << endl;
    				break;
    			}
    		}
    	}
    	return false;
    }
    

    Mein Main:

    int main()
    {
    	cout << " 1 Gruppe mit 200 Geburtstagen: \n";
    	group group1(200);
    	group1.sort();
    	group1.print();
    	group1.birthdayOnTheSameDay();
    

    Objekt erstellen Funktioniert, sortieren Funktioniert und die ausgabe Funktioniert. Die oben geschriebene Methode funktioniert nicht.

    Hier noch das Klassendiagramm:
    http://imageshack.us/photo/my-images/859/umluo.jpg/

    Braucht ihr noch mehr Input?



  • Tsubasa89 schrieb:

    jedoch passiert nix.

    Was heißt das? Was sollte "passieren"?



  • Wo kommt die Variable size her? Wo kommt birthdays her?


  • Mod

    Den Fehler kann ich in dem was du zeigst nicht genau benennen. Ungwöhnlich kommt mir aber z.B. vor, dass du size+1 Iterationen machst.

    Was mir aber ganz stark auffällt: Designoverkill? Ganz besonders die total überfrachtete Datumsklasse. Und in group erfindest du die Standardbibliothek neu. Zieh doch einfach N Zahlen von 1 bis 365 und guck, ob zwei gleiche dabei sind. Mit den Algorithmen aus der Standardbibliothek ist das ein Dreizeiler.

    Da:

    #include <iostream>
    #include <set>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
      cout << "Seed und Anzahl Versuchspersonen: ";
      int seed, N;
      cin >> seed >> N;
      srand(seed);
    
      // Und jetzt der eigentliche Algorithmus
      set<int> geburtstage;
      for (int i = 0; i < N; ++i)
        geburtstage.insert(rand() % 365);
      if (geburtstage.size() != N)
        cout << "Mehrere Personen haben am gleichen Tag Geburtstag.\n";
      else
        cout << "Geburtstage sind nicht entartet.\n";
    }
    

    Pack das meinetwegen noch in eine Funktion oder wenn's sein muss design es mit Klassen, aber so einfach kann's sein.



  • Vergleich mit "<=" ist natürlich falsch, da der Index nie == size sein darf.



  • Die Klassen waren so vorgegeben und sollen umgesetzt werden.
    birthdayOnTheSameDay() soll einfach zurückgeben ob es 2 gleiche Geburtstage gibt mehr nicht.

    Hier beide Klassen:
    date.h:

    #ifndef DATE_H
    #define DATE_H
    
    class Date
    {
    public:
    	Date();
    	void setRandomDate();
    	int getDayInYear();
    	int getDayInMonth();
    	int getMonth();
    	int getYear();
    	void print();
    
    private:
    	int year;
    	int dayInYear;
    };
    #endif
    

    date.cpp:

    #include "Date.h"
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <iomanip>
    using namespace std;
    
    Date::Date()
    {
    	setRandomDate();
    }
    void Date::print()
    {
    	cout << setw(2) << setfill('0') << getDayInMonth() << "." << setw(2) << setfill('0') << getMonth() << "." << year;
    }
    
    void Date::setRandomDate()
    {
    	//srand (time(0));
    	dayInYear = 1 + rand () % 365;
    	year = 1950 + rand () % 58;	
    }
    
    int Date::getDayInYear()
    {
    	return this->dayInYear;
    }
    
    int Date::getDayInMonth()
    {
    int d=getDayInYear(), i=0;
    int mo[12] = {{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};
    	do{
    		if (d<=31){return d;}
    	d = d-mo[i];
    		if (d<=mo[i])  {return d;}
    	i++;}
    
    while (i!=13 && d>mo[i-1]);	
    }
    
    int Date::getMonth()
    {
    int d=getDayInYear(), i=0;
    int mo[12] = {{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};
    	do{
    	d = d-mo[i];
    		if (d<=mo[i] && d<=0) 
    			return i+1; 
    		else
    	i++;}
    
    while (i!=13 && d>mo[i-1]);
    }
    
    int Date::getYear()
    {
    	return this->year;
    }
    

    group.h:

    #ifndef GROUP_H
    #define GROUP_H
    #include "Date.h"
    class group
    {
    public:
    	group(int);
    	int static const numberOfBirthdays = 200;
    	bool birthdayOnTheSameDay();
    	void sort();
    	void print();
    	bool isFull();
    
    private:
    	Date birthdays[numberOfBirthdays];
    	int size;
    };
    #endif
    

    group.cpp:

    #include "group.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    group::group(int gsize)
    {
    	if(gsize > 1 && gsize < numberOfBirthdays)
    		size = gsize;
    	else
    		size = numberOfBirthdays;
    }
    
    bool group::isFull()
    {
    	if (size == numberOfBirthdays)
    	return true;
    }
    
    bool group::birthdayOnTheSameDay()
    {
    	for(int i = 0; i <= size; i++)
    	{ 
    		for(int j = i+1; j <= size; j++)
    		{
    			if(birthdays[i].getDayInYear() == birthdays[j].getDayInYear())
    			{
    				return true;
    				cout << "\nMehrere Personen haben am gleichen Tag Geburtstag" << endl;
    				break;
    			}
    		}
    	}
    	return false;
    }
    
    void group::print()
    {
    	for(int i=0; i<size; i++)
    	{
    		cout << "Person " << setw(3) << setfill (' ') << i+1 << " von " << setw(3) << size <<":\t";
    		birthdays[i].print();
    		cout << endl;
    	}
    
    }
    
    void group::sort() //Bubble-Sort
    {
    	for (int i = size - 1; i > 0; i--) 
    	{
    		for (int j = 0; j < i; j++) 
    		{
    			if (birthdays[i].getYear() < birthdays[j].getYear()) 
    			{
    				Date swap = birthdays[i];
    				birthdays[i] = birthdays[j];
    				birthdays[j] = swap;
    			}
    		}
    	}	
    
    	for (int k = size - 1; k > 0; k--) 
    	{
    		for (int l = 0; l < k; l++) 
    		{
    			if (birthdays[k].getYear() == birthdays[l].getYear() && birthdays[k].getDayInYear() < birthdays[l].getDayInYear())
    			{
    				Date swap = birthdays[k];
    				birthdays[k] = birthdays[l];
    				birthdays[l] = swap;
    			}
    		}
    	}		
    }
    


  • Tsubasa89 schrieb:

    return true;
    				cout << "\nMehrere Personen haben am gleichen Tag Geburtstag" << endl;
    				break;
    

    Das cout unter return true wird niemals erreicht.



  • Q schrieb:

    Tsubasa89 schrieb:

    return true;
    				cout << "\nMehrere Personen haben am gleichen Tag Geburtstag" << endl;
    				break;
    

    Das cout unter return true wird niemals erreicht.

    Danke.
    cout und return getauscht und schon gehts.

    Bin ein Anfänger sorry 😃



  • Tsubasa89 schrieb:

    Q schrieb:

    Tsubasa89 schrieb:

    return true;
    				cout << "\nMehrere Personen haben am gleichen Tag Geburtstag" << endl;
    				break;
    

    Das cout unter return true wird niemals erreicht.

    Danke.
    cout und return getauscht und schon gehts.

    Bin ein Anfänger sorry 😃

    Das "break" kannst du auch wegmachen, das hat auch keinen Zweck mehr wenn es unter dem "return" steht.


Anmelden zum Antworten