Wahrheitstabelle ausgeben in C?
Hallo zusammen,
Das ist meine Aufgabe, die ich in der Uni lösen muss, allerdings habe ich ein Problem und es werden statt 16 Zeilen bei mir nur zwei Zeilen ausgegeben.
#include <stdio.h>
int main(void)
{
int schritte = 0;
int a, b, c, d, e, f, g, h, i;
a = 0;
b = 0;
c = 0;
d = 0;
printf(" | A | B | C | D || E | G | H | I | F |\n");
printf("-+---+---+---+---++---+---+---+---+---+-\n");
while(schritte <= 16)
{
while(a <= 1)
{
while(b <= 1)
{
while(c <= 1)
{
while(d <= 1)
{
e = a && -b;
g = c || -d;
h = -e || g;
i = a || b || d;
f = h==i;
printf(" |%d |%d | %d |%d ||%d |%d |%d |%d |\n", a, b, c, d, e, g, h, i, f);
d++;
}
c++;
}
b++;
}
a++;
}
schritte++;
}
}
als Ausgabe bekomme ich dann nur das:
| A | B | C | D || E | G | H | I | F |
-+---+---+---+---++---+---+---+---+---+-
|0 |0 | 0 |0 ||0 |0 |0 |0 |
|0 |0 | 0 |1 ||0 |1 |1 |1 |
muss da noch bisschen mit dem Abstand rumspielen aber vom Prinzip her passt es ja, nur leider nur mit den zwei Zeilen.
Danke im Vorraus!
2 Antworten
naja ganz einfach du setzt ja nix zurück, ergo ist überall nicht mehr <= 1
und schritte brauchst du gar nicht, das ergibt sich aus den kombinationen .
#include <stdio.h>
int main(void)
{
int a, b, c, d, e, f, g, h, i;
a = 0;
b = 0;
c = 0;
d = 0;
printf(" | A | B | C | D || E | G | H | I | F |\n");
printf("-+---+---+---+---++---+---+---+---+---+-\n");
while(a <= 1)
{
while(b <= 1)
{
while(c <= 1)
{
while(d <= 1)
{
e = a && -b;
g = c || -d;
h = -e || g;
i = a || b || d;
f = h==i;
printf(" | %d | %d | %d | %d | | %d | %d | %d | %d |\n", a, b, c, d, e, g, h, i, f);
d++;
}
d=0;
c++;
}
c=0;
b++;
}
b=0;
a++;
}
a=0;
}
| A | B | C | D || E | G | H | I | F |
-+---+---+---+---++---+---+---+---+---+-
| 0 | 0 | 0 | 0 | | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | | 0 | 1 | 1 | 1 |
| 0 | 0 | 1 | 0 | | 0 | 1 | 1 | 0 |
| 0 | 0 | 1 | 1 | | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 0 | | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | | 0 | 1 | 1 | 1 |
| 0 | 1 | 1 | 0 | | 0 | 1 | 1 | 1 |
| 0 | 1 | 1 | 1 | | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 | | 0 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 | | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | | 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | | 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 | | 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 |
wo siehst du da fehler ??
e = 0 && 1 = 0
g = 0 || 1 = 1
h = 1 || 0 = 1
i = 0 || 0 || 0 = 0
f = 1 == 0 = 0
setzt mal wahr und falsch ein
e = F && W = F
g = F || W = W
h = W || F = W
i = F || F || F = F
f = W == F = F
bei AND müssen beide WAHR sein um WAHR zu ergeben
bei OR muss nur einer WAHR sein um WAHR zu sein
Das liegt wohl daran, dass deine Variablen a, b, c, d nie wieder auf 0 zurückgesetzt werden. Somit werden die inneren Schleifen nach dem ersten Mal nicht mehr ausgeführt.
hast du vielleicht ne Ahnung wieso die Werter für e,g,h,i,f falsch berechnet werden?
Ich habe jetzt nochmal geändert und
trotzdem ab und zu falsches raus