In: Computer Science
A person writes a project with unit conversion functions, although she doesn’t use all of them. The conversions would be useful in a library. Create the library and modify the main.c to use the library. What is the name of your library? C PROGRAMMING
#include <stdio.h> // has printf() float inches2metres(float length_in_inches); float metres2inches(float length_in_metres); float pounds2kg(float weight_in_pounds); float kg2pounds(float mass_in_kg); float hours2seconds(float time_in_hours); float seconds2hours(float time_in_seconds); int main(void){ int i; float speed_mperhour, speed_inchespersec; printf(“speed (km/hr) speed (in/sec) \n\r”); for (i=1; i<=100;i++) { speed_inchespersec = metres2inches(i*1000.0)/hours2seconds(1.0); printf(“%f %f \n\r”, i, speed_inchespersec); } return 0; float inches2metres(float length_in_inches){ return (length_in_inches * 0.0254); // answer in metres float metres2inches(float length_in_metres){ return (length_in_metres * 39.3701); // answer in inches float pounds2kg(float weight_in_pounds){ return (weight_in_pounds * 0.453592); // answer in kilograms float kg2pounds(float mass_in_kg){ return (mass_in_kg * 2.20462) // answer in pounds float hours2seconds(float time_in_hours){ return (time_in_hours * 3600.0) // answer is seconds float seconds2hours(float time_in_seconds){ return (time_in_seconds / 3600.0) // answer is hours |
Steps to create your own library are :
Now,implementing for the given program:
Then name of the library is "unitconvo.h"
2)INTERFACE: the header file to your library should contain definitions for everything exported by your library:
#ifndef unitconvo.h #define unitconvo.h
extern float inches2metres(float length_in_inches);
extern float metres2inches(float length_in_metres);
extern float pounds2kg(float weight_in_pounds);
extern float kg2pounds(float mass_in_kg);
extern float hours2seconds(float time_in_hours);
extern float seconds2hours(float time_in_seconds);
2)IMPLEMENTATION: create a unitconvo.c file that #includes "unitconvo.h" and contains the implementation of every function in your library.
#include "unitconvo.h"
float inches2metres(float length_in_inches){
return (length_in_inches * 0.0254); }// answer in metres
float metres2inches(float length_in_metres){
return (length_in_metres * 39.3701);} // answer in inches
float pounds2kg(float weight_in_pounds){
return (weight_in_pounds * 0.453592);} // answer in kilograms
float kg2pounds(float mass_in_kg){
return (mass_in_kg * 2.20462)} // answer in pounds
float hours2seconds(float time_in_hours){
return (time_in_hours * 3600.0)} // answer is seconds
float seconds2hours(float time_in_seconds){
return (time_in_seconds / 3600.0) }// answer is hours
3)create a LIBRARY OBJECT FILE that can be linked into other programs that use your library (use the -c option to gcc to tell it just to create an object file (a .o file) rather than an executable:
gcc -o unitconvo.o -c unitconvo.c
4)USE the library in other programs:
step 1: Add an include line (#include "unitconvo.h") in all program source files that use library definitions (e.g., test.c). step 2: Link the program's .c file with the library object file (i.e. specify the unitconvo.o file as a command line argument to gcc): gcc test.c unitconvo.o
Hope,we helped you with the answer.Anymore further details required feel free to ask us.We would love to help you.Thank you