Splitting into multiple files You are to create a program that reads the dimensi
ID: 3689384 • Letter: S
Question
Splitting into multiple files You are to create a program that reads the dimensions of geometric shapes from command line and computes and outputs the area of the shape. Your program will consist of multiple files as follows. Create a header file geometry.h and add to it the following A marcro definition for the constant P A prototype definition for the function computeCircleArea. This function takes as input the radius of a circle and returns its area A prototype definition for the function computeRectancleArea. This function takes as input the length and width of a rectangle and outputs its area Create a source file geometry.c and add to it the definition of the two functions. Create your main program areas.c so it reads the parameters from command line. If the shape is a circle, the command line parameter should be the letter C followed by the radius. If the shape is a rectangle, the command line parameters should be the letter R followed by the length and the width. The following is an example of valid parameters. a.out R 12.5 14.2 >a.out C 3.6 a.out C 10.2 Your program should compute the area according to the parameters given. If the program is given invalid parameters, it should display an error message to the user and exit. The outputs corresponding to the previous examples are c 40.7 C 326.8 Note that the numeric values read from command line are read as strings. Use the function atof defined as: double atof (const char str) in stdlib.h to convert from string to float as neededExplanation / Answer
/*** geometry.h ***/
#ifndef GEOMETRY_H
#define GEOMETRY_H
#define PI 3.14159f
/* Prototypes for the functions */
float computeCircleArea(float radius);
float computeRectancleArea(float length, float breadth);
#endif
/** geometry.c ***/
#include <stdio.h>
#include "geometry.h"
float computeCircleArea(float radius)
{
return PI*radius*radius;
}
float computeRectancleArea(float length, float breadth)
{
return length*breadth;
}
/***areas.c ***/
#include <stdio.h>
#include <stdlib.h>
#include "geometry.h"
int main(int argc, char const *argv[])
{
float radius = 0.0f;
float rectanglearea = 0.0f;
float circlearea = 0.0f;
float width = 0.0f;
float length = 0.0f;
if(argc > 4) printf("Invalid Input ");
else if (strcmp(argv[1],"R") == 0)
{
length = atof(argv[2]);
width = atof(argv[3]);
rectanglearea = computeRectancleArea(length,width);
printf("R %0.1f ", rectanglearea);
}
else if (strcmp(argv[1], "C") == 0)
{
radius = atof(argv[2]);
circlearea = computeCircleArea(radius);
printf("C %0.1f ", circlearea);
}
else
printf("Invalid Input ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.