Hilfe! Schreibe gerade Klausur nach...



  • Ich muss eine Berufsschul Klausur in Anwendungsentwicklung nachschreiben und sitze hier nun alleine - der Lehrer hat vergessen den Strom abzuschalten 🙂

    Bitte helft mir und ihr Mods habt ein gutes Herz sonst werde ich eine 5 oder 6 auf dem Zeugniss stehen haben.

    ---------------------------------------

    Aufgabe 1:

    Das nachstehende Programm zieht 3 Zufallszahlen aus dem Wertebereich 1 ...10.
    Die Zahl mit dem maximalen Zahlenwert wird ausgegeben.

    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    
    void main()
    {
       int a,max=0;
       int zahl[3];
       srand(time(NULL));
       for(a=0;a<3;a++)
       {
          zahl[a]=rand()%10+1;
          if(max<zahl[a])
             max=zahl[a];
       }
       cout << "Der Maximale Wert lautet: "<< max;
    }
    

    a) Wie muss das Programm geändert werden
    - zur Berechnung der kleinsten Zahl?
    - mit der Angabe der Position dieser kleinsten Zahl?
    - zur Berechnung der maximalen Zahl aus dem Wertebereich 0 ... 15?
    - um die gezogenen Zahlen von links nach rechts aufsteigend sortiert auszugeben?

    ------------------------------------------------------

    Aufgabe 2:

    In einem 2-dim. Array sollen die diagonalen Zahlen addiert und ausgegeben werden.

    Beispiel

    1 3 5
    7 9 1
    3 5 7

    *ein Strich quer durch 1, 9, 7*

    Die Summe der Diagonalen ist: 17 (also 1+9+7)

    Bitte ergänzen Sie folgendes Programm:

    [code]
    #include <iostream.h>

    void main()
    {
    int a,b,sum=0;
    int tabelle[3][3]={{1,3,5},{7,9,1},{3,5,7}};

    for(a=0;a<3;a++)
    {
    for(b=0;b<3;b++)
    {

    }
    }
    cout << "Die Summe der Diagonalen ist: " << sum;
    }

    ------------------------------------------------------

    Help please!



  • Berechnung kleinste Zahl und Position:

    void main()
    {
       int a, min=1000, pos=0;
       int zahl[3];
       srand(time(NULL));
       for(a=0;a<3;a++)
       {
          zahl[a]=rand()%10+1;
          if(min>zahl[a])
          {
             min=zahl[a];
             pos = a;
          }
       }
       cout << "Der Minimale Wert lautet: "<< min << " Position = " << pos;
    }
    

    zur Berechnung der maximalen Zahl aus dem Wertebereich 0 ... 15?

    void main()
    {
       int a,max=0;
       int zahl[3];
       srand(time(NULL));
       for(a=0;a<3;a++)
       {
          zahl[a]=rand()%16;
          if(max<zahl[a])
             max=zahl[a];
       }
       cout << "Der Maximale Wert lautet: "<< max;
    }
    

    - um die gezogenen Zahlen von links nach rechts aufsteigend sortiert auszugeben?
    ...

    Aufgabe 2

    #include <iostream.h>
    
    void main()
    {
    int a,b,sum=0;
    int tabelle[3][3]={{1,3,5},{7,9,1},{3,5,7}};
    
    for(a=0;a<3;a++)
    {
      for(b=0;b<3;b++)
      {
        if(a==b) sum+=tabelle[a][b];
      }
    }
    cout << "Die Summe der Diagonalen ist: " << sum;
    }
    


  • Minimales Element:

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    
       int zahl[3];
    
       srand(time(NULL));
    
       for( int i = 0; i < 3; ++i)
       {
          zahl[i] = rand () % 10 + 1;
       }
    
       cout << "Der Minimale Wert lautet: " << *(min_element (&zahl[0], &zahl[3]));
    }
    

    Zahl aus 0..15

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    
       int zahl[3];
    
       srand(time(NULL));
    
       for( int i = 0; i < 3; ++i)
       {
          zahl[i] = rand () % 16;
       }
    
       cout << "Der Maximale Wert lautet: " << *(max_element (&zahl[0], &zahl[3]));
    }
    

    Sortiert

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    
       int zahl[3];
    
       srand(time(NULL));
    
       for( int i = 0; i < 3; ++i)
       {
          zahl[i] = rand () % 16;
       }
    
       sort (&zahl[0], &zahl[3]);
    
       for (int i = 0; i < 3; ++i)
       {
    	   cout << "Element " << (i + 1) << ": " << zahl[i] << endl;
       }
    }
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int sum=0;
    	int tabelle[3][3]={{1,3,5},{7,9,1},{3,5,7}};
    
    	for(int i = 0, j = 0; i < 3; ++i, ++j)
    	{
    		sum += tabelle[i][j];
    	}
    
    	cout << "Die Summe der Diagonalen ist: " << sum;
    }
    

    Sehe gerade bin zu langsam gewesen 🤡

    Edit:

    In der Aufgabe verlesen *g*



  • sortieren

    void main()
    {
       int a, min=1000, pos=0, hilf;
       int zahl[3];
       srand(time(NULL));
       for(a=0;a<3;a++)
       {
          zahl[a]=rand()%10+1;
          if(min>zahl[a])
          {
             min=zahl[a];
             pos = a;
          }
       }
       cout << "Der Minimale Wert lautet: "<< min << " Position = " << pos;
    
       //Sortieren
       for(a=0;a<3;a++)
       {
          if(a>0)
          {
              if(zahl[a]>zahl[a-1])
              {
                 //Zahlen tauschen
                 hilf = zahl[a];
                 zahl[a] = zahl[a-1];
                 zahl[a-1] = hilf;
              }
          }
       }
    
    }
    

Anmelden zum Antworten