使用动态内存分配C程序查找区间中的最大值和最小值

Finding maximum and minimum in inteval using dynamic memory allocation C program

您好,我正在尝试使用动态数组查找给定区间内的最大值和最小值。它可以找到最大值和最小值,但不是在给定的数字区间内。我哪里错了?请帮忙。

    #ifdef _MSC_VER
    #define _CRT_SECURE_NO_WARNINGS
    #endif

    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>

    int main(){
        int i, n;
        int *bliznak;
        int max = 0, min = 0;
        int a = 0;
        int b = 0;
        int p = 0;
        printf("Give upper and lower limit");
        scanf("%d %d", &a, &b);

        printf("enter elements");
        scanf("%d", &n);
        bliznak = (int*)calloc(n, sizeof(int));
if (bliznak==NULL)
{printf("ERROR"); exit(0);}
        for (i = 0; i < n; i++)
        {

            printf("enter number %d    ", i + 1);
            scanf("%d", bliznak + i);

        }




        for (i > a; i < b; i++){
        max = *(bliznak + i);
        min = *(bliznak + i);

            for (i = 1; i < n; i++)
            {

                {

                    if (*(bliznak + i)> max)
                    max = *(bliznak + i);


                    if (*(bliznak + i) < min)
                        min = *(bliznak);
                }
            }

    }   
        printf("\n MAX Numb %d\n", max);
        printf("\n Min Numb %d\n", min);

        system("pause");
        return 0;
    }

删除 for 循环后的分号:并改为添加块。

for (i > a; i < b; i++);---->

 for(i>a;i<b;i++){}

因为您正在检查间隔 a 和 b

这是您的代码的固定版本

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

int main(){
    int i, n;
    int *bliznak;
    int max = 0, min = 0;
    int a = 0;
    int b = 0;
    int p = 0;
    printf("Give lower and upper limit");
    scanf("%d%d", &a, &b);                 

    printf("enter elements");
    scanf("%d", &n);
    bliznak = (int*)calloc(n, sizeof(int));
    for (i = 0; i < n; i++)
    {
        printf("enter number %d    ", i + 1);
        scanf("%d", bliznak + i);
    }


  for (i = 0; i < n; i++)         // changed loop condition
  {
      if( ( *(bliznak + i) ) >= a && ( *(bliznak + i) ) <= b )
      {     
         max = *(bliznak);
         min = *(bliznak);
         break;
      }
  }

        for (i = 0; i < n; i++)
        {
                if( ( *(bliznak + i) ) >= a && ( *(bliznak + i) ) <= b )  // this checks the interval
                {

                  if (*(bliznak + i)> max)
                    max = *(bliznak + i);


                  if (*(bliznak + i) < min)
                    min = *(bliznak + i);          // you forgot + i
                }

        }


    printf("\n MAX Numb %d\n", max);
    printf("\n Min Numb %d\n", min);

    system("pause");
    return 0;
}

现在让我们循环解决问题

for (i > a; i < b; i++){

那个循环是错误的。您正在检查索引,而不是值。你应该把它改成

for (i = 0; i < n; i++)         // changed loop condition
 {
    if( ( *(bliznak + i) ) >= a && ( *(bliznak + i) ) <= b )
    {   
      max = *(bliznak);
      min = *(bliznak);
      break;
    }
}

然后,在

if (*(bliznak + i) < min)
     min = *(bliznak);

您忘记了+ i。应该是

min = *( bliznak + i );

要检查间隔,请使用此

if( ( *(bliznak + i) ) >= a && ( *(bliznak + i) ) <= b )

如上代码所示。