Skip to content

Instantly share code, notes, and snippets.

@KeitetsuWorks
Created November 20, 2018 16:46
Show Gist options
  • Select an option

  • Save KeitetsuWorks/c7973e416caa7f351b9bc13111dd7e76 to your computer and use it in GitHub Desktop.

Select an option

Save KeitetsuWorks/c7973e416caa7f351b9bc13111dd7e76 to your computer and use it in GitHub Desktop.
/**
* @file q13199335908.c
* @brief q13199335908
* @author Keitetsu
* @date 2018/11/20
* @copyright Copyright (c) 2018 Keitetsu
* @par License
* This software is released under the MIT License.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#define INPUT_MAX 20
void get_max_min(int *array, int n, int *max, int *min);
void calc_stddev(int *array, int n, double mean, double *stddev);
int main(void)
{
int num[INPUT_MAX];
int num_temp;
int count;
int i;
int max;
int min;
int sum; /* 合計 */
double mean; /* 平均 */
double stddev; /* 標準偏差 */
count = 0;
memset(num, 0, INPUT_MAX);
while (count < INPUT_MAX) {
printf("num[%d] ", count);
scanf("%d", &num_temp);
if (num_temp == 0) {
break;
}
num[count] = num_temp;
count++;
}
printf("count = %d\n", count);
for(i = 0; i < count; i++) {
printf("num[%d] = %d\n", i, num[i]);
}
get_max_min(num, count, &max, &min);
printf("max = %d\n", max);
printf("min = %d\n", min);
sum = 0;
for (i = 0; i < count; i++) {
sum += num[i];
}
printf("sum = %d\n", sum);
mean = (double)sum / count;
printf("mean = %lf\n", mean);
calc_stddev(num, count, mean, &stddev);
printf("stddev = %lf\n", stddev);
return 0;
}
void get_max_min(int *array, int n, int *max, int *min)
{
int max_temp;
int min_temp;
int i;
max_temp = array[0];
min_temp = array[0];
for(i = 0; i < n; i++) {
if (max_temp < array[i]) {
max_temp = array[i];
}
if (min_temp > array[i]) {
min_temp = array[i];
}
}
*max = max_temp;
*min = min_temp;
return;
}
void calc_stddev(int *array, int n, double mean, double *stddev)
{
int i;
double var;
var = 0.0;
for (i = 0; i < n; i++) {
var += ((array[i] - mean) * (array[i] - mean));
}
*stddev = sqrt(var / n);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment