C程序崩溃

时间:2018-03-24 18:38:41

标签: c arrays algorithm

我发现Reddit上的一个挑战是制作一个给出数字A的程序,如果B+C会找到B*C = A的最小可能值。此处ABC必须都是正整数。出于某种原因,我的程序崩溃了,我无法找到原因。我仍然是C的初学者,我还在努力学习这门语言。任何帮助和提示都会有所帮助。

#include <stdio.h>
#define MAXDIM 128

int factor1[MAXDIM];
int factor2[MAXDIM];
int sum[MAXDIM];
int counter;

void factors(int a);
void sumf(const int *f1,const int *f2);
int smallestSum(const int *s);

int main(void){
    int a;
    scanf("%d",&a);
    factors(a);
    sumf(&factor1[0],&factor2[0]);
    printf("%d => %d",a,smallestSum(&sum[0]));
    return 0;
}

void factors(int a){
    int i=1;
    int j=0;
    while(i<a/2){
        if(a%i==0){
            /*two arrays which hold all factors 
            e.g. 
            a = 10 
            factor1 = 1 2
            factor2 = 10 5
            */
            factor1[j]=i;
            factor2[j]=a/i;
            j++;
        }
        i++;
    }
    counter=i; // counter which holds length of factor arrays, used in other functions
}

void sumf(const int *f1,const int *f2){
    int i=0;
    while(i<counter+1){
        // array which holds sums of factor1 and factor2
        sum[i]=*(f1+i)+*(f2+i);
        i++;
    }
}

int smallestSum(const int *s){
    int min;
    int i=0;
    min=*s;
    while(i<counter+1){
        if(*(s+i)<min){
            min=*(s+i);
        }
    }
    return min; 
}

3 个答案:

答案 0 :(得分:0)

首先,在最小的时候,我将永远为0,所以它将永远运行。 我想你需要做i ++。但我认为你有更多的错误。 运行时你提供了什么输入?

答案 1 :(得分:0)

这里有一个可能的崩溃源:

counter=i;

它应该是:

counter=j;

j保留因子数组索引,而不是i。另一个崩溃源是:

void sumf(const int *f1,const int *f2){
    int i=0;
    while(i<counter+1){ // <-- here
         ...
    }
}

因为你有counter元素,所以它应该是这样的:

while(i<counter)

smallestSum()也有同样的问题。另外,你忘了在i的循环中增加smallestSum()

我应该说这些是我发现的错误,可能还有更多我错过的错误。但是一般来说,当你获得更多经验时,这段代码会更好。

答案 2 :(得分:0)

调试程序时使用调试器和断点。它还有助于打印出中间结果,以查看算法是否正常工作。 这样做可以注意到counter被初始化为不正确 一直都很有价值。您会注意到smallestSum()函数永远不会结束,因为循环永远不会中断。您需要在该函数内的适当位置增加i。检查您的条件while(i<counter+1)是否正确,因为循环从0开始比较counter就足够了。

该程序在所有这些更正之后起作用:

#include <stdio.h>
#define MAXDIM 128

int factor1[MAXDIM];
int factor2[MAXDIM];
int sum[MAXDIM];

int counter;

void factors(int a);
void sumf(const int *f1, const int *f2);
int smallestSum(const int *s);

void factors(int a){
    int i = 1;
    int j = 0;     
    while( i < a/2 ){

        if( a%i == 0){
            /*two arrays which hold all factors 
            e.g. 
            a = 10 
            factor1 = 1 2
            factor2 = 10 5
            */
            factor1[j] = i;
            factor2[j] = a/i;

            printf("i=%d j=%d factor1[j]=%d factor12[j]=%d\n",i,j,factor1[j],factor2[j]  );

            j++;
        }           
        i++;
    }

    counter = j; // counter which holds length of factor arrays
}

void sumf(const int *f1, const int *f2){

    int i = 0; 
    while(i<counter){
        // array which holds sums of factor1 and factor2
        sum[i] = *(f1+i) + *(f2+i);

         printf("i=%d sum %d %d %d\n", i,  sum[i], *(f1+i), *(f2+i) );
        i++;
    }
}

int smallestSum(const int *s){

    int min;
    int i = 0;
    min =*s;

    while(i<counter){

        if( *(s+i) < min){
             min= *(s+i);
        }
        i++;
    }

    return min;
}

int main(void){

    int a;
    scanf("%d",&a);

    factors(a);

    sumf( &factor1[0], &factor2[0] );

    printf("For a=%d the smallest sum is => %d", a, smallestSum(&sum[0]));    
    return 0;
}

测试:

90                                                                                                                                             
i=1 j=0 factor1[j]=1 factor12[j]=90                                                                                                            
i=2 j=1 factor1[j]=2 factor12[j]=45                                                                                                            
i=3 j=2 factor1[j]=3 factor12[j]=30                                                                                                            
i=5 j=3 factor1[j]=5 factor12[j]=18                                                                                                            
i=6 j=4 factor1[j]=6 factor12[j]=15                                                                                                            
i=9 j=5 factor1[j]=9 factor12[j]=10                                                                                                            
i=10 j=6 factor1[j]=10 factor12[j]=9                                                                                                           
i=15 j=7 factor1[j]=15 factor12[j]=6                                                                                                           
i=18 j=8 factor1[j]=18 factor12[j]=5                                                                                                           
i=30 j=9 factor1[j]=30 factor12[j]=3                                                                                                           
i=0 sum 91 1 90                                                                                                                                
i=1 sum 47 2 45                                                                                                                                
i=2 sum 33 3 30                                                                                                                                
i=3 sum 23 5 18                                                                                                                                
i=4 sum 21 6 15                                                                                                                                
i=5 sum 19 9 10                                                                                                                                
i=6 sum 19 10 9                                                                                                                                
i=7 sum 21 15 6                                                                                                                                
i=8 sum 23 18 5                                                                                                                                
i=9 sum 33 30 3                                                                                                                                
For a=90 the smallest sum is => 19