A person writes a project with unit conversion functions, although she doesn’t u
ID: 3781545 • Letter: A
Question
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?
#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) ”);
for (i=1; i<=100;i++)
{
speed_inchespersec = metres2inches(i*1000.0)/hours2seconds(1.0);
printf(“%f %f ”, 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
#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) ”);
for (i=1; i<=100;i++)
{
speed_inchespersec = metres2inches(i*1000.0)/hours2seconds(1.0);
printf(“%f %f ”, 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
Explanation / Answer
To create a library we first need to
1. create a header file which contains function prototypes
2. create a source file which contains the implementation of all these functions
3. Create library object file which can then be used in other programs
Below we first create header file mylib.h then implementation file mylib.c and the create object file and then use this library in main function
// Header file
#ifndef _MYLIB_H_
#define _MYLIB_H_
// function prototype for different functions exported by this library:
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);
#endif
// Implementation file mylib.c
#include "mylib.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
}
// Create library object file
gcc -o mylib.o -c mylib.c
// Use mylib.h in main function
#include <stdio.h> // has printf()
#include "mylib.h" // importing functions defined in mylib.c library
int main(void){
int i;
float speed_mperhour, speed_inchespersec;
printf(“speed (km/hr) speed (in/sec) ”);
for (i=1; i<=100;i++)
{
speed_inchespersec = metres2inches(i*1000.0)/hours2seconds(1.0);
printf(“%f %f ”, i, speed_inchespersec);
}
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.