Funktionen für Algorithmen mit Arrays mit Iteration (Schleifen) und Rekursion.



  • hi Leute ich brauch mal eure Hilfe

    ich soll ein Programm schreiben mit 4 Funktionen.
    Funktion 1 mittels Iteration: input(n,a)
    Funktion 2 mittels Iteration: output(n,a)
    Funktion 3 mittels Iteration: copy(a, anfang, anzahl, b, ziel)
    -(Universelle Kopierfunktion für Arrays)
    Funktion 4 rekursiv: mischen(m,a,n,b,c)
    - Es sollen die Elemente zweier aufsteigend sortierter Arrays
    a (m Elemente) und b (n Elemente)
    zusammengemischt und in einem Array c aufsteigend sortiert
    gespeichert werden.
    Beispiel: a: 1 4 5 6 9 b: 2 4 11 Ergebnis in c: 1 2 4 4 5 6 9 11.

    Wobei ich bei der Funktion 4 eure Hilfe brauche, denn ich bekomm das mit dem sortiern nicht gebacken.

    Mein Hauptmain:

    #include <iostream>

    using namespace std;

    void main()
    { const int max=20,max1=40;
    double array1[max]={}, array2[max]={},array3[max1]={};
    void input(int, double array1[]),
    output(int, double array1[]);
    double mischen(int, int, int, double array1[], double array2[],
    double array3[]);
    double copy(int,int,int,int,int,double array1[], double array2[]);
    int limit=0,limit2=0,limit3=0;
    int start,menge,ziel;
    char n='a';

    while(n!='f')//menü schleife
    {
    system("cls");//clear screen
    cout << endl << " Sie haben folgende Aktionen zur Auswahl " << endl <<
    endl;

    cout << " [ a ] Array A einlesen" << endl;
    cout << " [ b ] Array A und B anzeigen" << endl;
    cout << " [ c ] Array A auf Array B kopieren" << endl;
    cout << " [ d ] Array A und B mischen" << endl;
    cout << " [ e ] Gemischtes Array anzeigen" << endl;
    cout << " [ x ] Exit!" << endl << endl;
    cout << " >> ";
    cin >> n;

    system("cls"); //clear screen

    switch(n)// menü
    {
    case'a':
    {
    cout << endl << " Wie viele Arrays wollen sie eingeben? (max 20)"
    << endl << endl;
    cout<<"\t";
    cin >> limit;
    cout << endl << " Arrays eingeben " << endl << endl;

    input(limit,array1); // aufruf der eingabe funktion

    cout<<endl;
    system("pause");
    break;

    }

    case'b':
    {
    cout << " \n Array A: " << endl;
    cout << " \t\n" << endl;
    output(limit,array1);// aufruf der ausgabe funktion

    cout << "\n\t";
    cout << " \n Array B: " << endl;
    cout << "\t\n" << endl;
    output(limit2,array2);

    cout << endl << endl;
    system("pause");
    break;
    }

    case'c':
    {
    cout <<" Bitte geben sie den Startwert ein " << endl << endl;
    cout<<"\t";
    cin>>start;
    cout<<" \n Bitte geben sie die Menge der zu kopierenden Werte ein " << endl << endl;
    cout<<"\t";
    cin>>menge;
    cout<<" \n Bitte geben sie einen Zielwert an " << endl << endl;
    cout<<"\t";
    cin>>ziel;

    limit2=(copy(limit,limit2,start,menge,ziel,array1,array2)-1);// aufruf der kopier funktion

    cout << endl << " \n Array A auf Array B erfolgreich kopiert\n " << endl << endl;

    system("pause");
    break;
    }

    case'd':
    {
    mischen(limit,limit2,limit3,array1,array2,array3);// aufruf der misch funktion
    cout << " \n Arrays erfolgreich gemischt\n\n " << endl << endl;

    system("pause");
    break;
    }

    case'e':
    {
    cout << " \n Gemischtes Array: " << endl << endl;

    for(int i=0;i<(limit+limit2);i++)//ausgabe des dritten arrays
    {
    cout << "\t";
    cout << array3[i] << " ";
    }
    cout << endl <<endl;
    system("pause");
    break;
    }

    case'x':
    {
    exit (0);
    break;
    }

    default:
    {
    cout << "\n Uueltige Eingabe\n " << endl << endl;
    system("pause");
    break;
    }
    }
    }
    }

    Funktion 1 (input)

    #include <iostream>
    using namespace std;

    void input(int limit,double array1[])
    {

    for(int i=0; i<limit; i++)
    {
    cout<<"\t";
    cin>>array1[i];
    }
    }

    Funktion 2 (output)

    #include <iostream>
    using namespace std;
    void output(int limit, double array1[])
    {
    for(int i=0;i<limit;i++)
    {
    cout<<"\t";
    cout<<array1[i]<<" ";
    }
    }

    Funktion 3 (copy)

    #include <iostream>
    using namespace std;

    double copy(int limit,int limit2,int start,int menge,int ziel, double array1[], double array2[])
    {

    for(int i=start-1; i<(menge+start); i++)
    {
    array2[(ziel-1)]=array1[i];
    ziel++;
    }
    return ziel;
    }

    Funktion 4 (mischen)

    #include <iostream>
    using namespace std;

    double mischen(int limit,int limit2,int limit3, double array1[],double array2[],double array3[])
    {

    limit3=(limit+limit2); //anzahl der gesamten arrays ermitteln

    int a=limit-1,b=limit2-1,c=limit3-1;

    if(limit==0 && limit2==0)
    {
    return array3[0];
    }
    if(array1[a]>array2[b]) //wenn a größer als b ist wird a kopiert
    {
    array3[c]=array1[a]; //kopieren und runterzählen
    a--;
    c--;
    return mischen(limit-1,limit2,limit3-1,array1,array2,array3);
    }
    else
    {
    array3[c]=array2[b]; //kopieren und runterzählen
    b--;
    c--;
    return mischen(limit,limit2-1,limit3-1,array1,array2,array3);
    }

    }

    Danke schon mal im voraus!
    😋 😋





  • Ben schrieb:

    Funktion 4 rekursiv: mischen(m,a,n,b,c)
    - Es sollen die Elemente zweier aufsteigend sortierter Arrays
    a (m Elemente) und b (n Elemente)
    zusammengemischt und in einem Array c aufsteigend sortiert
    gespeichert werden.
    Beispiel: a: 1 4 5 6 9 b: 2 4 11 Ergebnis in c: 1 2 4 4 5 6 9 11.

    Wobei ich bei der Funktion 4 eure Hilfe brauche, denn ich bekomm das mit dem sortiern nicht gebacken.

    #include <iostream>
    using namespace std;
    
    double mischen(int limit,int limit2,int limit3, double array1[],double array2[],double array3[])
    {
    
      limit3=(limit+limit2); //anzahl der gesamten arrays ermitteln
    
      int a=limit-1,b=limit2-1,c=limit3-1;
    
      if(limit==0 && limit2==0)
      {
        return array3[0];
      }
      if(array1[a]>array2[b]) //wenn a größer als b ist wird a kopiert
      {
        array3[c]=array1[a]; //kopieren und runterzählen
        a--;
        c--;
        return mischen(limit-1,limit2,limit3-1,array1,array2,array3);
      }
      else
      {
        array3[c]=array2[b]; //kopieren und runterzählen
        b--;
        c--;
        return mischen(limit,limit2-1,limit3-1,array1,array2,array3);
      }
    }
    

    Versteh' ich nicht... Die Signaturen aus der Aufgabe und bei Dir sind unterschiedlich...Aber gut, das ist ein kosmetischer Makel - wobei wofür brauchst Du noch limit3?
    Und diese ganzen Hilfsvariablen?! Kopierst Du von hinten? Und die Kommentare stimmen doch auch nicht ("wenn a größer als b...")? Sehr verworren. 😞

    Mit ein bißchen Zeigerarithmetik läßt sich das ganze elegant lösen, so sähe mein Gerüst aus:

    void mischen(std::size_t m, const double* in1, std::size_t n, const double* in2, double* out){
      if(m==0){
        // kopiere Rest von in2
        return;
      }
      if(n==0){
        // kopiere Rest von in1
        return;
      }
      if(*in2 < *in1){
        // Wert kopieren und Rekursion mit neuem n, in2 und out
      } else {
        // Wert kopieren und Rekursion mit neuem m, in1 und out
      }
    }
    

Anmelden zum Antworten