2016-04-30 38 views
-2

我已創建一個二維數組程序和我試圖打印出該陣列基底上用戶選擇排序和打印出2D陣列基底上的用戶輸入

這是陣列

1 2 3 
1a b c 
2d e f 
3g h i 

如果在1B3用戶密鑰,它會顯示 adgbehcfi

如果C21的進入,它會顯示 cfibehadg

現在我已創建該數組,但是我堅持如何打印出基於用戶輸入數組的順序,請幫助我。謝謝。

花了一些日子後做,下面是我的代碼

#include <iostream>; 
using namespace std; 

int main() 
{ 
    string alphabate; 
    string array[3][3]; 
    string a="abcdefghi"; 

for(int i=0; i<3; i++) 
{ 
    for(int j=0; j<3; j++) 
    { 
     array[i][j] = a[j+ (i * 3)]; 
    } 
} 

for(int i=0; i<3; i++) 
{ 
    for(int j=0; j<3; j++) 
    { 
     cout << array[i][j]; 
    } 

    cout << endl; 
} 

cout << "Enter some alphabate:"; 
cin >>alphabate; 

    return 0; 
} 
+1

莫非你精心1B1 ABD C21 –

+0

它們指的是列第一列是1,2爲b,因爲b在字母順序排列3 column.I跟隨第二犯了一個錯誤,它是1b3 – Happy

+0

「我堅持如何讀取數組」看起來你已經知道'cin'了,那麼問題是什麼? – user463035818

回答

0

據我瞭解,你要打印僅基於列數/索引用戶已經進入了數組元素。這就是我意識到的。希望幫助:)

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

string array[3][3]; 

//function that prints the column c of the array 
void printArray(int c) { 
    for(int i=0; i<3; i++) { 
     cout << array[i][c]; 
    } 
} 

int main() { 

    string alphabate; 
    string a="abcdefghi"; 

    for(int i=0; i<3; i++) { 
     for(int j=0; j<3; j++) { 
      array[i][j] = a[j+ (i * 3)]; 
     } 
    } 

    cout << "Enter some alphabate:"; 
    cin >> alphabate; 

    //checking the input parameters 
    for (int j=0; j<3; j++) { 
     if (alphabate[j] == '1' || alphabate[j] == 'a') { 
      printArray(0); 
     } 
     else if (alphabate[j] == '2' || alphabate[j] == 'b') { 
      printArray(1); 
     } 
     else if (alphabate[j] == '3' || alphabate[j] == 'c') { 
      printArray(2); 
     } 
     else { 
      cout << "Error"; 
     } 
    } 

return 0; 
} 
+0

謝謝你的幫助,但是我花了一些時間試圖解決另外兩件事情。我已經更新了他們的問題。再一次感謝你。 – Happy

+0

我可以更新我的問題,所以我會在這裏發表。第一件事是我怎樣才能讓我的程序變得更聰明,就像讓cin >> alphabate更接近邏輯1234一樣,拒絕那些不像1(缺少2)34那樣的輸入。 另一件事是我如何讓我的程序接受像10個單詞的數組。我注意到,如果我輸入helloworld密碼文本缺少2d數組中的字母d。再次感謝您的幫助。謝謝 – Happy

+0

嗨,我忘了添加,我確實使數組成爲數組[5] [5],最終爲unix環境中的缺失字段獲取垃圾值。 – Happy