How to print 2 characters in C without using logical operators?

Michael Wendel

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:

  1. Ri: ring torus (rmajor > rminor)
  2. Hn: horn torus (rmajor = rminor)
  3. 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!

Sergey Kalinichenko

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to inverse a positive integer in C without * or - operators?

From Java

How to inverse a positive integer in C without * or - operators?

From Java

How to negate a positive integer in C without * or - operators?

From Java

How to compare the values inside if using logical operators in the bash shell script

From Dev

combined bit and logical operators in c

From Dev

Using logical operators in building a Pandas DataFrame

From Dev

Variable assignment using logical operators in PHP

From Dev

Logical comparison operators without control statements

From Dev

Space between logical operators in c

From Dev

Several errors in Swift when using || or && logical operators

From Dev

Sympy: Using logical operators on relationals

From Dev

How these logical operators work?

From Dev

Rounding integer division without logical operators

From Dev

How to print a 2D-array in C, without using the operator []?

From Dev

how to print map in c++ without using iterator

From Dev

Difference in C logical operators

From Dev

Simplifying Nested Conditionals using Logical Operators: Logical Operators are not working?

From Dev

How to print out a hollow circle in c using just characters/symbols

From Dev

Determine triangle type WITHOUT logical operators JAVA

From Dev

How does logical operators behave?

From Dev

How to print Excel using C# WPF without Office installed?

From Dev

how to print map in c++ without using iterator

From Dev

How to return AND and OR logical operators JavaScript

From Dev

Combine logical operators in C

From Dev

Determine a menu choice without using logical operators, relational operators, or selection constructs

From Dev

GNU find logical operators with -print

From Dev

Using logical operators with excel strings

From Dev

logical operators as conditionals in c

From Dev

Using Logical Operators inside NULLIF()

Related Related

  1. 1

    How to inverse a positive integer in C without * or - operators?

  2. 2

    How to inverse a positive integer in C without * or - operators?

  3. 3

    How to negate a positive integer in C without * or - operators?

  4. 4

    How to compare the values inside if using logical operators in the bash shell script

  5. 5

    combined bit and logical operators in c

  6. 6

    Using logical operators in building a Pandas DataFrame

  7. 7

    Variable assignment using logical operators in PHP

  8. 8

    Logical comparison operators without control statements

  9. 9

    Space between logical operators in c

  10. 10

    Several errors in Swift when using || or && logical operators

  11. 11

    Sympy: Using logical operators on relationals

  12. 12

    How these logical operators work?

  13. 13

    Rounding integer division without logical operators

  14. 14

    How to print a 2D-array in C, without using the operator []?

  15. 15

    how to print map in c++ without using iterator

  16. 16

    Difference in C logical operators

  17. 17

    Simplifying Nested Conditionals using Logical Operators: Logical Operators are not working?

  18. 18

    How to print out a hollow circle in c using just characters/symbols

  19. 19

    Determine triangle type WITHOUT logical operators JAVA

  20. 20

    How does logical operators behave?

  21. 21

    How to print Excel using C# WPF without Office installed?

  22. 22

    how to print map in c++ without using iterator

  23. 23

    How to return AND and OR logical operators JavaScript

  24. 24

    Combine logical operators in C

  25. 25

    Determine a menu choice without using logical operators, relational operators, or selection constructs

  26. 26

    GNU find logical operators with -print

  27. 27

    Using logical operators with excel strings

  28. 28

    logical operators as conditionals in c

  29. 29

    Using Logical Operators inside NULLIF()

HotTag

Archive