Beispielprogramm Grundlagen und Sortierung

//Programm: Array-Grundlagen
 
#include <iostream>
#include <conio.h>
#include <stdlib.h>     //braucht man für rand()!
#include <time.h>		//braucht man für time()!
#include <algorithm>    //STL-Bibliothek 
using namespace std;
 
#define MAX 10
 
void erstellen(int a[MAX])
{
	srand(time(0));		//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;
	}
}
 
void summe(int a[MAX])	
{
	int summe;
	for (int i=0;i<MAX;i++)
	{
		summe=summe+a[i];
	}
	cout<<"Summe: "<<summe<<endl;
}
 
void mittel(int a[MAX])
{
	float mittel;
	float summe;
	for (int i=0;i<MAX;i++)
	{
		summe=summe+a[i];
	}
	mittel=summe/MAX;
	cout<<"Arithmetisches Mittel: "<<mittel<<endl;
}
 
void index(int a[MAX])
{
	int eingabe;
	cout<< "\nGeben Sie den Index der Zahl ein: ";
	cin>>eingabe;
	cout<< "\nDie Zahl mit dem Index "<<eingabe<<" lautet "<<a[eingabe-1];
	cout<< endl;
}
 
void vorhanden(int a[MAX])
{
	int zahlabfrage;
	int y=0;
	cout<< "Geben Sie eine Zahl ein, diese wird überprüft: ";
	cin>> zahlabfrage;
	for(int i=0;i<MAX;i++)
	{
		if(zahlabfrage==a[i])
		{
			cout<<"Die Zahl "<< zahlabfrage<< " kommt bei den Zufallszahlen vor!";
			y++;
		}
 
 
	}
	if (y=0) cout<<"Die Zahl "<< zahlabfrage<< " kommt nicht bei den Zufallszahlen vor!";
	cout<<endl;
}
 
void vorhandenpos (int a[MAX])
{
	int zahlabfrage;
	cout<< "Geben Sie eine Zahl ein, diese wird überprüft: ";
	cin>> zahlabfrage;
	for(int i=0;i<MAX;i++)
	{
		if(zahlabfrage==a[i])
		{
			cout<<"Die Zahl "<< zahlabfrage<< " kommt bei den Zufallszahlen an der "<<i+1<<". Stelle vor!";
		}
	}
	cout<<endl;
}
 
void invertieren  (int a[MAX])
{
	int invert[MAX];
	for(int i=0;i<MAX;i++)
	{
		invert[i]=a[9-i];
 
		if (i<(MAX-1))cout<< invert[i] << " - ";
		else cout <<invert[i]<<endl;
 
	}
 
}
void tausch (int a[MAX])
{
	int c,b,n;
	cout<<endl<<"Geben Sie zwei Zahlen ein, die Sie tauschen wollen!\n";
	cout<<"1.Zahl-Index: ";
	cin>>c;
	cout<<endl;
	cout<<"2.Zahl-Index: ";
	cin>>b;
	cout<<endl;
	int dif;
	dif=c-b;
 
	for (int i=0;i<MAX;i++)
	{
		if(i==c-1)
		{
			n=a[i];
			a[i]=a[c-dif-1];	
		}
		if(i==c-dif-1)
		{
			a[i]=n;
		}
	}
	for (int i=0;i<MAX;i++)
	{
		if (i<(MAX-1))cout<< a[i] << " - ";
		else cout <<a[i]<<endl;
	}
}
 
void minmaxsp  (int a[MAX])	
{
	int min,max,sp;
	for	(int i=0;i<MAX;i++)
	{
		if(i==0)
		{
			min=a[i];
		}
		else
		{
			if(a[i]<min)
			{
				min=a[i];
			}
		}
	}
	for	(int i=0;i<MAX;i++)
	{
		if(i==0)
		{
			max=a[i];
		}
		else
		{
			if(a[i]>min)
			{
				max=a[i];
			}
		}
	}
	sp=max-min;
	cout<<"Maximum: "<<max<<endl;
	cout<<"Minimum: "<<min<<endl;
	cout<<"Spannweite: "<<sp<<endl;
 
}
 
void bubblesort (int a[MAX])
{
	int m;
	int q;
	for (int i=0; i<MAX;i++)
	{
		for(int n=0; n<MAX-i; n++)
		{
			if(a[n]>a[n+1])
			{
				m=a[n];
				q=a[n+1];
				a[n]=q;
				a[n+1]=m;
			}
		}
	}
 
	for (int i=0;i<MAX;i++)
	{
		if (i<(MAX-1))cout<< a[i] << " - ";
		else cout <<a[i]<<endl;
	}
}
void selectsort	(int a[MAX])
{
	int min,f ;
	for (int m=0;m<MAX;m++)
	{
		for	(int i=m;i<MAX;i++)
		{
			if(i==m)
			{
				min=a[m];
			}
			else
			{
				if(a[i]<min)
				{
					min=a[i];
				}
			}
 
		}
		for (int j=m; j<MAX; j++)
		{
			if(a[j]==min)
			{
				f=a[m];
				a[j]=f;
				a[m]=min;	
			}
 
		}
 
 
	}
 
 
}
 
void insertsort (int a[MAX])
{
	int x,s;
	for (int n=1;n<MAX;n++)
	{	
		x=a[n];
		s=0;
		while(a[s]<x)
		{
			s++;
		}			
		for (int t=n; t>=s+1; t--)
		{
			a[t]=a[t-1];
		}
		a[s]=x;
	}
 
 
}
 
 
 
int main()	
{
	char wahl;
 	cout<<"Grundaufgaben fuer Arrays\n\n";
 	cout<<"(a) Erstellen und ausgeben\n"; 
	cout<<"(b) Summe der Zufallszahlen\n";
	cout<<"(c) Arithmetisches Mittel der Zufallszahlen\n";
	cout<<"(d) Indexabfrage\n";
	cout<<"(e) Zahl eingeben mit Überprüfung ob diese vorkommt\n";
	cout<<"(f) Position einer Zahl im Array vorkommt\n";
	cout<<"(g) Array invertieren\n";
	cout<<"(h) Zwei Zahlen tauschen\n";
	cout<<"(i) Minimum, Maximum, Spannweite ermitteln\n";
	cout<<"(j) Sortieren - Bubblesort\n";
	cout<<"(k) Sortieren - SelectionSort\n";
	cout<<"(l) Sortieren - InsertSort\n";
	cout<<"(m) Sortieren - Standard Template Library\n";
	cout<<endl;
	do
	{
		wahl=getch();
		int zzahl[MAX]={0};
		if (wahl=='a')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
		}
		if (wahl=='b')
		{	
			erstellen(zzahl);
			ausgabe(zzahl);		
			summe(zzahl);
		}
		if (wahl=='c')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			mittel(zzahl);
		}
		if (wahl=='d')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			index(zzahl);
		}
		if (wahl=='e')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			vorhanden(zzahl);
		}
		if (wahl=='f')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			vorhandenpos(zzahl);
		}
		if (wahl=='g')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			invertieren(zzahl);
		}
		if (wahl=='h')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			tausch(zzahl);
		}
		if (wahl=='i')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			minmaxsp(zzahl);
		}
		if (wahl=='j')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			bubblesort(zzahl);
			cout<<endl;
		}
		if (wahl=='k')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			selectsort(zzahl);
			ausgabe(zzahl);
			cout<<endl;
 
		}
	   if (wahl=='l')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			insertsort(zzahl);
			ausgabe(zzahl);
			cout<<endl;
		}
 
	    if (wahl=='m')
		{
			erstellen(zzahl);
			ausgabe(zzahl);	
			sort(zzahl,zzahl+MAX);
			ausgabe(zzahl);
			cout<<endl;
		}
 
 
 
 
	}while(wahl!=27); //solange bis Escape Taste
 
 
 
 	getch ();
 	return 0;
}