OFFLINE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX_SIZE 100000
#define MAX_BINS 1000
#define MAX_STARS 50
#include "utils.c"
int asc(const void *a, const void *b)
{
double diff = (*(double *)a) - (*(double *)b);
if (diff < 0)
return -1;
else if (diff > 0)
return 1;
else
return 0;
}
void printstar(int a, int max)
{
int b = (max > 0) ? ((a * MAX_STARS) / max) : 0;
for (int i = 0; i < b; i++)
printf("*");
}
double findmin(double arr[], int n)
{
double min = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
double findmax(double arr[], int n)
{
double max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
double findmean(double arr[], int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum / n;
}
double findstddev(double arr[], int n, double mean)
{
double sq_sum = 0;
for (int i = 0; i < n; i++)
sq_sum += (arr[i] - mean) * (arr[i] - mean);
return sqrt(sq_sum / n);
}
double findmedian(double arr[], int n)
{
double temp[MAX_SIZE];
for (int i = 0; i < n; i++)
temp[i] = arr[i];
qsort(temp, n, sizeof(double), asc);
if (n % 2 == 0)
return (temp[n / 2 - 1] + temp[n / 2]) / 2.0;
else
return temp[n / 2];
}
void calculate_histogram(
double arr[], int n, int bin, int uniform_fix, double min_value, double max_value,
double start[], double end[], int arrcount[], int *maxx)
{
qsort(arr, n, sizeof(double), asc);
double min = findmin(arr, n);
double max = findmax(arr, n);
double hist_min = uniform_fix ? min_value : min;
double hist_max = uniform_fix ? max_value : max;
double bin_diff = (hist_max - hist_min) / bin;
for (int i = 0; i < bin; i++)
{
start[i] = hist_min + i * bin_diff;
end[i] = start[i] + bin_diff;
}
for (int i = 0; i < bin; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
if (i == bin - 1)
{
if (arr[j] >= start[i] && arr[j] <= end[i])
count++;
}
else
{
if (arr[j] >= start[i] && arr[j] < end[i])
count++;
}
}
arrcount[i] = count;
}
*maxx = arrcount[0];
for (int i = 1; i < bin; i++)
if (arrcount[i] > *maxx)
*maxx = arrcount[i];
}
int main()
{
char command[20];
char dis[20];
int n = 0, bin = 0, array_fixed = 0;
double arr[MAX_SIZE];
unsigned seed = 0;
double min_value = 0, max_value = 0;
int uniform_fix = 0;
double min, max, mean, stddev;
double start[MAX_BINS], end[MAX_BINS];
int arrcount[MAX_BINS], maxx = 0;
printf(
"Commands:\n"
" set - Set array size, seed, and distribution (uniform or gaussian)\n"
" min - Print minimum value\n"
" max - Print maximum value\n"
" mean - Print mean value\n"
" median - Print median value\n"
" stddev - Print standard deviation\n"
" hist - Plot histogram\n"
" summary - Print min, max, mean, median, stddev\n"
" help - Show this help message\n"
" exit - Exit the program\n");
while (1)
{
printf("\nEnter command: ");
scanf("%s", command);
if (!array_fixed)
{
int valid = 0;
if (strcmp(command, "set") == 0) valid = 1;
if (strcmp(command, "help") == 0) valid = 1;
if (strcmp(command, "exit") == 0) valid = 1;
if (strcmp(command, "min") == 0) valid = 1;
if (strcmp(command, "max") == 0) valid = 1;
if (strcmp(command, "mean") == 0) valid = 1;
if (strcmp(command, "median") == 0) valid = 1;
if (strcmp(command, "stddev") == 0) valid = 1;
if (strcmp(command, "hist") == 0) valid = 1;
if (strcmp(command, "summary") == 0) valid = 1;
if (!valid)
{
printf("Unknown command. Type 'help' for list of commands.\n");
continue;
}
else if (strcmp(command, "set") != 0 && strcmp(command, "help") != 0 && strcmp(command, "exit") != 0)
{
printf("Array not initialized. Use 'set' command first.\n");
continue;
}
}
if (strcmp(command, "help") == 0)
{
printf(
"Commands:\n"
" set - Set array size, seed, and distribution (uniform or gaussian)\n"
" min - Print minimum value\n"
" max - Print maximum value\n"
" mean - Print mean value\n"
" median - Print median value\n"
" stddev - Print standard deviation\n"
" hist - Plot histogram\n"
" summary - Print min, max, mean, median, stddev\n"
" help - Show this help message\n"
" exit - Exit the program\n");
continue;
}
if (strcmp(command, "exit") == 0)
{
break;
}
if (!array_fixed && strcmp(command, "set") == 0)
{
while (1)
{
printf("Enter number of elements (<= 100000): ");
if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_SIZE)
{
printf("Invalid input. Please enter a valid integer for number of elements.\n");
while (getchar() != '\n');
continue;
}
break;
}
while (1)
{
printf("Enter seed: ");
if (scanf("%u", &seed) != 1)
{
printf("Invalid input. Input again: \n");
while (getchar() != '\n');
continue;
}
break;
}
while (1)
{
printf("Distribution? (uniform/gaussian): ");
scanf("%s", dis);
if (strcmp(dis, "gaussian") == 0)
{
double mean_in, stddev_in;
printf("Enter mean and stddev: ");
scanf("%lf%lf", &mean_in, &stddev_in);
populate_array_gaussian(arr, n, mean_in, stddev_in, seed);
array_fixed = 1;
uniform_fix = 0;
printf("Array initialized with gaussian distribution.\n");
break;
}
else if (strcmp(dis, "uniform") == 0)
{
printf("Enter min and max: ");
scanf("%lf%lf", &min_value, &max_value);
populate_array_uniform(arr, n, min_value, max_value, seed);
array_fixed = 1;
uniform_fix = 1;
printf("Array initialized with uniform distribution.\n");
break;
}
else
{
printf("Invalid distribution. Please enter again.\n");
}
}
min = findmin(arr, n);
max = findmax(arr, n);
mean = findmean(arr, n);
stddev = findstddev(arr, n, mean);
continue;
}
if (strcmp(command, "set") == 0)
{
while (1)
{
printf("Enter number of elements (<= 100000): ");
if (scanf("%d", &n) != 1 || n <= 0 || n > MAX_SIZE)
{
printf("Invalid input. Please enter a valid integer for number of elements.\n");
while (getchar() != '\n');
continue;
}
break;
}
while (1)
{
printf("Enter seed: ");
if (scanf("%u", &seed) != 1)
{
printf("Invalid input. Input again: \n");
while (getchar() != '\n');
continue;
}
break;
}
while (1)
{
printf("Distribution? (uniform/gaussian): ");
scanf("%s", dis);
if (strcmp(dis, "gaussian") == 0)
{
double mean_in, stddev_in;
printf("Enter mean and stddev: ");
scanf("%lf%lf", &mean_in, &stddev_in);
populate_array_gaussian(arr, n, mean_in, stddev_in, seed);
array_fixed = 1;
uniform_fix = 0;
printf("Array re-initialized with gaussian distribution.\n");
break;
}
else if (strcmp(dis, "uniform") == 0)
{
printf("Enter min and max: ");
scanf("%lf%lf", &min_value, &max_value);
populate_array_uniform(arr, n, min_value, max_value, seed);
array_fixed = 1;
uniform_fix = 1;
printf("Array re-initialized with uniform distribution.\n");
break;
}
else
{
printf("Invalid distribution. Please enter again.\n");
}
}
min = findmin(arr, n);
max = findmax(arr, n);
mean = findmean(arr, n);
stddev = findstddev(arr, n, mean);
}
else if (strcmp(command, "min") == 0)
{
printf("Min : %.4lf\n", min);
}
else if (strcmp(command, "max") == 0)
{
printf("Max : %.4lf\n", max);
}
else if (strcmp(command, "mean") == 0)
{
printf("Mean : %.4lf\n", mean);
}
else if (strcmp(command, "median") == 0)
{
printf("Median : %.4lf\n", findmedian(arr, n));
}
else if (strcmp(command, "stddev") == 0)
{
printf("Stddev : %.4lf\n", stddev);
}
else if (strcmp(command, "hist") == 0)
{
while (1)
{
printf("Enter number of bins (<= 1000): ");
scanf("%d", &bin);
if (bin > 0 && bin <= MAX_BINS) break;
else { printf("Invalid number of bins. Must be between 1 and 1000 \n"); continue;}
}
calculate_histogram(arr, n, bin, uniform_fix, min_value, max_value, start, end, arrcount, &maxx);
for (int i = 0; i < bin; i++)
{
printf("[%8.2lf - %8.2lf]: ", start[i], end[i]);
printstar(arrcount[i], maxx);
printf(" \n");
}
}
else if (strcmp(command, "summary") == 0)
{
printf(
"Min : %15.4lf\n"
"Max : %15.4lf\n"
"Mean : %15.4lf\n"
"Median : %15.4lf\n"
"Std Dev : %15.4lf\n",
min, max, mean, findmedian(arr, n), stddev);
}
else
{
printf("Unknown command. Type 'help' for list of commands.\n");
}
}
return 0;
}
Comments
Post a Comment