// Programm: array-grundlagen.cpp // Beschreibung: Dieses Programm realisiert einige Grundaufgaben mit 1dim-Arrays // Autor: JL #include <iostream> // Zusatzbibliothek für Ein-und Ausgaben wird eingebunden #include <conio.h> // Zusatzbibliothek für Konsole wird eingebunden #include <stdlib.h> // Zusatzbibliothek für Zufallsgenerator #include <time.h> // Zusatzbibliothek für Initialisierung des Zufallsgenerators using namespace std; // Standard-Namensraum wird eingestellt #define MAX 10 void erstellen(int a[MAX]){ srand(time(NULL)); // Initialisierung des Zufallsgenerators for (int i=0;i<MAX;i++) { a[i]=rand()%100+1; } } void ausgabe(int a[MAX]){ for (int i=0;i<MAX;i++) if (i<(MAX-1)) cout << a[i] << " - "; else cout << a[i] << endl; } int summe(int a[MAX]){ int sum=0; for (int i=0;i<MAX;i++) sum=sum+a[i]; return sum; } bool suche(int a[MAX], int zahl){ bool gefunden=false; int i=0; do { if (zahl==a[i]) gefunden=true; i++; } while (!gefunden && i<MAX); return gefunden; } int main() {cout << "Grundaufgaben für Arrays\n\n"; char wahl; cout << "(a) Erstellen und ausgeben\n"; cout << "(b) Summe der Zufallszahlen ermitteln\n"; cout << "(c) Arithmetisches Mittel der Zufallszahlen ermitteln\n"; cout << "(d) Eine Zahl mittels Index ausgeben\n"; cout << "(e) Eine Zahl im Array suchen\n"; cout << "(f) Position einer Zahl im Array ausgeben\n"; cout << "(g) Array invertieren\n"; cout << "(h) Zwei Zahlen tauschen\n"; cout << "(i) Minimum, Maximum und Spannweite ausgeben\n"; cout << "(j) geordnetes Array ausgeben\n"; cout << endl; do { wahl=getch(); int zzahl[MAX]={0}; // Menüpunkt (a) if (wahl=='a') { erstellen(zzahl); ausgabe(zzahl); } // Menüpunkt (b) if (wahl=='b') { erstellen(zzahl); ausgabe(zzahl); cout << "Summe: " << summe(zzahl); } // Menüpunkt (c) if (wahl=='c') { erstellen(zzahl); ausgabe(zzahl); cout << "Arithmetisches Mittel: " << (float)summe(zzahl)/MAX; } // Menüpunkt (d) if (wahl=='d') { erstellen(zzahl); ausgabe(zzahl); int index; cout << "Bitte Index eingeben: "; cin >> index; cout << "Die Zahl mit Index " << "lautet " << zzahl[index]; } // Menüpunkt (e) if (wahl=='e') { erstellen(zzahl); ausgabe(zzahl); int zahl; cout << "Bitte Zahl eingeben: "; cin >> zahl; if (suche(zzahl,zahl)) cout << "Zahl ist im Array vorhanden\n"; else cout << "Zahl ist im Array nicht vorhanden\n"; } } while (!(wahl==27)); // Programm lauft solange bis ESC gedrückt wird getch(); return 0; }