1

I've made a program for finding the average of a one dimensional array. It sort of works but my problem is the float subprogram doesn't return float in main.

void vnesi_niza(int n, int a[50])
{
    for (int i=0;i<n;i++)
    {
        cout << "a[" << i << "]=";
        cin > >a[i];
    }
}

float sreden_uspeh(int n, int a[50])
{
    int sum=0;
    float as;
    for (int i=0;i<n;i++)
    {
        sum+=a[i];
        as=sum/n;
    }
    return as;
}

int main()
{
    int n;
    int niza[50];
    cout << "input the number of elements" << endl;
    cout << "n=";
    cin >> n;
    vnesi_niza(n,niza);
    cout << endl;
    cout << "the average is " << endl;
    cout << sreden_uspeh(n,niza); //sreden_uspeh means average
    return 0;
}
AStopher
  • 4,207
  • 11
  • 50
  • 75
ptushev
  • 177
  • 3
  • 16
  • The answers below are okay. You should also consider moving the division outside of the for loop. You don't need this `as` value. You could just `return static_cast(sum)/n;` at the end of your loop. – Geoff Jun 17 '18 at 12:40
  • Your function *does* return a `float`. But `floats` can hold integer values (an `int` is a perfectly valid `float`) and what you are calculating and returning is a `int`. – Jesper Juhl Jun 17 '18 at 13:18

4 Answers4

3

Since the sum of the grades is of type int the division operation will be the integer division, that is it will floor down to the previous closest integer. So you should instead do a decimal division like this: as = (1.0 * sum) / n.

Note: You don't need to calculate the average value each time in the loop, it is enough to do it once out of the loop where you will return it.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • 1
    `as = (1.0 * sum) / n;` reminds me about my math prof. (who surely made his code in Fortran). `as = (float)sum / n;` would do as well. ;-) Btw. `1.0` is a `double` constant - `1.0f` the `float` companion of it. (Sorry for the nitpicking.) – Scheff's Cat Jun 17 '18 at 10:22
2

The line as=sum/n performs integer division, then assigns as equal to the result, so the sreden_uspeh function returns the average of the input array truncated (rounded down) to an integer.

If you declare the variable sum as float sum = 0 (instead of as int sum = 0) then n will be cast to a float and floating point division will happen with as=sum/n, as per conversions.

Or, you can just cast the first operand to a float when you do the division, as=(float)sum/n.

Also you are assigning as but not using it each iteration of the loop, which is inefficient. In fact you can just write

float sreden_uspeh(int n, int a[50])
{
    int sum=0;
    for (int i=0;i<n;i++)
    {
        sum+=a[i];
    }
    return (float)sum/n;
}

Also be aware int a[50] is decayed to the pointer type int *a; the full array is not passed to sreden_uspeh.

Also, it is common when you are performing integer division to also take the modulus (using the % operator) since taking both the integer quotient and modulus you don't lose any info.

jcarpenter2
  • 5,312
  • 4
  • 22
  • 49
0

You made a small mistake. The type of sum is int it should be float.

float sreden_uspeh(int n, int a[50])
{
    float sum=0;
    float as;
    for (int i=0;i<n;i++)
    {
        sum+=a[i];
        as=sum/n;
    }
    return as;
}

One more mistake in statement cin > >a[i]; .

shubham johar
  • 268
  • 1
  • 9
  • I tried it, it works. But why should it be a float? Isn't only the return value supposed to be a float, i mean when you divide an int you can still get a float. – ptushev Jun 17 '18 at 09:17
  • 2
    @1nightstamp: No. in C/C++ if yo divide an integer by another integer, the result again is an integer. – datenwolf Jun 17 '18 at 09:24
0

when you put int/int the answer gets calculated in int . after wards it get transformed to float,, so you have to make it float/int