简体   繁体   English

C 最小函数总是返回 2686916

[英]C minimum function always returning 2686916

I am doing a simple function that returns the minimum integer from numbers given from the user(array).我正在做一个简单的函数,它从用户(数组)给出的数字中返回最小整数。

However, it always print 2686916 at the end.但是,它总是在最后打印2686916 Here is my code:这是我的代码:

int function()
{
    int ar[100];
    int i;
    int smallest = INT_MAX;
    int nums;
    int num;
    int sum=0;
    printf("\nenter array size\n");

    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
        scanf("%d",&ar[i]);
        sum=sum+ar[i];
    }
    if (nums <smallest){
        smallest=nums;
        printf("the smallest %d\n,smallest);
        return 0;
    }
}

I'm not sure what I'm doing wrong.我不确定我做错了什么。

My friend, it seems you are new to C, and before you ask questions like this one you should try to follow some tutorials for C. You might try something like this .我的朋友,看来你是新的C,你问这样的一个问题之前,你应该尽量遵循一些教程C.你可以尝试像这样

If the question you ask is not clear or the code you post won't compile anyway it is very hard to help you out.如果您提出的问题不清楚,或者您发布的代码无论如何都无法编译,则很难为您提供帮助。 For now this is all I can do:目前我能做的只有这些:

int function()
{
   int ar[100];
   int i;
   int smallest = INT_MAX;
   int nums = 0; //Always Initialize your variables!
   int num = 0;
   int sum= 0;
   printf("\nenter array size\n");

  scanf("%d",&num);
  for(i=0;i<num;i++)
  {
    scanf("%d",&ar[i]);
    sum=sum+ar[i];
  }
  if (nums <smallest)
  {
    smallest=nums;
    printf("the smallest %d\n",smallest);
  }
    return 0; //Don't put this in a place that might not be executed!
}

Now it should at least compile, it still doesn't do anything useful as far as I can see.现在它至少应该编译,就我所见,它仍然没有做任何有用的事情。 You compare "nums", a variable you didn't use before, with the biggest value of an int, set it to the never used "nums" and print it.您将之前未使用的变量“nums”与 int 的最大值进行比较,将其设置为从未使用过的“nums”并打印它。

You might want use "sums" or "ar[i]" in the if statement instead, and printing one of these values.(still not 100% sure what you want to do).您可能希望在 if 语句中使用“sums”或“ar[i]”,并打印这些值之一。(仍然不是 100% 确定您想要做什么)。

Some tips for next time (before you ask a question!):下次的一些提示(在你提问之前!):

  • Variables should always be initialized变量应该总是被初始化

In your code you try to use the value of "nums" before it gets a value, this might cause errors or strange results in your code.在您的代码中,您尝试在获得值之前使用“nums”的值,这可能会导致代码中出现错误或奇怪的结果。

  • Don't put a return in a place that might be skipped,不要把 return 放在可能会被跳过的地方,

In your code, "nums" would be bigger than "smallest" (unlikely, bit for example), the code would skip the if statement and never reach the return.在您的代码中,“nums”将大于“smallest”(不太可能,例如位),代码将跳过 if 语句并且永远不会到达返回。

  • Read your compiler warnings阅读编译器警告

The code you posted can't compile, read your errors and warnings, and fix them.您发布的代码无法编译、读取错误和警告并修复它们。

  • (tip) Use better variable names, using names like nums, num and sum make it easy to overlook a mistake. (提示)使用更好的变量名称,使用像 nums、num 和 sum 这样的名称可以很容易地忽略错误。

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

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