Michael Wendel - 8 months ago 93

C Question

I'm working on a program that determines a type of torus, volume of that torus, surface area, and cost of fabricating it/painting it based upon 2 input radii: rmajor and rminor. *The only complicated part is I can't use logical operators or comparison statements.* I am able to determine the surface area, volume, and costs related to the torus but I can't determine the manufacturing code (as shown in the print statements). The corresponding Manufacturing codes are as follows:

- Ri: ring torus (rmajor > rminor)
- Hn: horn torus (rmajor = rminor)
- Sp: spindle torus. (rmajor < rminor)

How would I go about correctly printing the corresponding manufacturing code to each type of torus?

Here is my current code:

`#include<stdio.h>`

#include<math.h>

//GLOABAL DECLARATIONS

#define COST 75.25

#define PAINT 13.65

int main()

{

//LOCAL DECLARATIONS

int rmajor;

int rminor;

int factor1;

int factor2;

int factor3;

char char1;

char char2;

double area;

double volume;

double cost;

double paint;

//EXECUTABLES

printf("Please input major radius (meters): ");

scanf("%d" , &rmajor);

printf("Please input minor radius (meters): ");

scanf("%d", &rminor);

printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");

volume = (M_PI * pow(rminor,2)) * (2*M_PI*rmajor);

area = (4 * pow(M_PI,2) * rmajor * rminor);

factor1 = rmajor / rminor;

factor2 = rminor / rmajor;

factor1 = (factor1 + 2) % (factor1 + 1);

factor2 = (factor2 + 2) % (factor2 + 1);

factor3 = (factor1 * rmajor) + (factor2 * rminor);

factor3 = factor3 / (factor1 + factor2);

char1 = (factor1 * ('R' - 'A')) + (factor2 * ('H' - 'A')) + (factor3 * ('S' - 'A'));

char2 = (factor1 * ('i' - 'A')) + (factor2 * ('n' - 'A')) + (factor3 * ('p' - 'A'));

//printf("\nchar1: %c\n", char1);

//printf("char2: %c\n", char2);

cost = COST * volume;

paint = PAINT * area;

printf("Manufacturing Code: %c%c\n ", 'A' + char1, 'A' + char2);

printf("Surface Area : %13.2f\n", area);

printf("Volume : %13.2f\n", volume);

printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");

printf("Cost of building torus ($): %12.2f\n", cost);

printf("Cost of painting torus ($): %12.2f\n", paint);

printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");

return(0);

}

Here are some of my example outputs:

`Please input major radius (meters): 10`

Please input minor radius (meters): 5

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Manufacturing Code: ?

Surface Area : 1973.92

Volume : 4934.80

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Cost of building torus ($): 371343.87

Cost of painting torus ($): 26944.02

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

52 lpvinslogin01.itap.purdue.edu ~/CS159/labs/lab03 % a.out

Please input major radius (meters): 7

Please input minor radius (meters): 7

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Manufacturing Code: ▒▒

Surface Area : 1934.44

Volume : 6770.55

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Cost of building torus ($): 509483.78

Cost of painting torus ($): 26405.14

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Output 1 is supposed to be a ring torus (Ri) and Output 2 should be a horn torus (Hn). Any help is appreciated, thank you!

Answer

Your code computes `factor1`

and `factor2`

incorrectly: they stay `1`

when the values are equal, while multiplication inside the computation of `char1`

and `char2`

suggests that exactly one of `factor1`

, `factor2`

, and `factor3`

must be `1`

, and the remaining ones must be zero.

You can fix this problem by multiplying `factor1`

and `factor2`

by `(factor3+1)%2`

:

```
factor1 = rmajor / rminor;
factor2 = rminor / rmajor;
factor1 = (factor1 + 2) % (factor1 + 1);
factor2 = (factor2 + 2) % (factor2 + 1);
factor3 = (factor1 * factor2); // rmajor == rminor
factor1 *= (factor3+1) % 2; // rmajor > rminor
factor2 *= (factor3+1) % 2; // rmajor < rminor
char1 = (factor1 * 'R') + (factor2 * 'S') + (factor3 * 'H');
char2 = (factor1 * 'i') + (factor2 * 'p') + (factor3 * 'n');
printf("Manufacturing Code: %c%c\n ", char1, char2);
```

Note that factoring out `'A'`

from the computation lets you arrive at a much simpler forumla.

Demo.