2017-01-22 35 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列,rows行和columns列。

首先,對於行i=0,您必須打印所有列,從j=0columns-1

接下來,對於j=columns-1列,您必須打印所有行減去第一個,從i=1rows-1

接下來,對於i=rows-1,打印j=columns-1j=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

是的,這是非常感謝! –