Hilfe bei 2 aufgaben



  • Taquito schrieb:

    for (int i=0; i<100; i++)

    for(float y = 0.0; y < 100.0; y += 0.1)
    {
      for(float x = 0.0; x < 1.0; x += 0.1)
    {
        // ...
      }
    }
    

  • Mod

    Hier die Lösung von Aufgabe 1 wie dein Lehrer sie sehen will. Wer braucht schon Schleifen? Oder Berechnungen zur Laufzeit?

    #include<iostream>
    
    template<int L, int C> class columnprinter
    {
     public:
      void operator()(){
        if (L<10) std::cout<<' ';
        std::cout<<std::fixed<<L+(C*0.1)<<"  ";
        columnprinter<L,C+1>()();
      }
    };
    
    template<int L> class columnprinter<L,10>
    {
    public:
      void operator()(){
        std::cout<<'\n';
      }
    };
    
    template<int L> class lineprinter
    {
    public:
      void operator()(){
        columnprinter<L,0>()();
        lineprinter<L+1>()();
      }
    };
    
    template<> class lineprinter<100>
    {
    public:
      void operator()(){
      }
    };
    
    int main()
    {
      std::cout.precision(1);
      lineprinter<0>()();
    }
    


  • Gefällt mir 🙂



  • ist meins jetzt falsch ???? meins siehst ja etwas anders aus


  • Mod

    Taquito schrieb:

    ist meins jetzt falsch ???? meins siehst ja etwas anders aus

    Nun, macht deines das was du willst?



  • leider nein ich arbeite noch drann aber weil bei dir die befehle die kenne ich nicht deshalb habe ich nachgefragt


  • Mod

    Taquito schrieb:

    leider nein ich arbeite noch drann aber weil bei dir die befehle die kenne ich nicht deshalb habe ich nachgefragt

    Vielleicht solltest du das mit dem "wie dein Lehrer sie sehen will" nicht so 100% ernst nehmen. Es gab ja auch ein paar andere Vorschläge.



  • @SeppJ
    Mal wieder ein Danke für deinen Lösungsvorschlag.
    Gefällt mir ebenfalls sehr gut. Solche einfachen Aufgaben und Lösungen helfen meine starre Denkweise ab und an aufzubrechen (habe natürlich auch erstmal an Schleifen gedacht).

    thumbs up 😃



  • zu aufgabe 3

    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<vector>
    #include<string>
    
    using namespace std;
    
    struct Messwert
    {
    	int x;
    };
    
    int mittlerenAbstand (vector<Messwert> x)
    {
    	int sum=0;
    	int abstand;
    
    	for ( int i=0; i<100; i++)
    	{
    		int absolut;
    		absolut=abs(x[i+1].x - x[i].x);
    		sum+=absolut;
    	}
    	abstand=sum/99;
    
    	return abstand;
    
    }
    


  • http://www.youtube.com/watch?v=GTKDM8UzOhA

    Jetzt mal im Ernst. Wenn du keine Fragen stellst, kann dir auch keiner helfen. Was ist dein Problem? Wo hakt es genau? Wir können alle (noch) nicht hellsehen.



  • Hier zu Aufgabe 2 nochmal eine einfache Lösung mit einer leichten Schleife:

    #include <iostream>
    using namespace std;
    
    int main() {
    	int temp = 0;
    	cout.precision(1);
    	cout.setf(ios::fixed,ios::floatfield);
    	for(float a = 0.0; a < 99.9; a += 0.1) {
    		cout << a;
    		if(a < 10.0) cout << "  ";
    		else cout << " ";
    		temp++;
    		if(temp == 10) {cout << "\n"; temp = 0; }
    	}
    }
    

    Ich hab sie einfach genug gehalten, sodass er sie verstehen sollte 😉



  • SeppJ: Das ist ein ganz netter Anfang, aber die ganzen Funktionsaufrufe und Formatierung zur Laufzeit sind doch ziemlich ineffizient. Glücklicherweise hat Boost genau das richtige dafür: Boost.Preprocessor.

    Ich schlage Folgendes vor:

    #include <iostream>
    
    #include <boost/preprocessor/seq/elem.hpp>
    #include <boost/preprocessor/seq/for_each.hpp>
    #include <boost/preprocessor/seq/for_each_product.hpp>
    #include <boost/preprocessor/seq/to_tuple.hpp>
    #include <boost/preprocessor/repetition/repeat_from_to.hpp>
    #include <boost/preprocessor/control/if.hpp>
    #include <boost/preprocessor/comparison/equal.hpp>
    #include <boost/preprocessor/comparison/less.hpp>
    
    #define STRINGIFY_AUX(x) #x
    #define STRINGIFY(x) STRINGIFY_AUX(x)
    
    #define MAKE_SEQ_ELEMENT(z, n, text) (n)
    
    #define s_100 BOOST_PP_REPEAT_FROM_TO(0, 100, MAKE_SEQ_ELEMENT, _)
    #define s_10  BOOST_PP_REPEAT_FROM_TO(0,  10, MAKE_SEQ_ELEMENT, _)
    
    #define FORMAT_NUMBER(r, product) \
      BOOST_PP_IF(BOOST_PP_LESS(BOOST_PP_SEQ_ELEM(0, product), 10), " ", "") \
      STRINGIFY(BOOST_PP_SEQ_ELEM(0, product)) "." STRINGIFY(BOOST_PP_SEQ_ELEM(1, product)) \
      BOOST_PP_IF(BOOST_PP_EQUAL(BOOST_PP_SEQ_ELEM(1, product), 9), "\n", "  ")
    
    int main() {
      std::cout << BOOST_PP_SEQ_FOR_EACH_PRODUCT(FORMAT_NUMBER, (s_100)(s_10));
    }
    

  • Mod

    Hier noch was für Aufgabe 3: Ich habe mich dafür entschieden die unnötige Beschränkung auf 100 Elemente und die unnötigen Zwischenergebnisse wegzulassen.

    #include<iostream>
    #include<iterator>
    #include<fstream>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    
    struct abs_dist_counter
    {
    private:
      double counter;
      int N;
    public:
      abs_dist_counter():counter(0.), N(0){}
      bool operator()(double x, double y) {++N; counter+=fabs(x-y); return false;}
      double mean_value(){return counter/N;}
    };
    
    int main()
    {
      ifstream in("DRUCK.DAT");
      istream_iterator<double> begin(in), end;
      abs_dist_counter result;
      adjacent_find<istream_iterator<double>, abs_dist_counter& > (begin, end, result);
      cout << "Durchschnittlicher Abstand: " << result.mean_value() <<'\n';
    }
    

    Ich liebe es, wenn die Liste der includes so lang ist wie das eigentliche Programm. 🙂 Dummerweise habe ich keine Standardfunktion für den Abstand zweier Werte gefunden, daher der etwas längliche Funktor.

    @seldon: 👍



  • Ich finde es nicht OK, einem Anfänger Code zu geben, den er gar nicht verstehen kann. Macht Beispiel 2 einfach mit Schleifen, auch wenn es weniger effizient ist.
    Er muss den Code verstehen können!

    #include <iostream>
    #include <cmath>
    
    #define _0x98BE0 3.1415926535897932384626433832795
    
    int main()
    {
        const int* const _0x98BE1 = new int((static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) << (static_cast<int>(_0x98BE0 + 1) ^ static_cast<int>(std::sin(_0x98BE0 / (1 << 1))))) + (1 << (1 << (1 << 1))) - (1 << 1));
    
        unsigned char _0x98BE2 = ((1 << 1) >> 1 & 1 << 1 & 1 << (1 << 1)) ^ ((((1 << 1) >> static_cast<int>(std::sin(_0x98BE0 / (static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) << 1))) & 1 << 1 & 1 << (1 << 1)) * (1 << (1 << 1))) >> (1 << 1));
        _0x98BE3:
        if(_0x98BE2 < ((1 | (1 << 1) ^ (1 << 1 * static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) << 1)) + ~(static_cast<unsigned>(-static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))) - (1 << static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))) ^ 1)))
        {
            unsigned char _0x98BE4 = ((1 << static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))) >> 1 & 1 << 1 & 1 << (1 << 1)) ^ ((((1 << 1) >> 1 & 1 << 1 & 1 << (1 << 1)) * (static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) << (1 << 1))) >> (1 << 1));
            _0x98BE5:
            if(_0x98BE4 < ((1 | (1 << static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))) ^ (1 << 1 * static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) << 1)) + ~(static_cast<unsigned>(-1) - (1 << 1) ^ 1)))
            {
                unsigned char _0x98BE6 = ((1 << 1) >> static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) & 1 << 1 & 1 << (1 << 1)) ^ ((((1 << 1) >> static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) & 1 << 1 & 1 << (1 << 1)) * (1 << (1 << 1))) >> (1 << 1));
                _0x98BE7:
                if(_0x98BE6 < ((static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) | (1 << 1) ^ (1 << static_cast<int>(std::sin(_0x98BE0 / (1 << static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))))) * 1 << 1)) + ~(static_cast<unsigned>(-1) - (1 << 1) ^ 1)))
                {
                    std::cout << *const_cast<char*>("\x09") << (_0x98BE2 ? static_cast<char>(_0x98BE2 + 48) : ' ') << static_cast<int>(_0x98BE4) << *reinterpret_cast<const char* const>(_0x98BE1) << static_cast<int>(_0x98BE6);
                    if(_0x98BE6 == ((1 | (1 << static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))) ^ (static_cast<int>(std::sin(_0x98BE0 / (1 << 1))) << 1 * 1 << 1)) + ~(static_cast<unsigned>(-static_cast<int>(std::sin(_0x98BE0 / (1 << 1)))) - (1 << 1) ^ 1)) - 1)
                        std::cout << std::endl;
                    ++_0x98BE6;
                    goto _0x98BE7;
                }
                ++_0x98BE4;
                goto _0x98BE5;
            }
            ++_0x98BE2;
            goto _0x98BE3;
        }
    
        delete _0x98BE1;
    }
    

  • Mod

    Eine Lösung schöner als die andere 😃 .

    @Threadersteller: Du siehst was passiert, wenn man einfach nur Aufgaben hinschreibt ohne Fragen zu stellen. Wenn du wirklich geholfen werden willst, empfehle ich eine lösungsorientierteres Vorgehen: Schreib was du vor hast, was du warum gemacht hast, wobei und warum du nicht weiterkommst. Dann wirst du vermutlich sehr schnell Antworten bekommen die dir auch wirklich helfen anstatt fertige Lösungen die du nicht verstehst.
    Siehe auch hier:
    http://www.c-plusplus.net/forum/200753



  • Ich habe hier nicht nach einer lösung gefragt ichhabe ja selbst hier sachen gepostet mit vorschlägen

    trotzdem danke für eure Hilfe


Anmelden zum Antworten