[問題] 迴圈最後值出錯

看板C_and_CPP作者 (多情種子)時間15年前 (2010/05/04 02:19), 編輯推噓1(105)
留言6則, 4人參與, 最新討論串1/1
我跑了一個迴圈 直到最後的值都好好的 最後一圈的時候很奇怪結果會變0 我是要算random產生的座標與原點的距離(著色的部份) 最後一個值 distance[SBRX]會變成0 好奇怪喔 亂數座標也看過都很大說 其他都沒有問題 就只有最後一個值 有沒有人可以告訴我為什麼呢 全部程式碼: (有問題處著色了) #include <iostream> #include <iomanip> #include <ctime> #include <cmath> #include <cstdlib> // contains function prototype for rand using namespace std; int main() { int SARX= 1; int SBRX= 4; int S= 5; int M= 2; int mu= pow(2.0,M); int square_length = 6000; //6km int square_width = 6000; //6km int Pmax= 10; //10(mW),10dbm int breakpoint=60; //60m double noise= pow(10.0, -12.3); //123dBm double Loss= pow(10.0, 25.3); //decaying coefficient double position_x[SBRX]; double position_y[SBRX]; double distance[SBRX]; int strategy_table[SBRX][S][mu]; for ( int N= 1; N<= SBRX ; N++ ) { const double scale1 = rand()/(double)RAND_MAX; const double scale2 = rand()/(double)RAND_MAX; position_x[N]= scale1* square_width; position_y[N]= scale2* square_length; distance[N]=sqrt(pow(position_x[N], 2) + pow(position_y[N], 2)); for(int X= 1; X<= S; X++ ) { for(int Y= 1; Y<= mu; Y++) { const double scale3 = rand()/(double)RAND_MAX; double proportion= (double)(X-1)/(S-1); if(scale3 <= proportion) strategy_table[N][X][Y]= 1; else strategy_table[N][X][Y]= 0; //cout<< "**"<<scale3<< "**" ; //cout<<proportion; cout<<strategy_table[N][X][Y] <<" "; } cout<< endl; } cout << "("<<position_x[N]<<","<<position_y[N]<<")"<< endl; cout << "The distance between SBRX and SARX is "<< distance[N]<<endl; } return 0; } -- 無名網誌(日本生活): http://www.wretch.cc/blog/gigigaga1202 Lang-8: http://lang-8.com/20414 如果你繼續追逐我的幻影 你將被真正的我追上 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.71.92.74 ※ 編輯: gigigaga17 來自: 202.71.92.74 (05/04 10:19) ※ 編輯: gigigaga17 來自: 202.71.92.74 (05/04 10:22) ※ 編輯: gigigaga17 來自: 202.71.92.74 (05/04 10:23) ※ 編輯: gigigaga17 來自: 202.71.92.74 (05/04 10:23)

05/04 10:26, , 1F
你的程式應該會當掉...
05/04 10:26, 1F

05/04 10:27, , 2F
索引值超出範圍?
05/04 10:27, 2F

05/04 10:51, , 3F
宣告 double array[10]; 可以合法存取的index在0~9....
05/04 10:51, 3F

05/04 10:51, , 4F
另外, 不知道你的開發環境是啥, 祈禱它支援VLA....
05/04 10:51, 4F

05/04 11:43, , 5F
又犯這個錯 天阿 我真的被matlab影響太久...
05/04 11:43, 5F

05/04 11:43, , 6F
我是用dev C++ 應該可以跑了 orz...謝謝大家
05/04 11:43, 6F
文章代碼(AID): #1BtuGSnp (C_and_CPP)