Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ILLUSTRATING THE USE OF MULTIPLE CALLED FUNCTIONS
- WITH CALLED FUNCTIONS PLACED ABOVE THE MAIN PROGRAM
- A. Design a program in C which compute for one time only the following for a box in the shape of a cube with length=width=height=a:
- (i) the total surface area, i.e., 6a2;
- (ii) the volume of the cube, i.e., a3 and
- (iii) the volume of the largest sphere that can fit into the cube,
- i.e., (4/3)π(a/2)3.
- You are to use three prototype or called functions in your design which are to be placed before the main function, with the output to be printed on the console from within the main function and displayed in a tabular form.
- B. Modify your program so that it will run in an infinite loop except when the user enters a value of 0 for a, at which point the program will terminate execution.
- SOURCE CODE A
- #include <stdio.h>
- #define pi 3.1415926
- float surfaceArea(float a){
- float result;
- result = 6 * a * a;
- return result;
- }
- float volume(float a){
- float result;
- result = a * a *a;
- return result;
- }
- float sphereVol(float a){
- float result,radius;
- radius=0.5*a;
- result = (4/3) * pi * radius * radius * radius;
- return result;
- }
- int main(void){
- float a;
- float area,vol,svol;
- printf("Please enter the value of a: ");
- scanf("%f",&a);
- area = surfaceArea(a);
- vol = volume(a);
- svol = sphereVol(a);
- printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n");
- printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
- return 0;
- }
- SAMPLE SOLUTION A
- SOURCE CODE B:
- #include <stdio.h>
- #define pi 3.1415926
- float surfaceArea(float a){
- float result;
- result = 6 * a * a;
- return result;
- }
- float volume(float a){
- float result;
- result = a * a *a;
- return result;
- }
- float sphereVol(float a){
- float result,radius;
- radius=0.5*a;
- result = (4/3) * pi * radius * radius * radius;
- return result;
- }
- int main(void){
- float a;
- float area,vol,svol;
- printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n\n");
- while(1){
- printf("Please enter the value of a: ");
- scanf("%f",&a);
- if(a==0){
- break;
- }
- area = surfaceArea(a);
- vol = volume(a);
- svol = sphereVol(a);
- printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
- } //end while
- return 0;
- } //end main
- SAMPLE SOLUTION B:
- ANOTHER SAMPLE SOLUTION B:
- Note: when the user input a value of 0 for a, the execution is terminated in agreement with specification
- ILLUSTRATING THE USE OF MULTIPLE CALLED FUNCTIONS
- WITH CALLED FUNCTIONS PLACED BELOW THE MAIN PROGRAM
- A. Design a program in C which compute for one time only the following for a box in the shape of a cube with length=width=height=a:
- (i) the total surface area, i.e., 6a2;
- (iv) the volume of the cube, i.e., a3 and
- (v) the volume of the largest sphere that can fit into the cube,
- i.e., (4/3)π(a/2)3.
- You are to use three prototype or called functions in your design, and display the output in a tabular form.
- C. Modify your program so that it will run in an infinite loop except when the user enters a value of 0 for a, at which point the program will terminate execution.
- SOURCE CODE A
- #include <stdio.h>
- #define pi 3.1415926
- float surfaceArea(float a);
- float volume(float a);
- float sphereVol(float a);
- int main(void){
- float a;
- float area,vol,svol;
- printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n\n");
- //while(1){
- printf("Please enter the value of a: ");
- scanf("%f",&a);
- //if(a==0){
- //break;
- //}
- area = surfaceArea(a);
- vol = volume(a);
- svol = sphereVol(a);
- printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
- //} //end while
- return 0;
- } //end main
- float surfaceArea(float a){
- float result;
- result = 6 * a * a;
- return result;
- }
- float volume(float a){
- float result;
- result = a * a *a;
- return result;
- }
- float sphereVol(float a){
- float result,radius;
- radius=0.5*a;
- result = (4/3) * pi * radius * radius * radius;
- return result;
- }
- SAMPLE SOLUTION A:
- B. SOURCE CODE B:
- #include <stdio.h>
- #define pi 3.1415926
- float surfaceArea(float a);
- float volume(float a);
- float sphereVol(float a);
- int main(void){
- float a;
- float area,vol,svol;
- printf("a\t\tSurfaceArea\t\tVolOfCube\t\t VolOfSphere\n\n");
- while(1){
- printf("Please enter the value of a: ");
- scanf("%f",&a);
- if(a==0){
- break;
- }
- area = surfaceArea(a);
- vol = volume(a);
- svol = sphereVol(a);
- printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t\t%.2f\n",a,area,vol,svol);
- } //end while
- return 0;
- } //end main
- float surfaceArea(float a){
- float result;
- result = 6 * a * a;
- return result;
- }
- float volume(float a){
- float result;
- result = a * a *a;
- return result;
- }
- float sphereVol(float a){
- float result,radius;
- radius=0.5*a;
- result = (4/3) * pi * radius * radius * radius;
- return result;
- }
- SAMPLE SOLUTION B:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement