为什么我的作业出现逻辑错误?

发布于 2024-07-11 10:20:12 字数 1588 浏览 6 评论 0原文

我是一名IT专业的大三学生。 我面临程序输出的问题。 该程序的想法是,我应该使用函数读取 10 个元素的数组,然后获取元素的平均值,然后获取最大值和最小值。 我的最大值和最小值是正确的,但平均值显示出奇怪的东西。 请检查代码并告诉我应该做什么或以某种方式帮助我。

输出是(请注意,请求的是 11 个数字而不是 10 个数字,如果我更改循环参数以使其只需要 10 个数字,那么它会

enter the group of integers numbers
1
2
3
4
5
6
7
8
9
0
9
 1 2 3 4 5 6 7 8 9 0the avg is 3.500000
9
1Press any key to continue . . .

// func-sortarray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define size  10

void readarray(int []);
void average(int []);
void printArray(int []);
void max(int []);
void min(int []);

int _tmain(int argc, _TCHAR* argv[])
{
    int sarray[size];
    readarray(sarray);
    printArray(sarray);
    average(sarray);
    max(sarray);
    min(sarray);
    return 0;
}

void readarray(int a[])
{
    printf("enter the group of integers numbers\n");
    for (int i=0; i<=size-1 ;i++)
        scanf("%d\n",&a[i]);
}

void average(int a[])
{
    int i;
    double avg;
    double total = 0;
    for (i=0; i <= size-1; i++)
    {
        total = total + a[i];
    }

    avg = total /size-1;
    printf("the avg is %f\n",avg);
}

void printArray(int a[])
{
    int j;
    for (j = 0; j <= size - 1; j++) 
        printf( "%2d", a[ j ]);
}

void max(int a[])
{
    int ma =a[0];

    for (int j=0;j<size-1;j++)
    {
        if (ma<a[j])
            ma=a[j];
    }
    printf("%d",ma);
}

void min(int a[])
{
    int mi =a[0];

    for (int j=0;j<size-1;j++)
    {
        if (mi>a[j])
            mi=a[j];
    }
    printf("\n%d",mi);
}

提前显示奇怪的内容

I'm a junior student in IT. I am facing a problem with the output of the program.
The idea of the program is that I should use functions to read an array of 10 elements, then get the average of the elements, then get the the max and min. I've got the max and min right but the average is showing weird stuff. Please check the code and tell me what should I do or help me in some way or another.

The out put is (notice that is requesting for 11 numbers instead of 10 and if I change the loop parameters to let it take only 10 then it's showing weird stuff

enter the group of integers numbers
1
2
3
4
5
6
7
8
9
0
9
 1 2 3 4 5 6 7 8 9 0the avg is 3.500000
9
1Press any key to continue . . .

// func-sortarray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define size  10

void readarray(int []);
void average(int []);
void printArray(int []);
void max(int []);
void min(int []);

int _tmain(int argc, _TCHAR* argv[])
{
    int sarray[size];
    readarray(sarray);
    printArray(sarray);
    average(sarray);
    max(sarray);
    min(sarray);
    return 0;
}

void readarray(int a[])
{
    printf("enter the group of integers numbers\n");
    for (int i=0; i<=size-1 ;i++)
        scanf("%d\n",&a[i]);
}

void average(int a[])
{
    int i;
    double avg;
    double total = 0;
    for (i=0; i <= size-1; i++)
    {
        total = total + a[i];
    }

    avg = total /size-1;
    printf("the avg is %f\n",avg);
}

void printArray(int a[])
{
    int j;
    for (j = 0; j <= size - 1; j++) 
        printf( "%2d", a[ j ]);
}

void max(int a[])
{
    int ma =a[0];

    for (int j=0;j<size-1;j++)
    {
        if (ma<a[j])
            ma=a[j];
    }
    printf("%d",ma);
}

void min(int a[])
{
    int mi =a[0];

    for (int j=0;j<size-1;j++)
    {
        if (mi>a[j])
            mi=a[j];
    }
    printf("\n%d",mi);
}

thanx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

享受孤独 2024-07-18 10:20:12

您在从零开始计数先乘后加规则方面遇到了一些问题。 好吧,让我们先开始吧。 最常见的是,当你从 0 开始计数时,你会这样做:

for(i=0; i < count; i++)
    /* ... */;

如果你从 1 开始计数,你会这样做:

for(i=1; i <= count; i++)
    /* ... */

如果你混合这些,你会得到相同的结果,但这可能会让你自己和其他阅读的人感到困惑。代码:

for(i=0; i <= count-1; i++) /* same, but better don't do this */
    /* ... */;

在计算平均值的代码中,有两个错误。 首先,由于数学原因,您应该使用括号:

avg = total / (size-1); /* there is still one bug */

其次,您有 size 元素。 因此,您必须除以 size,而不是除以 size-1

avg = total / size; /* right ! */

You've got some problems with counting starting with zero and multiply before addition rules. Well, let's get on the first. Most commonly, when you start counting from zero, you do it like this:

for(i=0; i < count; i++)
    /* ... */;

If you start counting from 1, you do it like this:

for(i=1; i <= count; i++)
    /* ... */

If you mix those, you get the same result, but it can confuse yourself, and others reading the code:

for(i=0; i <= count-1; i++) /* same, but better don't do this */
    /* ... */;

In the code calculating the average, you had two bugs. First, you should use parentheses, because of math:

avg = total / (size-1); /* there is still one bug */

And second, you have size elements. So you have to divide by size, and not by size-1:

avg = total / size; /* right ! */
何止钟意 2024-07-18 10:20:12

这行可能是问题所在:

avg = total /size-1;

您可能想要改为:

avg = total / size;

另外,您的 max() 和 min() 函数有一个像这样的循环:

for (int j=0;j<size-1;j++)

这可能会少检查一个数量超出您的预期。

上述类型的错误通常称为“栅栏错误”。 这个名字涉及到以下问题:如果你想建一个100m长的栅栏,并且每隔1m需要一个栅栏柱,那么你需要多少个栅栏柱? 答案不是 100,而是 101。此类错误的另一个名称是“相差一个错误”。

This line is probably the problem:

avg = total /size-1;

You probably want to instead:

avg = total / size;

Also, your max() and min() functions have a loop like this:

for (int j=0;j<size-1;j++)

This is probably checking one fewer number than you intend.

The above kinds of errors are commonly called "fencepost errors". The name relates to the following question: If you want to build a fence 100 m long, and you want a fencepost every 1 m, how many fenceposts do you need? The answer is not 100 but 101. Another name for this type of error is an "off by one error".

夕嗳→ 2024-07-18 10:20:12

由于 scanf() 字符串中的“\n”,它会跳过第一个输入数字。 要修复此问题,请从 scanf 字符串中删除 '\n',如下所示:

scanf("%d", &a[i]);

It is skipping the first input number because of the '\n' in the scanf() string. To fix it, remove the '\n' from the scanf string, like this:

scanf("%d", &a[i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文