On 1/11/2025 1:53 AM, Physfitfreak wrote:
Score of 85 might be a bit too harsh in his case, but I have no doubt that he's around the average IQ but on the dumb side of that average; never on the other side. That, I'm sure.
Get ready Maleki.
========================================================================================
//this code is hereby released to the public domain
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
/*
this program computes the descriptive statistics of sets of integers
1.0 release Dec 2020
2.0 release Jun 2024
used the population skewness and Kurtosis formulas from:
https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php also test the results of this code against that site
compile:
$ gcc -Wall prog.c -o prog -lm
or
$ gcc -Wall -Wextra -O2 prog.c -o prog -lm
or
$ gcc -Wall -Wextra -O2 -fsanitize=undefined prog.c -o prog
-lm
usage : ./prog -option N
where option is:
-r generates N random numbers
-c generates consecutive numbers 1 to N
-o generates random numbers with outliers
-f read in dataset from file
and N is 2+ or the file name
*/
//random ints
int randNbr(int low, int high) {
return (low + rand() / (RAND_MAX / (high - low + 1) + 1));
}
//comparator function used with qsort
int compareint (const void * a, const void * b)
{
if (*(int*)a > *(int*)b) return 1;
else if (*(int*)a < *(int*)b) return -1;
else return 0;
}
int main(int argc, char *argv[])
{
if(argc < 3) {
printf("Missing argument:\n");
printf(" * enter a number greater than 2\n");
printf(" * enter an option -r -c -o or -f\n");
exit(0);
}
//=================================================================================================
//generate datasets
//=================================================================================================
int i = 0, N = 0;
int *nums;
if(strcmp(argv[1],"-f") != 0) {
N = atoi(argv[2]);
nums = malloc(N * sizeof(int));
}
//random
if(strcmp(argv[1],"-r") == 0) {
srand(time(NULL));
for(i=0;i<N;i++) { nums[i] = randNbr(1,N*3); }
printf("%d Randoms between 1 and %d\n", N, 3*N);
for(i=0;i<N;i++) { printf("%d ", nums[i]); }
qsort(nums,N,sizeof(int),compareint);
printf("\nSorted:\n"); for(i=0;i<N;i++) { printf("%d ", nums[i]); }
}
//random with outliers
if(strcmp(argv[1],"-o") == 0) {
srand(time(NULL));
nums[0] = 1; nums[1] = 3;
for(i=2;i<N-2;i++) { nums[i] = randNbr(100,N*30); }
nums[N-2] = 1000; nums[N-1] = 2000;
printf("%d Randoms with outliers\n", N);
for(i=0;i<N;i++) { printf("%d ", nums[i]); }
qsort(nums,N,sizeof(int),compareint);
printf("\nSorted:\n"); for(i=0;i<N;i++) { printf("%d ", nums[i]); }
}
//consecutive numbers 1 to N
if(strcmp(argv[1],"-c") == 0) {
printf("%d Consecutive\n", N);
for(i=0;i<N;i++) {
nums[i] = i + 1;
printf("%d ", nums[i]);
}
}
//read dataset from file
if(strcmp(argv[1],"-f") == 0) {
nums = malloc(2 * sizeof(int));
FILE* datafile = fopen(argv[2], "r");
while(fscanf(datafile, "%d", &nums[N++]) == 1){
nums = realloc(nums, (N+1) * sizeof(int));
}
fclose (datafile);
N--;
printf("%d from file\n", N);
for(i=0;i<N;i++) { printf("%d ", nums[i]); }
qsort(nums,N,sizeof(int),compareint);
printf("\nSorted:\n"); for(i=0;i<N;i++) { printf("%d ", nums[i]); }
}
//=================================================================================================
//calc descriptive stats
//=================================================================================================
double dmin = nums[0], dmax = nums[N-1];
double sumN=0.0, median=0.0, Q1=0.0, Q2=0.0, Q3=0.0, IQR=0.0;
double diff = 0.0, sqrdiffmean = 0.0, cubediffmean = 0.0, quaddiffmean = 0.0;
double meanabsdev = 0.0, rootmeansqr = 0.0;
char temp[15]="";
for(i=0;i<N;i++) {sumN += nums[i];}
double mean = sumN / (double)N;
for(i = 0; i < N; i++)
{
diff = nums[i] - mean;
sqrdiffmean += diff * diff ; // for variance and sum squares
cubediffmean += diff * diff * diff ; // for skewness
quaddiffmean += diff * diff * diff * diff ; // for Kurtosis
meanabsdev += fabs(diff) ; // for mean absolute deviation
rootmeansqr += nums[i] * nums[i] ; // for root mean square
}
double stddev = sqrt(sqrdiffmean/N);
double skewness = cubediffmean / (N * pow(stddev,3));
double kurtosis = quaddiffmean / (N * pow(stddev,4));
// median and quartiles
// quartiles divide sorted dataset into four sections
// Q1 = median of values less than Q2
// Q2 = median of the data set
// Q3 = median of values greater than Q2
if(N % 2 == 0) {
Q2 = median = (nums[(N/2)-1] + nums[N/2]) / 2.0;
i = N/2;
if(i % 2 == 0) {
Q1 = (nums[(i/2)-1] + nums[i/2]) / 2.0;
Q3 = (nums[i + ((i-1)/2)] + nums[i+(i/2)]) / 2.0;
}
else {
Q1 = nums[(i-1)/2];
Q3 = nums[i + ((i-1)/2)];
}
}
if(N % 2 != 0) {
Q2 = median = nums[(N-1)/2];
i = (N-1)/2;
if(i % 2 == 0) {
Q1 = (nums[(i/2)-1] + nums[i/2]) / 2.0;
Q3 = (nums[i + (i/2)] + nums[i + (i/2) + 1]) / 2.0;
}
else {
Q1 = nums[(i-1)/2];
Q3 = nums[i + ((i+1)/2)];
}
}
//mode
//1 array to hold count of each value in set
//2 count how many times each number in the set occurs,
// and track the # of occurrences
//3 extract the values occurring most often
int occur = 0;
char mode[250] = "";
if(strcmp(argv[2],"-c") != 0) { //consecutive #s have no mode
int *modecnt = calloc(dmax + 1, sizeof(int)); //array
for(i = 0; i < N; i++) { //count occurrences of each value
modecnt[nums[i]]++;
if(modecnt[nums[i]] > occur) {
occur = modecnt[nums[i]];
}
}
if (occur > 1) { //extract modes if any
for(i = 0; i <= dmax; i++) {
if(modecnt[i] == occur) {
sprintf(temp,"%d ",i);
strncat(mode,temp,strlen(temp));
}
}
}
free(modecnt);
}
// outliers
// below Q1−1.5xIQR, or above Q3+1.5xIQR
IQR = Q3 - Q1;
char outliers[200]="";
if (N > 3) {
//range for outliers
double lo = Q1 - (1.5 * IQR);
double hi = Q3 + (1.5 * IQR);
//no outliers
if (dmin >= lo && dmax <= hi) {
strcat(outliers,"none (using IQR * 1.5 rule)");
}
//at least one outlier
if (dmin < lo || dmax > hi) {
for(i = 0; i < N; i++) {
double val = (double)nums[i];
if(val < lo || val > hi) {
sprintf(temp,"%.0f ",val);
strncat(outliers, temp, strlen(temp));
}
}
strcat(outliers," (using IQR * 1.5 rule)");
}
outliers[strlen(outliers)] = '\0';
}
//=================================================================================================
//output
//=================================================================================================
printf("\n--------------------------------------------------------------\n");
printf("Minimum : %.0f\n", dmin);
printf("Maximum : %.0f\n", dmax);
printf("Range : %.0f\n", dmax - dmin);
printf("Size N : %d\n" , N);
printf("Sum N : %.0f\n", sumN);
printf("Mean μ : %.2f\n", mean);
printf("Median : %.1f\n", median);
if(occur > 1) {
printf("Mode(s) : %s (%d occurrences ea)\n", mode,occur);}
else {
printf("Mode(s) : na (no repeating values)\n");}
printf("Std Dev σ : %.6f\n", stddev);
printf("Variance σ^2 : %.6f\n", sqrdiffmean/N);
printf("Mid Range : %.1f\n", (dmax + dmin)/2);
printf("Quartiles");
if(N > 3) {printf(" Q1: %.1f\n", Q1);}
if(N < 4) {printf(" Q1: na\n");}
printf(" Q2: %.1f (median)\n", Q2);
if(N > 3) {printf(" Q3: %.1f\n", Q3);}
if(N < 4) {printf(" Q3: na\n");}
printf("IQR : %.1f (interquartile range)\n", IQR);
if(N > 3) {printf("Outliers : %s\n", outliers);}
if(N < 4) {printf("Outliers : na\n");}
printf("Sum Squares SS : %.6f\n", sqrdiffmean);
printf("MAD : %.6f (mean absolute deviation)\n", meanabsdev / N);
printf("Root Mean Sqr : %.6f\n", sqrt(rootmeansqr / N));
printf("Std Error Mean : %.6f\n", stddev / sqrt(N));
printf("Skewness γ1 : %.6f\n", skewness);
printf("Kurtosis β2 : %.6f\n", kurtosis);
printf("Kurtosis Excess α4 : %.6f\n", kurtosis - 3);
printf("CV : %.8f (coefficient of variation\n", stddev / mean);
printf("RSD : %.6f%% (relative std deviation)\n", 100 * (stddev / mean));
printf("--------------------------------------------------------------\n");
printf("Check results up to N=9999 against\n");
printf("
https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php");
printf("\n\n");
free(nums);
return(0);
}
========================================================================================
OMG that's sweet! The ONLY part not 100% mine from scratch is the 4-line comparator function.
That's pure pwnage of the programming clowns Mehram Maleki (you) and Larry Pietraskiewicz (Feeb).
His closest "buddy" in here is the brain-dead Relf..
No, though I did like Relf and will miss his odd posts. I learned a bit about Usenet from him.