Question

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

Was it helpful?

Solution

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]);

OTHER TIPS

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 ! */

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top