看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 嚴格講起來有兩個,但我不知道是否解決了一個,另一個也可以使用同樣的概念去解決.. .... 有兩組陣列,一個是bool型態,另一個是double型態。 在main()中呼叫兩組動態陣列,它們的初始值已經在副程式中給過了。 我知道如何在main()中改它們的值,但我不知道如何將他們傳到另外的副程式中,在該 副程式中做運算、改變它們的值,再將他們傳回到main()中...... 我把問題直接在下面的程式碼中敘述。 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include <iostream> using std::cout; using std::cin; using std::ios; using std::cerr; using std::endl; #include <stdio.h> #include <fstream> using namespace std; using std::ifstream; using std::ofstream; #include <cstdlib> #include <math.h> void read_obstacles(); //我在main()中呼叫obst(它是二維動態陣列,在呼叫的副程式中已經給初始值了,假設 //全部都是0), //我另有一個txt檔,裡面有"座標",我想用read_obstacles()這個副程式來讀這個txt //檔,只要"座標"符合obst這個陣列的"元素值",它的bool值就改設成1 //我用過void這個宣告,compile會過,但跑到一半就會出現 //想請教這邊應該怎麼改、main()中應該如何呼叫、最下面的副程式該如何寫呢? ifstream inobst("obst.txt", ios::in); //這個txt檔的內容如下 2 0 2 1 2 2 2 3 0 0 0 1 0 2 0 3 0 0 1 0 2 0 0 3 1 3 2 3 double ***create_3D_Array(int a, int b, int c); bool **create_2D_Array_b(int a, int b); bool **obst; //######################################################################## int main() { int a,b,c; int i,j,k; a = 4; b = 3; c = 3; double ***arr = create_3D_Array(a,b,c); bool **obst = create_2D_Array_b(b,a); //如果上面的宣告不改,最下面的副程式寫法也不改,且沒有在main中呼叫 //read_obstacles()的話,是不會有任何問題的 //但呼叫read_obstacles()的話,就會出現The variable 'b' is being //used without being initialized. //請問上面的宣告、這裡的呼叫、最下面的副程式應該怎麼改 //才能讀進txt檔的資料以改變obst的元素值呢? cout << "3D:" << endl; for(i=0; i<=a-1; i++){ arr[i][0][0] = i; } //第二個問題其實跟第一個問題很雷同: //就是我可以在main中去更改arr的元素值,但要如何將它們傳到別的副程 //式中去做運算,再傳回來main中呢? system("pause"); return 0; } double ***create_3D_Array(int a, int b, int c) //######################################################################## { double ***A = new double**[a]; for (int i = 0 ; i < a ; i++) { A[i] = new double*[b]; for (int j = 0 ; j < b ; j++) { A[i][j] = new double[c]; } } for (int i = 0 ; i < a ; i++) for (int j = 0 ; j < b ; j++) for (int k = 0 ; k < c ; k++) A[i][j][k] = 0; return A; } bool **create_2D_Array_b(int a, int b) //######################################################################### { bool **B = new bool*[a]; for (int i = 0 ; i < a ; i++) { B[i] = new bool [b]; } for (int i = 0 ; i < a ; i++) for (int j = 0 ; j < b ; j++) B[i][j] = 0; return B; } void read_obstacles() { //############################################################################## int i, j; int a, b; for( j=0; j<b; j++) { for( i=0; i<a; i++) { obst[j][i] = false; //the value of false is 0 } } while ( inobst >> j >> i ) obst[j][i] = true; //the value of true is 1 } // read_obstacles() 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.130.48 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1437579712.A.9AE.html
flyfoxy: 建議用class內宣告要new的指標 07/23 00:02
flyfoxy: 我以前用你方法這樣做好像要多取一次位址丟回main 07/23 00:03
moussorgsky: 我有想過這問題,因為我做運算的副程式會有好幾個, 07/23 00:15
moussorgsky: 而且傳進副程式的動態陣列不會只有一組,這樣做好像 07/23 00:16
moussorgsky: 會很複雜...... 07/23 00:16
stupid0319: 好多***星星**連在一起,眼睛都花了 07/23 00:17
kaneson: 當判斷j<b的時候,b是多少?? 07/23 10:43
iWRZ: 拜託 //****** . //----- . //++++ ////////// 之類的拜託 07/23 15:24
iWRZ: 少用 看得人很累 07/23 15:24
iWRZ: /* 07/23 15:25
iWRZ: * 07/23 15:25
iWRZ: * 07/23 15:25
iWRZ: */ 這種就很好用又簡潔 07/23 15:25
MOONRAKER: 好用又簡潔又不是一條橫線 07/23 15:26
MOONRAKER: //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 07/23 15:27
iWRZ: //this is a function used for blablabla. ←這樣就好了 07/23 15:31