-1
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int i=0;
    int ray[20];
    float sum=0.00, average;

    for (i=0; i<20; i++)
    {
        printf("Enter integer #%d",i+1);
        scanf ("%d", &ray[i]);
        sum=(sum+ray[i]);
    }

    average=(sum/20);
    printf("Average = %.2f", average);

    if (ray[i] < average)
    {
        printf("The followiing values are less than the average: %d",     ray[i]);
    }
    system("pause");
}

The code runs fine and gives the correct average of the integers entered but the values that are less than the average comes out as -858993460

DaftTommy
  • 1
  • 1

4 Answers4

2

You are trying to print out ray[i], but i is currently 20which is outside the index of your array. Did you mean to copy your for loop around that if statement?

Suraj Jain
  • 4,463
  • 28
  • 39
odin
  • 392
  • 1
  • 3
  • 11
0

This Part in Your Code:

for (i=0; i<20; i++)
{
    printf("Enter integer #%d",i+1);
    scanf ("%d", &ray[i]);
    sum=(sum+ray[i]);
}
                               //Now i = 20,that is why it left loop
average=(sum/20);
printf("Average = %.2f", average);

if (ray[i] < average)          //You are saying if ray[20]<average
  {
    printf("The following values are less than the average: %d", ray[i]);
    }

Now ray[20] is outside the scope because there are 20 elements and array index starts from 0 and goes on to 19, so ray[20] is out of bound access. I highly recommend you to see this question for better understaning How dangerous is it to access an array out of bounds?.

Secondly You want to print all ray[i] where ray[i] < average so you should run a loop like

   printf("The following values are less than the average:");

   for(i = 0; i<20; i++)  // i starts from 0 and increase with run of loop 
                          // and loop stops when i>19 so you are not
                          // accessing forbidden area.
    {
         if (ray[i] < average){   //check if ray[i] is less than average
           printf("%d\n", ray[i]) 
         }
    }

All That makes :

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int i=0;
    int ray[20];
    float sum=0.00, average;

    for (i=0; i<20; i++)
    {
        printf("Enter integer #%d",i+1);
        scanf ("%d", &ray[i]);
        sum=(sum+ray[i]);
    }

    average=(sum/20);
    printf("Average = %.2f", average);

    printf("The following values are less than the average:");

    for(i = 0; i<20; i++)  // i starts from 0 and increase with run of loop 
                           // and loop stops when i>19 so you are not
                           // accessing forbidden area.
   {
     if (ray[i] < average){   //check if ray[i] is less than average
           printf("%d\n", ray[i]);
           }

}
    system("pause");
}
Community
  • 1
  • 1
Suraj Jain
  • 4,463
  • 28
  • 39
0
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int i=0;
    int ray[20];
    float sum=0.00, average;

    for (i=0; i<20; i++)
    {
        printf("Enter integer #%d: ",i+1);
        scanf ("%d", &ray[i]);
        sum=(sum+ray[i]);

        average=(sum/20);


            if (ray[i] < average)
                {
                printf("The followiing values are less than the average: %d", ray[i]);
                }
    }

    printf("Average = %.2f", average);

system("pause");
}

This gives the "the following integers are below the average" part after each value that is lower than the then average, but I need it to show the values which are below the average together at the end.

DaftTommy
  • 1
  • 1
0

The out-of-index issue

As the others have already pointed, your index is out of bounds. That happens because you've iterated for (i=0; i < 20; i++), which means once you left the for statement you were at i == 20. Your array was allocated with 20 positions, so you can access it from index 0 to 19. The awkward value you get is given because you are accessing "trash", or in other words, invalid positions with unpredictable (or almost) values.

The algorithm issue

Ok, once you got that index thing right, you still need an algorithm that displays the numbers that are lower than the average. You can't just copy your if statement into the loop because you only know the true average value once you've iterated through all of the values (which you are doing just fine).

So what you want is another loop that iterates throughout the array and that if statement inside of it (well, there are other ways to do it without running all of the values again, like sorting the array)

So, this is the algorithm I'll propose you

#include <stdio.h>
#include <stdlib.h>

#define array_size 20

void main()
{
    int i;
    int ray[array_size];
    int sum=0;
    float average;

    for (i=0; i<array_size; i++)
    {
        printf("Enter integer #%d: ",i+1);
        scanf ("%d", &ray[i]);

        sum += ray[i];
    }

    average=(sum/(float)array_size);
    printf("Average = %.2f\n", average);

    printf("The following values are less than the average: ");

    for (i = 0; i < array_size; i++)
    {
        if (ray[i] < average)
        {
            printf("%d ", ray[i]);
        }
    }

    system("pause");
}
Eduardo Pacheco
  • 504
  • 4
  • 14