2016-11-09 2 views
1

Ich versuche den Wert einer bestimmten Position (i, j) in 3 verschiedenen Matrizen zu ändern (multiplizieren mit einem int), abhängig von einer Bedingung. Allerdings, wenn ich die ganze Matrizen drucken scheint es, dass es die erste von allen Ints ist Multiplikation (auch die, die für den zweiten und dritten MatrizenMatrizen überschreiben sich gegenseitig

Hier habe ich die globalen Matrizen und Attribute einschließlich einiger Speicherzuweisung deklarieren.

int HEIGHT; 
int WIDTH; 
int ** matrixR= new int*[HEIGHT]; 
int ** matrixG= new int*[HEIGHT]; 
int ** matrixB= new int*[HEIGHT]; 

Dann in einer Funktion, die ich die Matrizen vervollständigen:

for(i=0; i<HEIGHT; i++){ 
     matrixR[i]= new int[WIDTH]; 
     matrixG[i]= new int[WIDTH]; 
     matrixB[i]= new int[WIDTH]; 
} 

Hier sind die Bedingungen, die ich für jede Matrix bewerten:

for (int i = 0; i < HEIGHT; ++i) { 
    for (int j = 0; j < WIDTH; ++j) { 
     float suma = pow(i - centerY, 2) + pow(j - centerX, 2); 
     if (suma > pow(radius, 2)) { 
      matrixR[i][j] = matrixR[i][j] * 1; 
     } 
    } 

} 

for (int i = 0; i < HEIGHT; ++i) { 
    for (int j = 0; j < WIDTH; ++j) { 
     float suma = pow(i - centerY, 2) + pow(j - centerX, 2); 
     if (suma > pow(radius, 2)) { 
      matrixG[i][j] = matrixG[i][j] * 2; 
     } 
    } 
} 

for (int i = 0; i < HEIGHT; ++i) { 
    for (int j = 0; j < WIDTH; ++j) { 
     float suma = pow(i - centerY, 2) + pow(j - centerX, 2); 
     if (suma > pow(radius, 2)) { 
      matrixB[i][j] = matrixB[i][j] * 3; 
     } 
    } 
} 

Dann, wenn ich drucken, um die Elemente der ersten Matrix:

for (int k = 0; k < HEIGHT; ++k) { 
    for (int i = 0; i < WIDTH; ++i) { 
     cout << matrixR[k][i] << " | "; 
    } 
    cout << '\n'; 
} 

Wo ich matrixR[i][j]*1 bekommen sollte ich bin immer matrixB[i][j]. Überschreiben sich die Matrizen? Ist irgendwas falsch?

+0

Gut geschrieben en Frage. :) – MordechayS

+0

Was ist der Punkt von 'matrixR [i] [j] = matrixR [i] [j] * 1;'? –

+1

Haben Sie die Werte in Ihren Matrizen initialisiert? –

Antwort

1

Ich fügte Werte für centerX, centerY und radius hinzu und fügte einen Initialisierungsabschnitt hinzu (alle Matrixelemente sind eins hinterher).

ich keinen Fehler finden kann, ist der Ausgang

1 | 1 | 1 | 1 | 1 | 1 | 1 | 
1 | 1 | 1 | 1 | 1 | 1 | 1 | 
1 | 1 | 1 | 1 | 1 | 1 | 1 | 
1 | 1 | 1 | 1 | 1 | 1 | 1 | 
1 | 1 | 1 | 1 | 1 | 1 | 1 | 


2 | 2 | 2 | 2 | 2 | 2 | 2 | 
2 | 2 | 2 | 2 | 2 | 2 | 2 | 
2 | 2 | 2 | 1 | 2 | 2 | 2 | 
2 | 2 | 1 | 1 | 1 | 2 | 2 | 
2 | 2 | 2 | 1 | 2 | 2 | 2 | 


3 | 3 | 3 | 3 | 3 | 3 | 3 | 
3 | 3 | 3 | 3 | 3 | 3 | 3 | 
3 | 3 | 3 | 1 | 3 | 3 | 3 | 
3 | 3 | 1 | 1 | 1 | 3 | 3 | 
3 | 3 | 3 | 1 | 3 | 3 | 3 | 

Dies ist der Code, die ich verwendet habe:

// init variables 
int HEIGHT = 5; 
int WIDTH = 7; 
int centerY = 3; 
int centerX = 3; 
int radius = 1; 



int ** matrixR= new int*[HEIGHT]; 
int ** matrixG= new int*[HEIGHT]; 
int ** matrixB= new int*[HEIGHT]; 

for(int i=0; i<HEIGHT; i++){ 
     matrixR[i]= new int[WIDTH]; 
     matrixG[i]= new int[WIDTH]; 
     matrixB[i]= new int[WIDTH]; 
} 


// init matrix values 
{ 
for (int k = 0; k < HEIGHT; ++k) 
    for (int i = 0; i < WIDTH; ++i) 
     matrixR[k][i] = 1; 

for (int k = 0; k < HEIGHT; ++k) 
    for (int i = 0; i < WIDTH; ++i) 
     matrixG[k][i] = 1; 

for (int k = 0; k < HEIGHT; ++k) 
    for (int i = 0; i < WIDTH; ++i) 
     matrixB[k][i] = 1; 
} 


for (int i = 0; i < HEIGHT; ++i) { 
    for (int j = 0; j < HEIGHT; ++j) { 
     float suma = pow(static_cast<double>(i) - centerY, 2) + pow(static_cast<double>(j) - centerX, 2); 
     if (suma > pow(static_cast<double>(radius), 2)) { 
      matrixR[i][j] = matrixR[i][j] * 1; 
     } 
    } 

} 

for (int i = 0; i < HEIGHT; ++i) { 
    for (int j = 0; j < WIDTH; ++j) { 
     float suma = pow(static_cast<double>(i) - centerY, 2) + pow(static_cast<double>(j) - centerX, 2); 
     if (suma > pow(static_cast<double>(radius), 2)) { 
      matrixG[i][j] = matrixG[i][j] * 2; 
     } 
    } 
} 

for (int i = 0; i < HEIGHT; ++i) { 
    for (int j = 0; j < WIDTH; ++j) { 
     float suma = pow(static_cast<double>(i) - centerY, 2) + pow(static_cast<double>(j) - centerX, 2); 
     if (suma > pow(static_cast<double>(radius), 2)) { 
      matrixB[i][j] = matrixB[i][j] * 3; 
     } 
    } 
} 

for (int k = 0; k < HEIGHT; ++k) { 
    for (int i = 0; i < WIDTH; ++i) { 
     cout << matrixR[k][i] << " | "; 
    } 
    cout << '\n'; 
} 

cout << '\n'; 
cout << '\n'; 

for (int k = 0; k < HEIGHT; ++k) { 
    for (int i = 0; i < WIDTH; ++i) { 
     cout << matrixG[k][i] << " | "; 
    } 
    cout << '\n'; 
} 

cout << '\n'; 
cout << '\n'; 

for (int k = 0; k < HEIGHT; ++k) { 
    for (int i = 0; i < WIDTH; ++i) { 
     cout << matrixB[k][i] << " | "; 
    } 
    cout << '\n'; 
} 

std::cin.get(); 
+0

Danke, scheint zu sein, dass es die Tatsache war, dass meine Matrizen global waren. Ich verstehe immer noch nicht, warum es nicht funktioniert, aber Wenn Sie (wie Sie) in einem Hauptprogramm() platziert sind, funktioniert das. – danielsto

+0

könnten Sie Ihren ursprünglichen Code posten, wo der Fehler auftritt? –

0

hat dieses Bild zu machen, kompilieren und ausführen:

for(int i=0; i < HEIGHT; ++i) { 
    for (int j=0; j < WIDTH; ++j){ 
     matrixR[i][j] = 3; 
     matrixG[i][j] = 20; 
     matrixB[i][j] = 30; 
    } 
} 

int centerX = 5; 
int centerY = 5; 
int radius = 2; 

Ausgabe bekomme ich sieht gut aus ...

Verwandte Themen