1

我必须找到一种方法来显示数组中的最大值和最小值,数组的大小为 100 并且不会超过该值,并且不需要输入验证。程序将一直要求输入,直到遇到 0,它也会被添加到数组中。

除了如何跟踪最大值和最小值之外,我已经弄清楚了一切。如果有人可以修复我的代码或向我展示,我将不胜感激。当输入等于 0 时,我遇到的另一个问题是让循环终止并在 while 循环内进行最大/最小计算。

/*
 ============================================================================
 Name        : test.c
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#define n  100
int main(void){


 int numbers[n];
 int i = 1;
 int j;
        int input;
 int maxvalue;
 int minvalue;

   printf("Enter the next array element>");

input = scanf("%d", &numbers[100]);



while (input != 0){

  numbers[i] = input;
  i++;
  printf("Enter the next array element, while loop>");
  input = scanf("%d", &numbers[n]);
  if (input == 0){
printf("Enter the next array element, if loop");
   numbers[i] = 0;

   for (j =2;j <= i; j++){
    minvalue = numbers[1];

    j++;
    if (numbers[j] > minvalue){
     maxvalue = numbers[j] ;
    }
    else{
     minvalue = numbers[j] ;
    }

   }


  }
 }


printf("%f\t", maxvalue);

printf("%f\n", minvalue); 
 }

编辑:我删除了您的所有建议并编辑了我的代码。这是我下面的代码。但是,它的输出不是我所期望的。

#include <stdio.h>
#include <stdlib.h>
#define N  100
int main(void){


    int numbers[N];
    int i = 0;
    int j;
        int input;
    int maxvalue;
    int minvalue;

            printf("Enter the next array element>");

scanf("%d", &input);



while (input != 0){

        numbers[i] = input;
        i++;

        if (input == 0){
                   i++;
            numbers[i] = 0;
                        minvalue = numbers[0];
                        maxvalue = numbers[0];
                        for (j=0;j<=i-1;j++){

                            if (minvalue >= numbers[j]){
                                minvalue = numbers[j];
                            }else if (maxvalue <= numbers[j]){
                                maxvalue = numbers[j];
                            }


                        }

/* min = value of first array element
max = value of first array element

begin loop for each array element, index = 0 to (n-1)

--- if array element value is less than min, set min to this value
--- if array element value is more than max, set max to this value

increment index and repeat loop til last index is completed

average = sum / number of elements (n).
max and min will hold their correct values.*/




        }
                printf("Enter the next array element, while loop>");
    scanf("%d", &input);
    }


printf("%d\t", maxvalue);
printf("%d", minvalue);
    }

这是输出,我得到了!有人可以为我解决这个问题。

Enter the next array element>1
Enter the next array element, while loop>2
Enter the next array element, while loop>3
Enter the next array element, while loop>0
12190144 l6Press [Enter] to close the terminal

最后编辑:我自己解决了这个问题。我将最小/最大检查放在主 WHILE 循环之外,这允许将 0 的输入输入到数组中。

#include <stdio.h>
#include <stdlib.h>
#define N  100
int main(void){


    int numbers[N];
    int i = 0;
    int j;
        int input;
    int maxvalue =1;
    int minvalue = 1;
            printf("Enter the next array element>");

scanf("%d", &input);
minvalue = input;
maxvalue = input;



while (input != 0){
    numbers[i] = input;

    ++i;
                printf("Enter the next array element>");
    scanf("%d", &input);

if (input == 0){
numbers[i] = 0;
  ++i;

  }

}
for (j =0;j<i;j++){
 if (numbers[j] >= maxvalue){
                                maxvalue = numbers[j];
                            }
                            if(numbers[j] < minvalue){
                                minvalue = numbers[j];
                            }

}

printf("%d\t", maxvalue);
printf("%d\n", minvalue);

    }
4

5 回答 5

2

首先,您要input分配scanf(). 这是调用分配的项目数,并且由于您说输入将始终正确,因此该值将始终为1

其次,您正在使用以下行写到numbers[]数组的末尾:

input = scanf("%d", &numbers[100]);

(您应该这样做scanf("%d, &input),并分配numbers[i]给循环中的输入。

最后,您不需要重新计算maxvalueminvalue遍历循环numbers[]的每次迭代。相反,只需将它们与它们进行比较并input相应地分配它们。

希望这能让你走上正确的轨道。

于 2010-09-26T07:23:32.500 回答
2

看起来您的核心问题是您仅将每个数字与minvalue. 这对于决定是否替换 current 很好minvalue,但显然它并没有告诉你任何关于每个元素与maxvalue.

另一个问题:从第一个元素初始化 minvalue 是有意义的,但如果你在循环中这样做就不行。这只会使您之前的所有工作无效。

您还需要对 maxvalue 进行相同的初始化。您应该将该数字初始化为第一个值。

您还应该在累积数据时或在完成数据时计算最小值和最大值。但是,您不想做的是将过去的元素与每个新元素一起循环。这给您的程序带来了二次时间复杂度而没有任何好处。

最后,不要容忍糟糕的格式。调试总是涉及到对代码的研究,你会希望它的格式总是完美的,既要专业,又便于阅读你自己的工作。

于 2010-09-26T07:29:17.607 回答
2

您在问两个问题,关于最小/最大计算和循环的策略。不要这样做(对自己),而是一次解决一个问题。所以首先放一些类似的东西

signed int input[] = { 8, -5 , /* some more values */ };
size_t const n = sizeof input/ sizeof input[0];

一开始就忘记你的scanf问题。

然后将您的最小/最大检测包装在适当的循环指令中。

然后编译带有警告的代码:例如-Wallfor gcc,但这可能因您的编译器而异。

我的告诉我一些事情:

test-numbers.c:21: 警告: 'maxvalue' 可能在此函数中未初始化使用 test-numbers.c:22: 警告: 'minvalue' 可能在此函数中未初始化使用

这告诉您,您没有很好地考虑算法的起点,这是非常错误的。

于 2010-09-26T07:40:17.747 回答
0

我重新缩进了你的代码,并用 `/* ...PLACEHOLDER... */

#include <stdio.h>
#include <stdlib.h>
#define N  100
int main(void) {
    int numbers[N];
    int i = 0;
    int input;
    int maxvalue;
    int minvalue;

    printf("Enter the next array element>");
    scanf("%d", &input);

    while (input != 0) {
        numbers[i] = input;
        i++;

        if (input == 0) {
            /* ...PLACEHOLDER... */
        }
        printf("Enter the next array element, while loop>");
        scanf("%d", &input);
    }
    printf("%d\t", maxvalue);
    printf("%d", minvalue);
}

希望您能看到输入 1、2 或 3 以及输入 0 时会发生什么。

提示:maxvalue并且minvalue值永远不会改变。

while()另一个提示:该行执行了多少次?


使用示例运行进行编辑

对于此示例运行,代码在左侧,发生的情况在左侧

        printf("输入下一个数组元素>"); |
        scanf("%d", &input); | 输入 42
                                                 |
        而(输入!= 0){| 输入是 42,所以你做循环
            数字[i] = 输入;| 数字[0] = 42
            我++;| 我 = 1
                                                 |
            如果(输入 == 0){ | 输入!= 0;跳过占位符
                /* ...占位符... */ |
            } |
            printf("输入下一个...>"); |
            scanf("%d", &input); | 输入 3
        } |
        而(输入!= 0){| 输入为 3
            数字[i] = 输入;| 数字[1] = 3
            我++;| 我 = 2
                                                 |
            如果(输入 == 0){ | 输入!= 0;跳过占位符
                /* ...占位符... */ |
            } |
            printf("输入下一个...>"); |
            scanf("%d", &input); | 输入 0
        } |
        而(输入!= 0){| 输入为0,body跳过
            /* ...占位符... */ |
        } |
        printf("%d\t", 最大值); | 最大值尚未初始化
        printf("%d", 最小值); | 最小值没有改变
于 2010-09-27T00:20:55.570 回答
-3
int cmp(const void *a,const void *b)
{
  return *(const int*)a-*(const int*)b;
}
...
qsort( numbers, 100, sizeof(numbers[0]), cmp );
printf("\nmin: %d\nmax: %d",numbers[0],numbers[99]);
于 2010-09-26T07:26:33.517 回答