简体   繁体   English

C程序崩溃

[英]C program crashing

I have found a challenge on Reddit to make a program which given a number A , will find the smallest possible value of B+C , if B*C = A . 我发现Reddit面临一个挑战,即如果B*C = A ,则给出给定数字A的程序将找到B+C的最小可能值。 Here A , B , and C must all be positive integers. 在此, ABC必须全部为正整数。 For some reason, my program crashes and I can't find the reason why. 由于某种原因,我的程序崩溃了,我找不到原因。 I'm still a beginner in C and I'm still trying to learn the language. 我仍然是C语言的初学者,并且仍在尝试学习该语言。 Any help and tips would be helpful. 任何帮助和提示都将有所帮助。

#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; 
}

To start off with, in smallestSum i will be forever 0, so it will run forever. 首先,在smallestSum中,我将永远为0,因此它将永远运行。 I think you need to do i++. 我认为您需要做i ++。 But I would think you have more bugs in there. 但我认为您那里还有更多错误。 What input are you providing when running it ? 您在运行时提供什么输入?

one possible crash source is here: 一种可能的崩溃源在这里:

counter=i;

it should be: 它应该是:

counter=j;

j is keeping factors array index, not i . j是保持因子数组索引,而不是i Another crash source is here: 另一个崩溃源在这里:

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

Because you have counter element, it should be like this: 由于您具有counter元素,因此应如下所示:

while(i<counter)

smallestSum() has same problem too. smallestSum()也有同样的问题。 In addition yo have forgotten to increase i in the loop in smallestSum() . 此外,您还忘了在smallestSum()中的循环中增加i

I should say these are bugs that I found and there may be more that I'm missing. 我应该说这些是我发现的错误,可能还有更多我所缺少的错误。 But generally this code can be much better when you get more experience. 但是通常,当您有更多经验时,此代码会更好。

When you debug your program use debugger and breakpoints. 在调试程序时,请使用调试器和断点。 It also helps to print out intermediate results to see if the algorithm works properly. 它还有助于打印出中间结果,以查看算法是否正常工作。 Doing so you can notice that counter gets initialized to improper value all the time. 这样一来,您会注意到counter一直都在初始化为不正确的值。 You would notice that smallestSum() function never ends because the loop never breaks. 您会注意到smallestSum()函数永远不会结束,因为循环永远不会中断。 You need to increase i at the proper place inside that function. 您需要在该函数内的适当位置增加i Check if your condition while(i<counter+1) is correct, since loop starts from 0 comparing to counter is enough. 检查您的条件while(i<counter+1)是否正确,因为与counter相比,循环从0开始就足够了。

The program works after all those corrections: 经过所有这些更正后,该程序才能运行:

#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;
}

Test: 测试:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM