2016-11-28 56 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; 
} 
相关问题