#include <stdio.h>
main()
{
int choice, no;
printf("1. Show sum of odd/even number to N termn");
printf("2. Smallest, largest and average of the supplied numbersn");
printf("3. Terminate the programsnn");
printf("Enter your choice[1|2|3]: ");
scanf("%d", &choice);
if (choice == 1)
{
int i , no , sum = 0, j, sum2 = 0;
printf("nEnter any number: ");
scanf("%d", &no);
for (i = 2; i <= no; i = i + 2)
{
sum = sum + i;
}
printf("nSum of all even number between 1 to %d = %dn", no, sum);
for (j = 1; j <= no; j = j + 2)
{
sum2 = sum2 + j;
}
printf("Sum of all odd number between 1 to %d = %dn", no, sum2);
}
else if(choice == 2)
{
float max, min, avg, num,counter=0, sum = 1;
printf("nPlease enter all the number you want![0 to end]: ");
scanf("%f", &num);
max = min = num;
while (num != 0)
{
printf("Please enter all the number you want![0 to end]: ");
scanf("%f", &num);
if (max < num && num > 0)
max = num;
else if (min > num && num > 0)
min = num;
sum = sum + num;
counter++;
}
printf("nThe smallest and largest of entered numbers are %.2f and %.2f respectively.n", min, max);
avg = sum / counter;
printf("The sum of entered number is %.2fn", sum);
printf("The average of entered number is %.2fn", avg);
}
}
我的问题是,当我选择2号时,它将显示最小,最大的数字,但是当我输入大量200的总和时,总和显示错误!但是当我输入小价值时,它可以正常工作!?
少量大数
图片包括
您的总和从未计算第一个输入。具有初始值sum = 1,
对于您的小数:您的总数=(1 1 1 2)恰好是正确的。但是对于您的数字:您的总数=(1 100 100 200)= 400.1(您可以看到您错过了第一个输入100);
您的错误:
-
总和应将其初始化为0;
-
您没有计算第一个输入(循环之前):不计算总和
-
当用户最终输入0时,您不应继续
counter++
,因为'0'不是有效的输入。
您的程序有几个问题:
- 您将总和至1个,而不是应为0。
- 您以不同的方式处理第一个值和后续值。这基本上可以,但是请确保在BothZ情况下治疗相同。在您的代码中,您正确地为第一个值分配了最小值和最大值,但错过了增加总和和计数器。
- 您的代码未检查已输入有效的浮点。这意味着如果用户输入不是浮子的东西,它将悬挂。您的程序应处理输入,好像是零。
- 从理论上讲,当它为零时,您不应除以
counter
。实际上,这不会发生,因为您还考虑了柜台中的终止零。
也许最好像所有其他值一样对待第一个值。然后,您可以初始化的最小值和最大值到大小值(例如,<float.h>
的FLT_MAX
),也可以在循环内检查count == 0
以实现第一个和以下值的不同行为。
在这种情况下,当给出无效输入或零时,您可以从无限环中 break
。这似乎很复杂,但导致更简单的代码:
#include <stdio.h>
#include <float.h>
int main(void)
{
float max = -FLT_MAX; // minimum possible float value
float min = FLT_MAX; // maximum possible float value
float sum = 0.0f;
int count = 0;
for (;;) {
float num;
printf("Please enter all the number you want![0 to end]: ");
if (scanf("%f", &num) < 1 || num == 0) break;
if (max < num) max = num;
if (min > num) min = num;
sum += num;
count++;
}
if (count) {
float avg = sum / count;
printf("%d valuesn", count);
printf("Smallest: %.2fn", min);
printf("Largest: %.2fn", max);
printf("Sum: %.2fn", sum);
printf("Average: %.2fn", avg);
}
return 0;
}
#include <stdio.h>
main()
{
int choice = 0;
for (;choice != 3;)
{
printf("_____________________________________________________________nn");
printf("1. Show sum of odd/even number to N termn");
printf("2. Smallest, largest and average of the supplied numbersn");
printf("3. Terminate the programsnn");
printf("Enter your choice[1|2|3]: ");
scanf("%d", &choice);
printf("_____________________________________________________________nn");
if (choice == 1)
{
int i, no, sumc1 = 0, j, sum2c1 = 0;
printf("nEnter any number: ");
scanf("%d", &no);
for (i = 2; i <= no; i = i + 2)
{
sumc1 = sumc1 + i;
}
printf("nSum of all even number between 1 to %d = %dn", no, sumc1);
for (j = 1; j <= no; j = j + 2)
{
sum2c1 = sum2c1 + j;
}
printf("Sum of all odd number between 1 to %d = %dnnn", no, sum2c1);
}
else if (choice == 2)
{
float counter, num, large, small, num2, sum = 0, avg;
printf("nEnter first number[Enter 0 to stop]: ");
scanf("%f", &num);
num2 = num;
large = num;
small = num;
for (counter = 0; num != 0; counter++)
{
printf("Enter another number [Enter 0 to stop]: ");
scanf("%f", &num);
if (num > large && num > 0)
large = num;
if (num<small && num > 0)
small = num;
sum = sum + num;
}
sum = sum + num2;
avg = sum / counter;
printf("nThe largest number is %.2fn", large);
printf("The smallest number is %.2fn", small);
printf("The sum of entered numbers are %.2fn", sum);
printf("The average of entered number are %.2fnnn", avg);
}
}
}
我只是想出了所有的问题,尽管有些答复给了我完整的代码,但我必须使用简单的代码,因为我只是开始学习基本的代码。谢谢。
此代码可能对某人有用。
//uniten.encik//