27.3
This commit is contained in:
parent
a2fb2ab9af
commit
27dabccef1
13
primza.c
13
primza.c
@ -4,24 +4,26 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int eingabe;
|
int eingabe;
|
||||||
int *zahlenBisN = (int *)malloc(100 * sizeof(int));
|
int *zahlenBisN = (int *)malloc(100 * sizeof(int)); // Speicher wird dynamisch für 100 Zeichen reserviert
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
printf("%s", "Bis wohin sollen die Primzahlen berechnet werden (Ende=0) ? ");
|
printf("%s", "Bis wohin sollen die Primzahlen berechnet werden (Ende=0) ? ");
|
||||||
scanf("%d", &eingabe);
|
scanf("%d", &eingabe);
|
||||||
|
|
||||||
if(eingabe > 100)
|
if(eingabe > 100) // Wenn mehr Speicher benötigt wird, dann wird mit realloc mehr speicher reserviert
|
||||||
{
|
{
|
||||||
free(zahlenBisN);
|
free(zahlenBisN); // Der Speicher von der default reservierung wird freigegeben, damit er erneut reserviert werden kann
|
||||||
zahlenBisN = (int *)realloc(zahlenBisN, eingabe * sizeof(int));
|
zahlenBisN = (int *)realloc(zahlenBisN, eingabe * sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Array wird mit zahlen von 1 bis n gefüllt
|
||||||
for (int i = 0; i < eingabe; ++i)
|
for (int i = 0; i < eingabe; ++i)
|
||||||
{
|
{
|
||||||
zahlenBisN[i] = i+1;
|
zahlenBisN[i] = i+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Jede nicht Primzahl wird entfernt
|
||||||
for (int i = 0; i < eingabe; ++i)
|
for (int i = 0; i < eingabe; ++i)
|
||||||
{
|
{
|
||||||
for (int j = 2; j < i; j++ ) {
|
for (int j = 2; j < i; j++ ) {
|
||||||
@ -32,6 +34,7 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alle zahlen die noch im String vorhanden sind und nicht 0 sind werden ausgegeben
|
||||||
for (int i = 1; i < eingabe; ++i)
|
for (int i = 1; i < eingabe; ++i)
|
||||||
{
|
{
|
||||||
if(zahlenBisN[i] != 0)
|
if(zahlenBisN[i] != 0)
|
||||||
@ -40,6 +43,6 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}while(eingabe != 0);
|
}while(eingabe != 0); // Das Programm wird so oft wiederholt bis 0 eingegeben wird
|
||||||
free(zahlenBisN);
|
free(zahlenBisN); // Speicher wird erneut freigegeben um Memory leaks zu verhindern
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user