PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : for-Schleife in die Bedingung der while-Schleife



The EYE
12-12-2010, 10:32
Hallo!

Ich möchte eine while Schleife erstellen, bei der in der while-Bedingung eine for-Schleife steht.
Ich habe mal ein kleines Beispielprogramm geschrieben. Mir ist bewusst, dass es sich hierbei auch um eine Endlosschleife handelt (bzw. handeln würde, sie funktioniert ja nicht).


#include<iostream>

using namespace std;

int main()
{
int array1[10][10];
int array2[10][10];

// Nullsetzen von array1
for(int i=0; i<=9; i++)
{
for(int j=0; j<=9; j++)
{
array1[i][j] = 0;
}
}

// Nullsetzen von array2
for(int i=0; i<=9; i++)
{
for(int j=0; j<=9; j++)
{
array2[i][j] = 0;
}
}


while (
for(int i=0; i<=9; i++)
{
for(int j=0; j<=9; j++)
{
array1[i][j] == array2[i][j];
}
}
)
{
cout << "Arrays sind identisch!" << endl;

return 0;
}

Das Problem ist, dass ich (wenn ich richtig informiert bin) Arrays nicht "im ganzen" miteinander vergleichen kann, sondern immer alles Schrittweise ausführen muss. Daher die for-Schleife.

Ich bin gespannt auch eure Lösungen!

Gruß Max

msi
12-12-2010, 11:30
du musst das anderst lösen! sonst verbrauchst du immer alle cpu wegen der überprüfung.
aber du möchtest es in der art so:





int identisch = 1;
while (1) {
for(int i=0; i<=9; i++) {
for(int j=0; j<=9; j++) {
if ( array1[i][j] != array2[i][j] ) {
identisch = 0; break;
}
}
if ( !identisch ) break;
}
if ( identisch )
cout << "Arrays sind identisch!" << endl;
else
break;

}




aber du solltest es so programmieren, dass bei jeder änderung der arrays eine fkt aufgerufen wird, die nur die änderungen überprüft und dann
ermittelt ob das array nun identisch ist.

The EYE
12-12-2010, 11:43
Hey!

Du meinst, wenn sich bei array1[5][4] der Wert geändert hat sollte nur dieser Bereich berücksichtigt werden?
In dem Programm, in das das ganze eingebaut wird sich das Array allerdings meistens sehr stark ändern.

Gruß Max

msi
12-12-2010, 16:26
auf jeden fall sollltest du das überprüfen der arrays irgendwo "triggern" und das nicht in einer for schleife ewig machen

rennradler
15-12-2010, 07:08
Zwei Arrays kann man auch mit der Funktion memcmp vergleichen. Das ist die effizienteste Methode, weil hier handoptimierter Assemblercode dahinter steht. Vorsichtig sein muß man sein, wenn es ein Array aus strukturieren Datentypen ist, da man nicht sicher sein kann, was in den aufgrund von Memory Alignments unbelegten Bytes für Werte stehen.

jeebee
15-12-2010, 08:00
... da man nicht sicher sein kann, was in den aufgrund von Memory Alignments unbelegten Bytes für Werte stehen.

Deshalb ist es sicher keine schlechte Idee, dass man so ein Array entweder mit calloc(n, sizeof(ArrayElement_t)) oder mit malloc/memset(..., 0, n*sizeof(ArrayElement_t)) initialisiert. Dann ist nämlich der gesamte Memory-Bereich mit 0 gefüllt und memcmp funktioniert zuverlässig.

HTH

jeebee

The EYE
20-12-2010, 14:09
Hey!

Könntet ihr das an einem kleinen Beispielprogramm deutlich machen?

Gruß Max

jeebee
20-12-2010, 14:20
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct s {
int x;
short y;
int z;
}; // memory layout: bytes 0-3: x, 4-5: y, 8-11: z

int main()
{
struct s *array = calloc(10, sizeof(struct s));
for (i = 0; i < 10; i++) {
array[i].x = i;
array[i].y = 2*i;
array[i].z = 3*i;
}
struct s *array2 = calloc(10, sizeof(struct s));
for (i = 0; i < 10; i++) {
array2[i].x = i;
array2[i].y = 2*i;
array2[i].z = 3*i;
}

if (memcmp(array, array2, 10*sizeof(struct s)) != 0) {
printf("contents of array and array2 are not equal!\n");
} else {
printf("contents of array and array2 are equal!\n");
}

array2[3].z = 0;

if (memcmp(array, array2, 10*sizeof(struct s)) != 0) {
printf("contents of array and array2 are not equal!\n");
} else {
printf("contents of array and array2 are equal!\n");
}

return 0;
}

The EYE
21-12-2010, 08:20
Hey!

Ich glaube du hattest zwei int vergessen.
Bekomme noch folgende Fehlermeldung:

error C2440: 'Initialisierung': 'void *' kann nicht in 's *' konvertiert werden
(Zeile 13 und 19)

Und hier der Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct s {
int x;
short y;
int z;
}; // memory layout: bytes 0-3: x, 4-5: y, 8-11: z

int main()
{
struct s *array = calloc(10, sizeof(struct s));
for (int i = 0; i < 10; i++) {
array[i].x = i;
array[i].y = 2*i;
array[i].z = 3*i;
}
struct s *array2 = calloc(10, sizeof(struct s));
for (int i = 0; i < 10; i++) {
array2[i].x = i;
array2[i].y = 2*i;
array2[i].z = 3*i;
}

if (memcmp(array, array2, 10*sizeof(struct s)) != 0) {
printf("contents of array and array2 are not equal!\n");
} else {
printf("contents of array and array2 are equal!\n");
}

array2[3].z = 0;

if (memcmp(array, array2, 10*sizeof(struct s)) != 0) {
printf("contents of array and array2 are not equal!\n");
} else {
printf("contents of array and array2 are equal!\n");
}

return 0;
}

Gruß Max

jeebee
22-12-2010, 00:23
Ja, die ints hab ich vergessen... Btw: für ISO C90 (selten, aber trotzdem erwähnenswert) sollte das int i vor dem ersten for und nicht im for drin stehen.

Bezgl. Fehlermeldung: Compiler, Version; Betriebssystem, Version?

edit: gcc meckert wegen dem int i im for auch ohne auf strict ansi gesetzt zu sein... damit's wirklich ISO C90 (ANSI C89) ist, müsste noch der //-Kommentar durch einen /* */-Kommentar ersetzt werden.


So kompilierts mit
gcc -Wall -Wextra -ansi -pedantic -o mcmp mcmp.c (gcc 4.4.5) ohne Warnungen:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct s {
int x;
short y;
int z;
}; /* memory layout: bytes 0-3: x, 4-5: y, 8-11: z */

int main()
{
/* all variable declarations need to be at begin of block (for -pedantic) */
struct s *array = calloc(10, sizeof(struct s));
struct s *array2 = calloc(10, sizeof(struct s));
int i;
for (i = 0; i < 10; i++) {
array[i].x = i;
array[i].y = 2*i;
array[i].z = 3*i;
}
for (i = 0; i < 10; i++) {
array2[i].x = i;
array2[i].y = 2*i;
array2[i].z = 3*i;
}

if (memcmp(array, array2, 10*sizeof(struct s)) != 0) {
printf("contents of array and array2 are not equal!\n");
} else {
printf("contents of array and array2 are equal!\n");
}

array2[3].z = 0;

if (memcmp(array, array2, 10*sizeof(struct s)) != 0) {
printf("contents of array and array2 are not equal!\n");
} else {
printf("contents of array and array2 are equal!\n");
}

return 0;
}