题
我必须找到一种在数组中显示最大数量和最小数字的方法,数组的大小为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
最终编辑:我自己解决了。我将最小/最大检查在主循环时将其放在主机外,这允许输入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);
}
解决方案
首先,您要分配 input
到返回值 scanf()
. 。这是呼叫分配的项目数,由于您说输入始终是正确的,因此此值将永远是 1
.
其次,您正在写过去 numbers[]
与该行的数组:
input = scanf("%d", &numbers[100]);
(你应该做 scanf("%d, &input)
而是分配 numbers[i]
输入循环。
最后,您无需重新计算 maxvalue
和 minvalue
通过迭代 numbers[]
循环的每一个迭代。相反,只需将它们与 input
并相应地分配它们。
希望这能使您走上正确的轨道。
其他提示
看来您的中心问题是您仅比较每个数字 minvalue
. 。可以决定是否替换电流是可以的 minvalue
, ,但显然没有告诉您关于每个元素与 maxvalue
.
另一个问题:从第一个元素初始化MinValue是有意义的,但是如果您在循环中进行操作。这只是使您先前的所有工作无效。
您也需要使用MaxValue进行相同的初始化。您应该将该数字初始化为第一个值。
您还应该决定在累积数据或完成时通过数据计算最小值和最大值。但是,您不想做的是通过每一个新元素循环通过过去的元素。这使您的程序二次时间复杂性无益。
最后,不要忍受crummy格式。调试始终涉及研究代码,您将希望它始终是完美的格式化,既可以专业,又可以促进阅读自己的作品。
您正在问两个问题,即有关最小 /最大计算的策略和循环的策略。不要(对您自己),但一次解决一个问题。所以首先把类似的东西
signed int input[] = { 8, -5 , /* some more values */ };
size_t const n = sizeof input/ sizeof input[0];
一开始,忘记了你的 scanf
问题。
然后将最小/最大检测包装在适当的循环指令中。
然后通过警告编译您的代码:例如 -Wall
为了 gcc
, ,但这对于您的编译器可能有所不同。
我的人告诉我一些事情:
test-numbers.c:21:警告:'MaxValue'可以在此功能测试名称中未经启示使用。C:22:警告:'Minvalue'可以在此功能中使用无限期化。
这告诉您,您在不考虑算法的起点时做了很大的错误。
我已经重新注册了您的代码,并用`/ * ...占位符... */替换了很多代码。 */
#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的情况以及Enetr 0时会发生什么。
暗示: maxvalue
和 minvalue
值永远不会改变。
另一个提示:几次 while()
行执行?
编辑 与示例运行
对于此示例运行,代码在左侧,发生的事情在左侧
printf("Enter the next array element>"); | scanf("%d", &input); | Enter 42 | while (input != 0) { | input is 42, so you do the loop numbers[i] = input; | numbers[0] = 42 i++; | i = 1 | if (input == 0) { | input != 0; skip placeholder /* ...PLACEHOLDER... */ | } | printf("Enter the next ...>"); | scanf("%d", &input); | enter 3 } | while (input != 0) { | input is 3 numbers[i] = input; | numbers[1] = 3 i++; | i = 2 | if (input == 0) { | input != 0; skip placeholder /* ...PLACEHOLDER... */ | } | printf("Enter the next ...>"); | scanf("%d", &input); | enter 0 } | while (input != 0) { | input is 0, skip while body /* ...PLACEHOLDER... */ | } | printf("%d\t", maxvalue); | maxvalue hasn't been initialized printf("%d", minvalue); | minvalue hasn't been changed
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]);