2016-11-28 57 views
0

所以我的程序中有整數m和n,一旦你輸入了值,它應該創建一個數值從m到n的數組(例如m = 1和n = 10它會創建數組q,其值爲1到10)。然後在數組中查看是否有任何數字等於任何兩個數字平方和(例如,在數組中,數字5等於1平方+2平方)。問題是,當我嘗試輸入它崩潰的第一個值時,確定問題出現在函數中,但似乎無法弄清楚。由於當我嘗試輸入值時,程序崩潰

#include <iostream> 
using namespace std; 

int squared (int a, int b, int q[]){ 


    while (a<=0 || b<=0){ 
     cout <<"You can't input an integer that is 0 or below: "; 
     cin >>a; 
     cin >>b; 
     if (a>0 || b>0) break; 
    } 


    for (int p=0; p<b; p++){ 
     for (int i=a ; i<b; i++){ 
      q[p] = a; 
     } 
    } 

    for (int z=0; z<b; z++){ 
     for (int x=0; x<b; x++){ 
      for (int c=0; c<b; c++){ 
       if (q[z] == (q[x] * q[x]) + (q[c] * q[c])){ 
        int result= (q[x] * q[x]) + (q[c] * q[c]); 
        return result; 

       } 
      } 
     } 
    } 



} 

int main() { 
    int m,n; 
    int M[100]; 
    cout <<"Input integers m un n: "; 
    cin >>m,n; 
    cout <<squared(m,n,M); 

return 0; 
} 
+1

'CIN >> M,N;'沒有做什麼,你認爲它。 – NathanOliver

+1

您是否嘗試過在調試器中查找錯誤發生的位置?另外,你能定義「崩潰」嗎?究竟出了什麼問題?有關調試小程序,請參閱[本文](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 – EJoshuaS

+0

'cin >> m >> n;' – Raindrop7

回答

1

最有可能崩潰,因爲這個:cin >>m,n;,它應該是cin >> m >> n;,否則你用n未初始化的下一行,從而得到不確定的行爲,例如崩潰。

你使用什麼編譯器和什麼標誌,因爲這會在編譯時正常觸發一些警告/錯誤。

0

cin >> m, n;是不正確,它輸入只有這可以解釋爲m:

(cin >> m), n;這意味着:cin, n;糾正它:因爲你檢查此條件兩次

cin >> m >> n;

if(a > 0 || b > 0) break;是多餘的:一旦在條件成立的時候(a或b等於或小於0),while while條件second while while檢查相同的條件是多餘的,因爲它會自動中斷。

你通過而不通過其大小的陣列,你是幸運的,如果設置了第一元件1中的任何第二值等於陣列例如大小:

m = 1; n = 10; then the size is ten which is correct. 

什麼約:

m = 7; n = 10; // now is size 10? it's only 3 

糾正它傳遞的大小如:

m = 4; n = 8; 
int size = 8 - 4; 
cout << Squared(m, n, M, size); 

也:

for (int p = 0; p < b; p++) 
{ 
    for (int i = a ; i < b; i++) 
    { 
     q[p] = a; 
    } 
} 

你正在分配相同的值a到數組的所有元素和迭代在嵌套循環中做同樣的事情!它很可能寫入:

int x = 0; x = 0; 

因此,平方內結果的條件永遠不會成功,因爲相同的值永遠不會等於它的平方。 4 = 4 * 4從未達到

這裏是你的搜索內容:

#include <iostream> 
using namespace std; 

// I only make squared search for the result not inputing m and n lik e in yours 
int squared (int m, int n, int* M) 
{ 
    int result; 
    for(int i(0); i < n; i++) 
     for(int j(0); j < n; j++) 
      for(int k(0); k < n; k++) 
       if((M[i] == ((M[j] * M[j]) + (M[k] * M[k]))) && j != k) // here to avoid comparing with the same index 
       { 
        cout << M[i] << " = (" << M[j] << "*" << M[j] << ") + (" << M[k] << "*" << M[k] << ")" << endl; 
        result = ((M[j] * M[j]) + (M[k] * M[k])); 
        cout << result << endl; 
        return result; // if success we return result 
       } 

    return -1; // otherwise we return -1 as a sign of error (no square yields in negative value) 
} 

int main() 
{ 
    int n, m, size; 

    do 
    { 
     cout <<"m: "; 
     cin >> m; 
     cout << "n: "; 
     cin >> n; 

     if(n <= 0 || m <= 0) 
      cout <<"You can't input an integer that is 0 or below: "; 
     // also it's very important to check whether n is greater than m or not because if m == n or m > n then size will be negative and as you know no array has a negative nor 0 size 
     if(m >= n) 
      cout << "n must be greater than m!" << endl; 
    }while (m <= 0 || n <= 0 || m >= n); 

    size = n - m; // getting size of array assuming m can be any value 
    int* M = new int[n - m]; // allocating dynamic array 

    // inputting array as you asked 
    for(int i(0), j = m; i < size; i++, j++) 
     M[i] = j; 

    // checking the values of array elements 
    for(int i = 0; i < size; i++) 
     cout << M[i] << ", " ; 
    cout << endl; 

    // getting the result 
    cout << squared(m, n, M) << endl; 

    // cleaning 
    delete[] M; 

    return 0; 
} 
相關問題