2017-01-22 6 views
-1

さて、私はこの練習をしなければならないと私はこの方法で、この配列2Dを表示することができますどのように今ドントThis is the exercise:私は試験のために勉強しています、私は助けが必要です! C++

これは私がやっていることです:

#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; 
    } 

} 

まあ、私の疑問はどのようにあります私の質問は行と列を減算するために追加する変更を行う方法ですので、この行列を通過する。私はinformaticaの最初の学生であり、よく私はルーキーであるときにいくつかの通常の疑問を持っています。助けてくれれば幸いです。

+0

"行と列を減算するために加算する"ということをさらに詳しく説明できますか? –

+0

練習の翻訳を提供する必要があります。 – Gabriel

+4

[なぜ誰かが私を助けることができますか?実際の質問ではありませんか?](http://meta.stackoverflow.com/q/284236) – EJoshuaS

答えて

0

インデックスについて考えてみましょう。行の場合はi、列の場合はj、列の場合はrowscolumnsの列があります。

最初に、行i=0については、j=0からcolumns-1までのすべての列を印刷する必要があります。

次に、j=columns-1列の場合、すべての行から最初の行を差し引いて、i=1からrows-1までの行を印刷する必要があります。

次に、i=rows-1の場合は、j=columns-1~j=0となります。

など。

//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

はい、これは方法です、本当にありがとう! –

関連する問題