Created
May 21, 2016 17:09
-
-
Save shimastripe/6ba7a58d4dfe80884f0e7d3660ab1577 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int sum(int*, int); | |
int ave(int*, int); | |
int min(int*, int); | |
int max(int*, int); | |
int main(void){ | |
int n; | |
int* num; | |
printf("n="); | |
scanf("%d", &n); | |
num = (int *)malloc(sizeof(int)*n); // numのサイズ分メモリを確保 | |
if(num == NULL){ | |
printf("Cannot allocate memory.\n"); | |
return 1; | |
} | |
for(int i=0;i<n;i++){ | |
printf("%d個目:", i); | |
scanf("%d", &num[i]); | |
} | |
printf("合計数は:%dです。\n", sum(num, n)); | |
printf("平均値は:%dです。\n", ave(num, n)); | |
printf("最小値は:%dです。\n", min(num, n)); | |
printf("最大値は:%dです。\n", max(num, n)); | |
free(num); | |
return 0; | |
} | |
int sum(int* num, int n){ | |
int total=0; | |
for(int i=0;i<n;i++){ | |
total += num[i]; | |
} | |
return total; | |
} | |
int ave(int* num, int n){ | |
return sum(num, n)/n; | |
} | |
int min(int* num, int n){ | |
int min = *num; | |
for(int i=1;i<n;i++){ | |
if(min > *(num+i)){ | |
min = *(num+i); | |
} | |
} | |
return min; | |
} | |
int max(int* num, int n){ | |
int max = *num; | |
for(int i=1;i<n;i++){ | |
if(max < *(num+i)){ | |
max = *(num+i); | |
} | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment