[問題] 我有一個陣列函式 想把結果值cout出來

看板C_and_CPP (C/C++)作者 (飛揚的杯子)時間14年前 (2011/12/15 13:17), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) C++/C 問題(Question): 我的函式在這裡 Calc_index(Seed S,int t[]) double Calc_index(Seed S,int t[])//S為解的陣列 { int i,ii; int order; for(i=0;i<City;i++) { order=1; for(ii=0;ii<City;ii++) { if((i!=ii)&& (S.Coord[ii])<(S.Coord[i])) { order+=1; } } t[i]=order; cout << t[i] << " "; } cout<<endl; return 0; } 利用結構資料儲存這個函式值 struct Seed { long double F[City];//磁力 long double Coord[City];//維度 long double Q;//電荷量 long double Total_d;//總路徑距離 long double index;//路徑行經路線(這個函式的儲存值) long double temp_T;//SA溫度 } Seeds[Seed_number],Seed_T[Seed_number]; 這是我資料執行的部分 Seed SA[Seed_number]; Seed SA_Int[Seed_number]; Seed temp[Seed_number]; long double SA_temp; for(i=0;i<Seed_number;i++) { SA_temp = Seeds[i].Total_d; while ((iter<Sa_iteration)&&( temp_t>Ts)) { //------------------------------------------- Seedasgn(&SA[i],&Seeds[i]); Seedasgn(&SA_Int[i],&Seeds[i]); //產生更新解 for(j=0;j<City;j++) { SA[i].Coord[j]=lb+(ub-lb)*Rand; } Calc_index(SA[i],t); SA[i].index = t[i]; SA[i].Total_d=Calc_distance(SA[i]); totdif_SA=SA[i].Total_d-Best.Total_d; //sum(f(x)-f(x_best)) if(totdif_SA<0) { SA_Int[i].index = SA[i].index; SA_Int[i].Total_d = SA[i].Total_d; } else { my_rand = ((double)rand()*5/RAND_MAX); my_exp = exp(totdif_SA/temp_t); if(my_rand< my_exp) { SA_Int[i].index = SA[i].index; SA_Int[i].Total_d = SA[i].Total_d; } } ------------------------------------------------------------------ if( SA[i].Total_d < SA_temp ) { SA_temp = SA[i].Total_d; temp[i].Total_d = SA[i].Total_d; temp[i].index = SA[i].index; } -----在這裡做SA[i].temp_T資料轉換 故SA[i].index會變---------------- SA[i].temp_T = temp_t; cout << "SA[i].temp_T = " << SA[i].temp_T << endl; temp_t = (SA[i].temp_T/(1+cooling*SA[i].temp_T)); cout << "跌代溫度 = " << temp_t << endl; iter++; } ------在這裡做SA_Int[i].temp_T資料轉換 故SA_Int[i].index會變---------- iter=0; if ( temp[i].Total_d < SA_Int[i].Total_d ) { SA_Int[i].Total_d = temp[i].Total_d; SA_Int[i].index = temp[i].index; } ------在這裡做Seeds[i].temp_T資料轉換 故Seeds[i].index會變--------- if ( SA_Int[i].Total_d < Seeds[i].Total_d) { Seeds[i].Total_d = SA_Int[i].Total_d; Seeds[i].index = SA_Int[i].index; } cout <<"SA+初始解挑選 = " << Seeds[i].Total_d << endl; } for(i=0;i<Seed_number;i++) { cout << "第"<< i+1 <<"個解 總距離 = " << Seeds[i].Total_d <<endl; } for(i=0;i<Seed_number;i++) { Search_best(Seeds[i],i,&min); //i++; } cout << endl << "---------------------------" << endl; cout << "SA最佳解 = " << min << endl << endl; return 0; } ------------------------------------------------ 我想問的是....如何把Calc_index(Seed S,int t[])的函數值 用SA_Int[i].index出來... 我想確定index 會不會跟著我寫的函式讀進去該名稱位址裡 ----------------------------------------------- 附上我執行出來的結果... http://ppt.cc/CGm- 我直接上載執行檔 這是我執行出來的結果的圖片... http://ppt.cc/DoL~ 這是流程架構圖 有密碼 唷 http://ppt.cc/dZ_H -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.218.80.41

12/15 13:22, , 1F
好像不能執行??我貼圖好了XD
12/15 13:22, 1F
※ 編輯: flyingcop 來自: 180.218.80.41 (12/15 13:36)

12/15 13:52, , 2F
你預期要跑出什麼結果出來?
12/15 13:52, 2F

12/15 13:53, , 3F
抱歉沒看圖 其實只要知道是哪個陣列索引下去就沒問題了
12/15 13:53, 3F

12/15 13:54, , 4F
不要怕 value[index[i]] 這種看起來很可怕的東西
12/15 13:54, 4F

12/15 14:00, , 5F
什麼是value[index[i]]??呀
12/15 14:00, 5F

12/15 18:14, , 6F
應該改良式模擬退火法.方便放整體大致流程或結構對照??
12/15 18:14, 6F
※ 編輯: flyingcop 來自: 140.127.189.67 (12/15 19:58)

12/15 19:59, , 7F
我有放流程架構對照圖了
12/15 19:59, 7F
文章代碼(AID): #1EwOBQrH (C_and_CPP)
文章代碼(AID): #1EwOBQrH (C_and_CPP)