Here, have a look at this version, and see if it does what you want.
Code:
#include <stdio.h>
int main(void)
{
int histogram[256], i, ch, max, min, tempsum;
long int size_of_file, sum;
FILE *fp;
sum = 0;
max = -1;
min = 257;
for (i = 0; i < 256; i++)
{
histogram[i] = 0;
}
fp = fopen("a.img", "rb");
if (fp == NULL)
{
perror("a.img");
return 1;
}
fseek(fp, 0L, SEEK_END);
size_of_file = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (size_of_file == 0)
{
printf("Empty file\n");
fclose(fp);
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
histogram[ch]++;
sum += ch;
if (ch > max) max = ch;
if (ch < min) min = ch;
}
fclose(fp);
for (i = 0, tempsum = 0; i < 256; i++)
{
tempsum += histogram[i];
if ((i + 1) % 32 == 0)
{
int j;
printf("%03d:%03d %5d ", i - 31, i, tempsum);
for (j = 0; j < tempsum; j++) putchar('*');
putchar('\n');
tempsum = 0;
}
}
printf("\n\nTotal pixel values ===>%d", sum);
printf("\n\nMaximum value of the pixel===>%d", max);
printf("\n\nMinimum value of the pixel===>%d", min);
printf("\n\nAverage====%d", sum / size_of_file);
return 0;
}