#include <stdio.h>
#define ARRAY_SIZE 100000
int input_sounds(int sound[ARRAY_SIZE])
{
int number_of_sound = 0;
char text[ARRAY_SIZE * 10];
int i;
printf("Please enter the number of sound values, followed by sound values, delimited by spaces or linebreaks:\n");
scanf("%d", &number_of_sound);
for (i = 0; i < number_of_sound; ++i)
scanf("%d", &sound[i]);
return number_of_sound;
}
unsigned long long calculate_total_sound(int sound[ARRAY_SIZE], int number_of_sound)
{
unsigned long long total_sound = 0;
int i, j;
for (i = 0; i < number_of_sound; ++i)
for (j = 0; j < number_of_sound; ++j)
if (i != j)
{
int sound_to_hear = sound[i] - sound[j];
if (sound_to_hear < 0)
sound_to_hear = -sound_to_hear;
total_sound += sound_to_hear;
}
return total_sound;
}
int main()
{
int sound[ARRAY_SIZE];
int number_of_sound = 0;
unsigned long long total_sound = 0;
number_of_sound = input_sounds(sound);
total_sound = calculate_total_sound(sound, number_of_sound);
printf("The total sound is %lld\n", total_sound);
return 0;
}
// Output:
Please enter the number of sound values, followed by sound values, delimited by spaces or linebreaks:
5 1 5 3 2 4
The total sound is 40