/*
Projekt SortElements
Ordnen von 2 Elementen eines Feldes (array)
Version 1.0 / 2001.10.01
Autor: pstrainer@gmx.net
*/
#include
#include
#include
#include
#define Myrndi_lohi(ilo,ihi) (rand() % ((ihi)-(ilo)+1) + (ilo))
#define array_length 10
void init_array (int*);
int sort_elements(int,int,int*);
void print_array(int*);
void main (void) {
int myarray[array_length];
int i,j,ok;
printf("Projekt SortElements\nAbbruch mit i=j=0\n");
init_array(myarray); // initialize
print_array(myarray);
i=1;
while ( (i!=0) || (j!=0) ) {
printf("\nIndex i = ");
scanf("%d",&i);
printf("Index j = ");
scanf("%d",&j);
ok=sort_elements(i,j,myarray);
if(!ok) printf("Fehler: illegale Eingabe wird ignoriert\n");
else if(ok>1) printf("Elemente wurden getauscht\n");
print_array(myarray);
}
#ifdef _DEBUG
printf("\nPress any key to continue ");
_getch();
#endif
}
void init_array (int *i) {
// initialize array with random numbers lo...hi
int j;
srand(clock()); // randomize with clock
for(j=0;j Kontrolle der verwendeten Indices:
An das aufrufende Programm wird ein Code zurückgegeben:
0=Fehler, 1=ok
Bei Fehler wird das Feld nicht geändert.
> Erweiterung des return-Codes: 1=keine Änderung, 2=sort-Änderung
Version 1.0 / 2002.04.01
Autor: Peter Schindler, pstrainer@gmx.net
Syntax:
int myarray[...];
int i,j,ok;
...
ok=sort_elements(i,j,myarray);
Length:
13 lines of code
*/
int j,ok=0;
if( (a>=0) && (a=0) && (zi[z]) { // sort ?
ok++;
j=i[a];
i[a]=i[z];
i[z]=j;
}
}
return ok;
}
void print_array(int *a) {
int i;
printf("Index: ");
for (i=0;i               ( geocities.com/pstrainer/entwicklung/c)                   ( geocities.com/pstrainer/entwicklung)                   ( geocities.com/pstrainer)