– Assignment 1 Custom Libraries and Implementation Files Objectives • To practic
ID: 3781831 • Letter: #
Question
– Assignment 1 Custom Libraries and Implementation Files Objectives • To practice developing customized library • To practice creating implementation files • To practice linking multiple object files for final executable --------------------------------------------------------------------------------------------------------------------------------------- Background Sometimes the standard C libraries (stdio.h, stdlib.h, etc) are not extensive enough to handle every programming need. If you find yourself rewriting or reusing functions you’ve written earlier like those used to calculate statistical parameters (mean, variance etc), copying the code again and again is inefficient. If you could write the code once, troubleshoot it so it works, and have it compiled in its own object file, you could then link these object files to your source code object file and not have to rewrite all the functions. --------------------------------------------------------------------------------------------------------------------------------------- EGR 107 Page 2 Part I – [100 points] Description Your task is to create a personal library to implement a statistic package on any given data (assume that the data is always of type float). To do this you must create: 1. a header file (stats.h) with 5 function prototypes: § maximum() § minimum() § mean() – equation: = = 1 0 [ ] 1 N i x i N x § variance() – equation: 2 1 0 2 ( [ ] ) 1 1 x i x N N i = = § histogram() 2. five (5) implementation files with each of functions listed above – min.c, max.c, mean.c, variance.c, and histogram.c You will then write C code (main.c) that asks the user for the data file he/she wishes to compute the statistics for and displays the results to the user. YOU WILL NOT WRITE ANY STATISTICS FUNCTIONS IN main.c – you will simply call the functions prototyped in stats.h and coded in min.c, max.c, mean.c, variance.c, and histogram.c!!! Procedure: (Refer to textbook pg 669-677) 1. Create the header file stats.h: a. Start with a block comment summarizing the purpose of the header file b. # define any constant that you need for the header file (NOT YOUR CODE) c. Individual function prototypes (with introductory comments stating purpose of each function) d. Save e. Below is the template for stats.h #ifndef STATS_H #define STATS_H float minimum(…); // NOTE: You need to complete the prototypes float maximum(…); float mean(…); float variance(…); void histogram(…); #endif // STATS_H_INCLUDED EGR 107 Page 3 2. Create 5 implementation files (min.c, max.c, mean.c, variance.c, histogram.c) a. Start with a block comment summarizing the purpose of each implementation file b. #include the necessary standard C libraries for each function c. #include “stats.h” – the angular brackets (< >) are used for standard C libraries found in the system directory, the quotation marks (“ ”) indicate a custom library found in the SAME directory as your code. d. #define – any constants for that particular implementation file e. Each file will contain one function as you would normally write it. i. The first four files will calculate maximum, minimum, mean, and variance and return one value each time (so they can be regular call-by-value functions) ii. Your histogram function will count the number of occurrences of data within a certain range; each range is commonly referred to as a “bin”. We will fix the number of bins that we use to ten. You will divide the range of your data (max-min) into 10 bins. Let’s assume that max = 100 and min = 0. The first bin will hold the number of data points between 0 and 9, the second will hold the number of data points between 10 and 19, etc. Note that the ranges for each bin are inclusive and that the last bin must have a range of 90 to 100 (not 90 to 99). iii. Points will be lost for using 10 if-else statements!!! Think nested loops. iv. Your histogram function must be passed the number of data points, a pointer to the data array, and a pointer to a 10 element array that will hold the histogram count. f. Note: the implementation files WILL NOT have a main() function. g. Note: the implementation files WILL NOT have ANY I/O functions (printf(), scanf() etc). h. Make sure all these files are added to the project along with the header file. 3. Create your code (main.c) a. Include the usual (detailed) comment block including program name, author, date, inputs, outputs and description. b. Along with all the regular #include statements you MUST #include “stats.h”. Remember use double quotes to indicate local header file. c. Download the input test file “grades.txt” from course website. d. Create a function of type void to explain the program to the user. Do not forget to call the function from main(). e. Create a function to read the data from the file into your input array and call it from main(). This function will return the number of grades in the file. Assume that the maximum number of scores is 1000. f. OUTPUT ALL RESULTS FROM main(). Your output must match the format example given below. EGR 107 Page 4 Output format: There are 400 grades read Mean = 60.022750 Variance = 205.449531 Maximum = 100.000000 Minumum = 0.000000 Grade Histogram 0% - 9%: 2 10% - 19%: 1 20% - 29%: 4 30% - 39%: 24 40% - 49%: 59 50% - 59%: 105 60% - 69%: 108 70% - 79%: 67 80% - 89%: 24 90% - 100%: 6 The above printout implies that there were 2 scores between 0% and 9%, 1 score between 10% and 19%, 4 scores between 20% and 29%, etc. in the file “grades.txt”. -----------------------------------------------------------------------------------------
Please include all codes required for max, min, histogram, etc
Explanation / Answer
The "input test file" is not provided. So I could not provide the output.
// STATS.h file
#ifndef STATS_H
#define STATS_H
float minimum(int arr[], int size); // NOTE: You need to complete the prototypes
float maximum(int arr[], int size);
float mean(int arr[], int size);
float variance(int arr[], int size);
void histogram(const unsigned char* arr, const int width, const int height,
const int canvasHeight,int* histogramBins);
#endif // STATS_H_INCLUDED
// min.c
#include<stdio.h>
// Function to calculate minimum value in the array.
float minimum(int arr[], int size) {
int i;
float small = 0;
for (i = 0; i < size; ++i) {
if (small < arr[i])
small = arr[i]
}
return small;
}
// max.c
#include<stdio.h>
// Function to calculate maximum value in the array.
float maximum(int arr[], int size) {
int i;
float largest = arr[0];
for (i = 1; i < size; ++i) {
if (largest > arr[i])
largest = arr[i]
}
return largest;
}
// mean.c
// Function to calculate mean value .
#include<stdio.h>
float mean(int arr[], int size){
int i;
float sum = 0;
for (i = 1; i < size; ++i) {
sum += arr[i];
}
return sum/size;
}
// variance.c
// Function to calculate variance.
#include<stdio.h>
#include<math.h>
float variance(int arr[], int size){
int i;
int sum = 0;
int avg = mean(arr,size);
for (i = 0; i < size; i++){
sum += pow((arr[i] - avg),2);
}
return sum/size;
}
// histogram.c
// Function to calculate the Histogram and print the value.
#include <stdio.h>
void histogram(unsigned char* arr, const int width, const int height,
const int canvasHeight,int* histogramBins)
{
// number of Bins = 10.
const int BINS = 10;
// Clear the output
memset(histogramBins, 0, BINS * sizeof(int));
// Get the bin values
const unsigned int totalPixels = width * height;
for (int index = 0; index < totalPixels; index++)
{
histogramBins[arr[index]]++;
}
// Resolve the maximum count in bins
int maxBinValue = 0;
for (int index = 0; index < BINS; index++)
{
if (histogramBins[index] > maxBinValue)
{
maxBinValue = histogramBins[index];
}
}
maxBinValue = maxBinValue == 0 ? 1 : maxBinValue;
// Normalize to fit into the histogram UI canvas height and print the values.
for (int index = 0; index < BINS; index++)
{
histogramBins[index] = histogramBins[index] * canvasHeight / maxBinValue;
printf("%d ", histogramBins[index]);
}
}
// lab4.c
#include<stdio.h>
#include<stdlib.h>
#include "STATS.h"
// Global array to store max 1000 grades.
int arr[1000];
// To read from the file.
int read_from_file(){
FILE *fp;
fp = fopen("input.txt", "r");
int i =0 ;
// Read till the end of the file.
for(i=0;i<1000;i++){
fscanf(fp,"%d",&arr[i]);
if (feof(fp))
break;
}
fclose(fp);
// Return the size of the array read.
return i;
}
int main(){
int size = 0;
int minm = 0;
int maxm = 0;
int avg = 0;
int var = 0;
// Please fill up the values you require below.
const int width = ????
const int height = ????
const int canvasHeight = ????
int histogramBins[??] = ???
size = read_from_file();
// Call the respective functions to get the values.
minm = minimum(arr, size);
maxm = maximum(arr, size);
avg = mean(arr, size);
var = variance(arr,size);
histogram(arr, width, height, canvasHeight,int* histogramBins);
// Print out the values.
printf("There are %d grades ",size);
printf("Mean= %d",avg);
printf("Variance= %d",var);
printf("Maximum= %d",maxm);
printf("Minimum= %d",minm);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.