if Statments funktionieren nicht, wie gewünscht
-
Hallo zusammen,
Ich hab ein großes Problem und zwar: Ich muss in diesem Programm in einem 2-dimensionalen Array die "Nachbarn" zählen (also 1 oder nicht 1)... Zum Testen hab ich die Position [1] [1] genommen... Aber er will nicht den 1er bei [2] [1] zählen... aber beim 2. Beispiel zählt er den 1er bei [3] [1]... Ich komm auf den Fehler nicht drauf? Könnt ihr mir helfen?
Bei diesem Beispiel gibt er 7 als Nachbarn aus:
#include <stdio.h> #include <stdlib.h> #define MAX 8 int CountNeighbours (int,int,char [MAX] [MAX]); int main () { int i; int i2; int Nachbarn=0; char GOL1 [MAX] [MAX] = { {1,1,1,0,0,0,0,0}, {1,0,1,0,0,0,0,0}, {1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, }; char GOL2 [MAX] [MAX]; //Ausgabe des Start-Arrays for (i=0;i<8;i++) { for (i2=0;i2<8;i2++) { printf("%d ",GOL1[i][i2]); } printf("\n"); } Nachbarn = CountNeighbours (1,1,GOL1); printf ("Nachbarn: %d",Nachbarn); } int CountNeighbours (int x,int y,char GOL1 [MAX] [MAX]) { int Nachbarn=0; int Nachbarn2=0; (GOL1 [x-1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+2] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [2] [2] == 1) ? Nachbarn++ : Nachbarn2++; return Nachbarn; }
Bei diesem Beispiel gibt er 8 als Nachbarn aus:
#include <stdio.h> #include <stdlib.h> #define MAX 8 int CountNeighbours (int,int,char [MAX] [MAX]); int main () { int i; int i2; int Nachbarn=0; char GOL1 [MAX] [MAX] = { {1,1,1,0,0,0,0,0}, {1,0,1,0,0,0,0,0}, {1,0,1,0,0,0,0,0}, {0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, }; char GOL2 [MAX] [MAX]; //Ausgabe des Start-Arrays for (i=0;i<8;i++) { for (i2=0;i2<8;i2++) { printf("%d ",GOL1[i][i2]); } printf("\n"); } Nachbarn = CountNeighbours (1,1,GOL1); printf ("Nachbarn: %d",Nachbarn); } int CountNeighbours (int x,int y,char GOL1 [MAX] [MAX]) { int Nachbarn=0; int Nachbarn2=0; (GOL1 [x-1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+2] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [2] [2] == 1) ? Nachbarn++ : Nachbarn2++; return Nachbarn; }
LG
Navy_Seal209
-
Navy_Seal209 schrieb:
(GOL1 [x-1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+2] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [2] [2] == 1) ? Nachbarn++ : Nachbarn2++;
Selten so schönen Code gesehen. Hier noch zwei Vorschläge.
(GOL1 [x-1] [y-1] == 1) || Nachbarn++; (GOL1 [x] [y-1] == 1) || Nachbarn++; (GOL1 [x+1] [y-1] == 1) || Nachbarn++; (GOL1 [x-1] [y] == 1) || Nachbarn++; (GOL1 [x+2] [y] == 1) || Nachbarn++; (GOL1 [x-1] [y+1] == 1) || Nachbarn++; (GOL1 [x+1] [y+1] == 1) || Nachbarn++; (GOL1 [2] [2] == 1) || Nachbarn++; Nachbarn2 = 8 - Nachbarn;
Nachbarn2 = 010 - ( Nachbarn = (GOL1 [x-1] [y-1] == 1) + (GOL1 [x] [y-1] == 1) + (GOL1 [x+1] [y-1] == 1) + (GOL1 [x-1] [y] == 1) + (GOL1 [x+2] [y] == 1) + (GOL1 [x-1] [y+1] == 1) + (GOL1 [x+1] [y+1] == 1) + (GOL1 [2] [2] == 1));
-
Navy_Seal209 schrieb:
Bei diesem Beispiel gibt er 7 als Nachbarn aus:
#include <stdio.h> #include <stdlib.h> #define MAX 8 int CountNeighbours (int,int,char [MAX] [MAX]); int main () { int i; int i2; int Nachbarn=0; char GOL1 [MAX] [MAX] = { {1,1,1,0,0,0,0,0}, {1,0,1,0,0,0,0,0}, {1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, }; char GOL2 [MAX] [MAX]; //Ausgabe des Start-Arrays for (i=0;i<8;i++) { for (i2=0;i2<8;i2++) { printf("%d ",GOL1[i][i2]); } printf("\n"); } Nachbarn = CountNeighbours (1,1,GOL1); printf ("Nachbarn: %d",Nachbarn); } int CountNeighbours (int x,int y,char GOL1 [MAX] [MAX]) { int Nachbarn=0; int Nachbarn2=0; (GOL1 [x-1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y-1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+2] [y] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x-1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; (GOL1 [x+1] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; //(GOL1 [2] [2] == 1) ? Nachbarn++ : Nachbarn2++; FALSCH (GOL1 [x] [y+1] == 1) ? Nachbarn++ : Nachbarn2++; //RICHTIG return Nachbarn; }
-
warum x+2?
(GOL1 [x+2] [y] == 1) ? Nachbarn++ : Nachbarn2++;
-
Außerdem beachtet die Funktion CountNeighbours die Grenzen des Arrays nicht, d.h. es funktioniert bisher bei (x, y) nur für die inneren Felder und nicht für die Randfelder.
-
bugtracker2 schrieb:
warum x+2?
(GOL1 [x+2] [y] == 1) ? Nachbarn++ : Nachbarn2++;
genius... das war der Fehler ^^ vielen vielen dank
Trotzdem danke für alle für ihre lösungsvorschläge
LG
Navy
-
Th69 schrieb:
Außerdem beachtet die Funktion CountNeighbours die Grenzen des Arrays nicht, d.h. es funktioniert bisher bei (x, y) nur für die inneren Felder und nicht für die Randfelder.
Ja das stimmt... Aber vorher wäre noch schön, wenn es so funktionieren würde... (was jetzt der fall ist)
-
So etwas geht besser mit Zeigern statt Indizes.
-
berniebutt schrieb:
So etwas geht besser mit Zeigern statt Indizes.
Glaube ich nicht.