rand srand problemm (schnelle antwort gesucht gestern were besser)
-
//biblio8hkes copiler #include <stdio.h> //Statrt c #include <stdlib.h> //gia thn rand kai srand kirios #include <time.h> //gia thn time //Biblio8hkes dikias mas kataskeuhs //#include "Anazitish.h" //biblio8hkh sunarthsewn anazhthseis //#include "Taxinomish.h" //Dhlosh sunarthsewn pou xriazonte gia thn main void PrintOutMessege1(void); int ArraySize(void); void RandomArray(int *pArray, int CopySize); void Question(int *Array,int Size); void PrintOutMatrix(int *pArray,int CopySize); ///////////////////////////////////////////////////////////////////////////////////////////////// //Kuria sunarthsh// ///////////////////////////////////////////////////////////////////////////////////////////////// int main(void) { //dhlosh metablhton int *Array; int Size; // Array=NULL; Size=0; //Kurio Programma //Dhmiourgia enos pinaka kai pi8anon ektiposh tou PrintOutMessege1();//Ektiponh Minima Size=ArraySize(); Array=malloc(Size*(sizeof(int)));//kataskeuh tou pinaka me mege8os pou epileksate RandomArray(Array,Size); Question(Array,Size); //Telos Dimiourgia Pinaka // //Algori8mh pou taxinomoun ton pinaka kai kanoun ebresh tou stoixiou return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////Sinarthseis\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ////////////////////////////////////////////////////////////////////////////////////////////////// /* ************************************************************************************************** *Ektiponh ena minima gia ton xristh * ************************************************************************************************** */ void PrintOutMessege1(void) { //kurio Programma printf("Plhktrologise to mege8os tou pinaka pou 8eleis gia.\n"); printf("16(a), 64(b), 256(c), 512(d), 1024(e), kai gia allo mege8os (f):\n"); } /* ************************************************************************************************** *Skopos hsagogh tou mege8os enos pinaka * * * *Epistrefi: to megefos tou pinaka(int) * ************************************************************************************************** */ int ArraySize(void) { //Dhlosh sunarthsewn char Choise; int LocalSize; int WhileEnd; // Choise=0; LocalSize=0; WhileEnd=0; //kurio programma //elenxos eisagogh do{ WhileEnd=0; //gia na bgei apo to Loop. scanf("%c",&Choise); //eisagh ena gramma getchar(); //gia ton xarakthra pou einai sto reuma eisodou //epiloges switch(Choise) { case 'a': { LocalSize=16; break; } case 'b': { LocalSize=64; break; } case 'c': { LocalSize=256; break; } case 'd': { LocalSize=512; break; } case 'e': { LocalSize=1024; break; } case 'f': { printf("Pliktologise thn epilogi sou:\n"); scanf("%d",&LocalSize); break; } default: { PrintOutMessege1(); printf("Pliktologises la8os, ksanaprospa8a.\n"); WhileEnd=1; } } }while(WhileEnd==1); return LocalSize; } /* ************************************************************************************************** * Gemizh ton pinaka mas me mia akoulou8ia ari8mon tixeon, h me mia entelos tixea akoulou8ia * * pou einai sunarthsh tou xronou. Analoga me thn epilog man. * ************************************************************************************************** */ void RandomArray(int *pArray, int CopySize) { //Dhlosh Metablhton int ChoiceRandom; int i=0; // ChoiceRandom=0; i=0; //kurio Programma do{ printf("Plhktrologise thn methodo pou 8eleis na gemiseis ton pinaka sou:\n"); printf("(0)Thxea h (1)Entelos thxea me sunarthsh ton xrono?.\n"); scanf("%d",&ChoiceRandom); }while((ChoiceRandom==1)||(ChoiceRandom==0)); if(ChoiceRandom==0) { srand(); } else { srand(time(NULL)); } //Apo8hkeush tixeon stoixion se pinaka for(i=0;i<CopySize;i++) { pArray[i]=rand(); } PrintOutMatrix(pArray, CopySize); } /* ************************************************************************************************* *Ektiponi ton an epilexth me Nai ton pinaka * ************************************************************************************************* */ void Question(int *pArray,int CopySize) { //dhlsosh Matablhton char Question; // Question=0; //Kurio programma do{ printf("8eleis na ektipo8h o pinakas sou(Y/N)???\n"); scanf("%c", &Question); getchar(); //perni to stoixio pou einai sto reuma eisodou }while((Question=='y')||(Question=='Y')||(Question=='n')||(Question=='N')); //Epilegi sumfona me thn isagogh if((Question=='y')||(Question=='Y')) { PrintOutMatrix(pArray, CopySize); } } ////////////////////////////////////////////////////////////////////////////////////////////////// //Sinarthsh ektiposhs tou pinaka // ////////////////////////////////////////////////////////////////////////////////////////////////// void PrintOutMatrix(int pArray,int CopySize) { //Dhlosh Metablhton int i; int BackSpace; // i=0; BackSpace; //Kurio Programma for(i=0;i<6;i++) { BackSpace=BackSpace+1; if(BackSpace<7)//Ektiponi ena keno ana 7 stoixia { printf("\n"); BackSpace=0; } printf("%6d",pArray[i]);//gia na dixnh ston xrhsth anri gia 1 iso me } }
-
hallo noch mal srand() ist gleich mit srand(1) laut theori. srand kreirt zufelich eine reihe von zahlen. Aber da rand mit parameter 1 die reihe kreihr ist das nicht zufehlich. Deshalb wird beim jeden programmstart die gleiche reihe kreihrt. Die lössung ist rand mit srand(time(NULL)) zu starten. Um eine ganz zufelige reihe zu machen. wie funktioniert srand(time(NULL)). Ganz einfach der computer hat ein zeit chip daruf ist ein datum gescpeicher ein tag in den 60 jahern(die wissenschaftler haben die genaue zeit am den tag berechnet). Nun macht der computer folgentes um die zeit zu bestimmen. Er nimmt den genauen tag und macht tag+(vergagene secuden)=Aktuelle zeit. time.h holt diese vergangenen secunden und tut sie in srand. Also wen von 1960 123456789 secunden vergangen sind. Wird srand so initialisiert srand(123456789).
Problemm wie kann ich in einen programm 2 srand benutzen.
Ich habe mir volgendes nachgedacht aber kann es nciht programmieren
if(a==0)
{
b=time(Null)
}
else
{
b=0
}srand(b);
ich habe s genau so geschireben aber der compiler amcht fehler wiso??
-
Master User schrieb:
kann ich srand 2 mal benutzen???
Warum nicht? Ist nur die Frage ob das Sinn macht.
Master User schrieb:
time(0) ist gleich mit time(NULL) stimmt doch und auf die zweite frage richtig oder???
Im Allgemeinen ist folgendes definiert:
#define NULL (void*)0
Das sollte deine Frage beantworten.
Btw.: Hast du dir das Ziel gesetzt dieses Forum zu füllen?
-
Compilier mal den folgenden code neue version das wird dich dann überaschen wie kann ich dieses problemm lössen. P.S. kannst du auch mal kurz die zeiger anschauen ob die richtik sind, auch ja die 9Kb ist doch nicht viel
/* ************************************************************************************************** * Onoma : Xristos Mazarakhs * * A.M. : 018200200179 * * * * Skopos : Kani anazitish kai taxinomisi se enan pinaka me thn boh8eia ton duo bhblio8hkon * * taxinomisi.h kai anazitisi.h * * * ************************************************************************************************** */ //biblio8hkes copiler #include <stdio.h> //Statrt c #include <stdlib.h> //gia thn rand kai srand kirios #include <time.h> //gia thn time //Biblio8hkes dikias mas kataskeuhs //#include "Anazitish.h" //biblio8hkh sunarthsewn anazhthseis //#include "Taxinomish.h" //Dhlosh sunarthsewn pou xriazonte gia thn main void PrintOutMessege1(void); int ArraySize(void); void RandomArray(int *pArray, int CopySize); void Question(int *pArray,int Size); void PrintOutMatrix(int *pArray,int CopySize); int Sort(int *Array,int Size); int SearchedNumber(); ///////////////////////////////////////////////////////////////////////////////////////////////// //Kuria sunarthsh// ///////////////////////////////////////////////////////////////////////////////////////////////// int main(void) { //dhlosh metablhton int *Array; int Size; int SortTyp; int Number; // Array=NULL; Size=0; SortTyp=0; Number=0; //Kurio Programma //Dhmiourgia enos pinaka kai pi8anon ektiposh tou PrintOutMessege1();//Ektiponh Minima Size=ArraySize(); Array=malloc(Size*(sizeof(int)));//kataskeuh tou pinaka me mege8os pou epileksate RandomArray(Array,Size); Question(Array,Size); //Telos Dimiourgia Pinaka // //Algori8mh pou taxinomounish SortTyp=Sort(Array, Size); //Telos Taxinomisis // //ebresh stoixiou Number=SearchedNumber(); //Search(); //Telo ebreseis stoixiou // //Ektiposh stoixiou //Telos Ektiposhs Stoixiou an ne h oxi // //Ektiposh //Apodesmeush dikton free(Array); Array=NULL; // return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////Sinarthseis\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ////////////////////////////////////////////////////////////////////////////////////////////////// /* ************************************************************************************************** *Ektiponh ena minima gia ton xristh * ************************************************************************************************** */ void PrintOutMessege1(void) { //kurio Programma printf("Plhktrologise to mege8os tou pinaka pou 8eleis gia.\n"); printf("16(a), 64(b), 256(c), 512(d), 1024(e), kai gia allo mege8os (f):\n"); } /* ************************************************************************************************** *Skopos hsagogh tou mege8os enos pinaka * * * *Epistrefi: to megefos tou pinaka(int) * ************************************************************************************************** */ int ArraySize(void) { //Dhlosh sunarthsewn char Choise; int LocalSize; int WhileEnd; // Choise=0; LocalSize=0; WhileEnd=0; //kurio programma //elenxos eisagogh do{ WhileEnd=0; //gia na bgei apo to Loop. scanf("%c",&Choise); //eisagh ena gramma getchar(); //gia ton xarakthra pou einai sto reuma eisodou //epiloges switch(Choise) { case 'a': { LocalSize=16; break; } case 'b': { LocalSize=64; break; } case 'c': { LocalSize=256; break; } case 'd': { LocalSize=512; break; } case 'e': { LocalSize=1024; break; } case 'f': { printf("Pliktologise thn epilogi sou:\n"); scanf("%d",&LocalSize); break; } default: { PrintOutMessege1(); printf("Pliktologises la8os, ksanaprospa8a.\n"); WhileEnd=1; } } }while(WhileEnd==1); return LocalSize; } /* ************************************************************************************************** * Gemizh ton pinaka mas me mia akoulou8ia ari8mon tixeon, h me mia entelos tixea akoulou8ia * * pou einai sunarthsh tou xronou. Analoga me thn epilog man. * ************************************************************************************************** */ void RandomArray(int *pArray, int CopySize) { //Dhlosh Metablhton int ChoiceRandom; int i=0; // ChoiceRandom=0; i=0; //kurio Programma do{ printf("Plhktrologise thn methodo pou 8eleis na gemiseis ton pinaka sou:\n"); printf("(0)Thxea h (1)Entelos thxea me sunarthsh ton xrono?.\n"); scanf("%d",&ChoiceRandom); }while((ChoiceRandom==1)||(ChoiceRandom==0)); if(ChoiceRandom==0) { srand(); } else { srand(time(NULL)); } //Apo8hkeush tixeon stoixion se pinaka for(i=0; i<CopySize; i++) { pArray[i]=rand(); } PrintOutMatrix(pArray, CopySize); } /* ************************************************************************************************* *Ektiponi ton an epilexth me Nai ton pinaka * ************************************************************************************************* */ void Question(int *pArray,int CopySize) { //dhlsosh Matablhton char Question; // Question=0; //Kurio programma do{ printf("8eleis na ektipo8h o pinakas sou(Y/N)???\n"); scanf("%c", &Question); getchar(); //perni to stoixio pou einai sto reuma eisodou }while((Question=='y')||(Question=='Y')||(Question=='n')||(Question=='N')); //Epilegi sumfona me thn isagogh if((Question=='y')||(Question=='Y')) { PrintOutMatrix(pArray, CopySize); } } ////////////////////////////////////////////////////////////////////////////////////////////////// //Sinarthsh ektiposhs tou pinaka // ////////////////////////////////////////////////////////////////////////////////////////////////// void PrintOutMatrix(int *pArray,int CopySize) { //Dhlosh Metablhton int i; int BackSpace; // i=0; BackSpace; //Kurio Programma for(i=0;i<6;i++) { BackSpace=BackSpace+1; if(BackSpace<7)//Ektiponi ena keno ana 7 stoixia { printf("\n"); BackSpace=0; } printf("%6d",pArray[i]);//eksi psifia afou int 5 psifios einai (ena keno toulaxiston se 2) } } /* ************************************************************************************************** * Epilegi thn Methodo me thn opia to programma kani taxinomisi ston pinaka * * * * Eisodos : ton pinaka kai to megethos tou pinaka * * * ************************************************************************************************** */ int Sort(int *Array,int Size) { //dilsoh sunartsewn int Question; // Question=0; do{ printf("Epelekse thn methodo me thn opia theleis na taxinomiseis ton pinaka sou:\n"); printf("(1) Mh taxinomimenos pinakas \n"); printf("(2) Me bouble sort\n"); printf("(3) Grigorh taxinomish.\n"); printf("(4) Sunxoneush taxinomish\n"); scanf("%d",&Question); getchar();//gia na paro to symbolo apo to reuma eksodo }while((Question==1)||(Question==2)||(Question==3)||Question==4)); switch(Question) { case 1: { //Den taxinomite o pinakas break; } case 2: { //boubleSort break; } case 3: { //grigori taxinomish break; } case 4: { //sunxoneush break; } default: { printf("!!!FATAL ERROR!!!, epikinonise me ton programtisti sou.\n"); } } return Question; } /* ************************************************************************************************** *Sunartiseis ebresis stoixiou me diaforous tropous * * * ************************************************************************************************** */ int SearchedNumber() { //dhlsoh sunarthsewn int TempNumber; // TempNumber=0; //kurio Programma printf("Plhktrologise ton ari8mo pou 8eleis na broume mesa ston Pinaka mas:\n"); scanf("%d",& TempNumber); return TempNumber; }
-
Wie schon gesagt wurde: Es ist nicht sinnvoll, srand mehr als einmal aufzurufen. Setz an den Anfang von main() den Aufruf srand(time(0)) und dann ruf srand nie wieder auf.
-
okey das habe ich vertsanden aber ich MUSS srand MEHRMALS benutzen.
-
Und WARUM?
Einmal zu seeden reicht doch vollkommen aus... Wenn Du srand(time(0)) einmal direkt als erstes innerhalb von main aufrufst, dann ist er innerhalb sämtlicher Funktionen ebenfalls initialisiert. Und er wird auch jedesmal andere Werte ausspucken wenn Du rand() aufrufst.
-
Warum mußt Du srand mehrmals aufrufen? Erzähl' mal.
MaSTaH schrieb:
Master User schrieb:
time(0) ist gleich mit time(NULL) stimmt doch und auf die zweite frage richtig oder???
Im Allgemeinen ist folgendes definiert:
#define NULL (void*)0
Das sollte deine Frage beantworten.
Nein (wenigstens verwirrt es), NULL ist ein Makro, das zu einer Nullzeigerkonstanten expandiert. Im Zeigerkontext (wie oben) ist 0 auch eine Nullzeigerkonstante, genau wie ((void*)0l) oder 0x00.
-
dumme frage wenn ich stdlib.h includiere dan ist doch 0 gleich NULL. stimmt doch in etwa
-
Master User schrieb:
dumme frage wenn ich stdlib.h includiere dan ist doch 0 gleich NULL. stimmt doch in etwa
Nein. Zumindest darfst Du nicht davon ausgehen, dass das immer der Fall ist.