2017-01-22 2 views
-1

Nun, ich habe diese Übung zu tun, und ich jetzt dont wie dieses Array 2D auf diese Weise zeigen können This is the exercise:Ich studiere für eine Prüfung, ich brauche Hilfe! C++

Dies ist, was ich getan habe:

#include <iostream> 
#include <cmath> 
using namespace std; 


const int MaxFila = 8; 
const int MaxColumna = 5; 

void rellenarVector (int matriz[MaxFila][MaxColumna]); 
void MostrarMatriz (int matriz[MaxFila][MaxColumna]); 

int main() { 

    int matriz[MaxFila][MaxColumna]; 
    rellenarVector(matriz); 
    MostrarMatriz(matriz); 

    return 0; 
} 

void rellenarVector (int matriz[MaxFila][MaxColumna]){ 

    cout << "introduce los valores de la matriz: " << endl; 
    for(int i = 0; i < MaxFila; i++){ 
     for(int j = 0; j < MaxColumna; j++){ 
      cin >> matriz[i][j]; 
     } 
    } 
} 

void MostrarMatriz (int matriz[MaxFila][MaxColumna]){ 

    int Filaux = 0, columaux = 0; 

    for(int i = Filaux; i < MaxFila; i++){ 

     for(int j = columaux; j < MaxColumna; j++){ 
      cout << matriz[i][j] << " "; 
     } 
     cout << endl; 
    } 

} 

Nun meine Zweifel, wie Um diese Matrix zu durchlaufen, weil meine Frage ist, wie man die Änderung von addieren, um Zeilen und Spalten zu subtrahieren. Ich bin ein Student der ersten informatica und gut ich habe einige normale Zweifel, wenn ich Anfänger bin. Jede Hilfe wäre hilfreich, danke.

+0

Können Sie bitte genauer erklären, was Sie meinen, indem Sie "addieren, um Zeilen und Spalten zu subtrahieren"? –

+0

Sie sollten eine Übersetzung der Übung zur Verfügung stellen. – Gabriel

+4

[Warum kann "Kann mir jemand helfen?" Keine eigentliche Frage?] (Http://meta.stackoverflow.com/q/284236) – EJoshuaS

Antwort

0

Denken Sie über die Indizes nach. Angenommen, Sie haben i für die Zeilen und j für die Spalten, rows Zeilen und columns Spalten.

Zuerst müssen Sie für die Zeile i=0 alle Spalten drucken, von j=0 bis columns-1.

Als nächstes müssen Sie für die Spalte j=columns-1 alle Zeilen minus die erste von i=1 bis rows-1 drucken.

Als nächstes, für i=rows-1, drucken j=columns-1 bis j=0.

Und so weiter.

//make sure to obtain columns and rows from to your matrix 
int columns=5; 
int rows=8; 

int direction=1; //let say 1 is left to right (start direction), 2 is up to down, 3 is right to left and 4 is down to up 
int i=0, j=0; 

//set the limits where it must change direction 
int minJ=0, maxJ=columns-1, minI=1, maxI=rows-1, totalElements=rows*columns; 

for(int k=0;k<totalElements;k++) { //stop when all elements have been printed 
    cout<<matriz[i][j]<<" "; 

    //move one index based on the direction 

    if(direction==1) { 
     //check if direction must change, otherwise advance in this direction 
     if(j==maxJ) { 
      //now go down 
      direction=2; 
      //reduce the limits, because the next time its going in this direction, the last element will already have been printed 
      maxJ--; 
      //also move the pointer in the new direction so in the next loop it prints the next element 
      i++; 
     } else { 
      j++; 
     } 
    } else if(direction==2) { 
     if(i==maxI) { 
      //now go left 
      direction=3; 
      maxI--; 
      j--; 
     } else { 
      i++; 
     } 
    } else if(direction==3) { 
     if(j==minJ) { 
      //now go up 
      direction=4; 
      minJ++; 
      i--; 
     } else { 
      j--; 
     } 
    } else if(direction==4) { 
     if(i==minI) { 
      //now go right again 
      direction=1; 
      minI++; 
      j++; 
     } else { 
      i--; 
     } 
    } 
} 
+0

Ja, das ist der Weg, vielen Dank! –

Verwandte Themen