Reputation: 411
Hey guys I finished up this basic C program that displays an ordered set, least value, max value, average and median to a user who inputs any given set of numbers. The problem I'm having is that I have to set a standard amount of precision using something like "3.2%f" when I print the numbers, how can I print only them minimum amount of decimal places I want for each number? For example, if I input 5, 5.5, -0.2313, and 4 my program will display them in order as -0.23, 4.00, 5.00, and 5.50. How can I get it to read -.2313, 4, 5, and 5.5? Thanks in advance for the help.
#include <stdio.h>
int findSize();
void sortArray(int size, double num[]);
double findAverage(int size, double num[]);
double findMedian(int size, double num[]);
void findLowtoHigh(int size, double num[]);
void findHightoLow(int size, double num[]);
int main()
{
while(1)
{
int size = findSize();
if(size <= 1)
{
return 0;
}
double num[size];
double lowest = 0;
double highest = 0;
double average = 0;
double median = 0;
fprintf(stdout, "\n");
sortArray(size, num);
findLowtoHigh(size, num);
findHightoLow(size, num);
average = findAverage(size, num);
median = findMedian(size, num);
fprintf(stdout, "\n\nLowest Value: %3.4f", num[0]);
fprintf(stdout, "\nHighest Value: %3.4f", num[size-1]);
fprintf(stdout, "\n\nAverage Value: %3.4f\n", average);
fprintf(stdout, "Median Value: %3.4f", median);
fprintf(stdout, "\n");
}
}
int findSize()
{
int size;
fprintf(stdout, "\nPlease enter size of the array: ");
scanf("%d", &size);
return size;
}
void sortArray(int size, double num[])
{
for(int i = 0; i <= size - 1; i++)
{
int j = i+1;
fprintf(stdout, "Please enter number %d: ", j);
fscanf(stdin, "%lf", &num[i]);
}
if(size > 1)
{
double holder = 0;
for(int y = 0; y < size - 1; y++)
{
for(int k = 0; k < size - 1; k++)
{
if(num[k] > num[k+1])
{
holder = num[k];
num[k] = num[k+1];
num[k+1] = holder;
}
}
}
}
}
void findLowtoHigh(int size, double num[])
{
fprintf(stdout, "\nFrom least to greatest: ");
for(int x = 0; x <= size - 1; x++)
{
fprintf(stdout, "%3.2f ", num[x]);
}
}
void findHightoLow(int size, double num[])
{
fprintf(stdout, "\nFrom greatest to least: ");
int reverse = size - 1;
while(reverse != -1)
{
fprintf(stdout, "%3.2f ", num[reverse]);
reverse--;
}
}
double findAverage(int size, double num[])
{
double average = 0;
for(int a = 0; a <= size - 1; a++)
{
average = average + num[a];
}
average = average / size;
return average;
}
double findMedian(int size, double num[])
{
double median = 0;
if(size % 2 == 0)
{
median = (num[size/2 - 1] + num[size/2])/2;
}
else
{
median = num[size/2];
}
return median;
}
Upvotes: 2
Views: 109
Reputation: 153456
Let sprintf()
do the heavy lifting and then lop off trailing zeros.
char *print_min_precision(char * buffer) {
// look fo rthe decimal point, if not found return without altering buffer
if (strchr(buffer, '.') == NULL)
return buffer;
// Find the last digit
char *p = &buffer[strlen(buffer) - 1];
// While not at the beginning and digit is zero ...
while (p > buffer && *p == '0') {
// Change '0' to `\0`
*p-- = '\0';
}
if (p > buffer && *p == '.') {
*p-- = '\0';
}
return buffer;
}
char buf[400]; // a BA buffer
// Print the number using some %f format.
// 6 here is the maximum number of fractional digits to use
sprintf(buf, "%.6f", num[reverse]);
// Print the reduced number.
fprintf(stdout, "%s ", print_min_precision(buf));
In general, attempting to numerically figure out the trailing decimals has many edge conditions. Better to print to a buffer and post-process it. Be sure to provide adequate buffers for things like:
print_min_precision(sprintf(buf, "%.6f", 10e300));
Upvotes: 3
Reputation: 1981
It's hard to get everything you want without custom-coding from scratch, but "%g
" takes care of at least most of what you want. The leading zero is pretty much non-negotiable, is all. It generally picks the most compact notation for the value, including scientific notation in extreme cases.
Upvotes: 3