Last active
November 13, 2017 14:34
-
-
Save ericek111/1244cac1faa442cc9e217d8ff62d6b9c to your computer and use it in GitHub Desktop.
Find average of random numbers in array
This file contains 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> | |
#include <string.h> | |
#include <time.h> | |
int find_median(int* arr, size_t count); | |
size_t median_count(int* arr, size_t count, int median); | |
int find_median(int* arr, size_t count) { | |
int sum = 0; | |
for (size_t i = 0; i < count; i++) { | |
sum += arr[i]; | |
} | |
return sum / (long)count; | |
} | |
size_t median_count(int* arr, size_t count, int median) { | |
size_t medianc = 0; | |
for (size_t i = 0; i < count; i++) { | |
if(arr[i] == median) medianc++; | |
} | |
return medianc; | |
} | |
int main() { | |
const int lower_limit = 0; | |
const int upper_limit = 100; | |
size_t n = 1000; | |
int sum = 0; | |
int* narr = malloc(sizeof(int) * n); | |
int median = 0; | |
size_t medianc = 0; | |
// scanf("%d", &n); | |
srand((unsigned int) time(NULL)); // initialize the RNG | |
for (size_t i = 0; i < n; i++) { | |
narr[i] = lower_limit + rand() % (upper_limit - lower_limit + 1); | |
//sum += narr[i]; | |
printf("%d ", narr[i]); | |
} | |
median = find_median(narr, n); | |
medianc = median_count(narr, n, median); | |
/*median = sum / n; | |
for (size_t i = 0; i < n; i++) { | |
if(narr[i] == median) medianc++; | |
}*/ | |
printf("\nmedian: %d, occuring %zu times.\nsum: %d, count: %zu\n", median, medianc, sum, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment