Question

C programming. (30 pts) Consider the following code. struct Triangle { float side1; float side2; float...

C programming.

(30 pts) Consider the following code.

struct Triangle {

float side1;

float side2;

float side3;} t1, t2;

enum Boolean {FALSE, TRUE};

enum Boolean isEquilateral(struct Triangle * triangle);

a. (15 pts) Implement the function isEquilateral which returns enum value TRUE if the given triangle is equilateral and enum value FALSE otherwise. (Hint: in the real world, no two float values are exactly the same. If the difference of two float values is between -0.0001 and 0.0001, consider “almostEqual” and neglect the minor difference.)

b. (6 pts) Assign values of your choice to member variables of t1 to 2.5, 4.5, and 4.5, respectively.

c. (4 pts) Call the function isEquilateral on triangle t1 and capture the returned value in variable result.

d. (5 pts) Copy the content from t1 to t2 without having to individually copy each member.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Full Working C Code:

#include<stdio.h>
#include <stdbool.h>
struct Triangle
{
float side1;
float side2;
float side3;
}t1,t2;
//Enum with two values
enum Boolean { TRUE, FALSE };
/*
Function to check if the triangle is an Equilateral Triangle or not i.e
are all the 3 sides equal or not
*/
bool isEquilateral( struct Triangle object)
{
enum Boolean b;
if ( object.side1 == object.side2 && object.side2 == object.side3)
b = TRUE;
else
b = FALSE;
return b;
//Returns either True (0) or False (1)
}
int main()
{
//Initialize varibales of object t1
t1.side1 = 4.5;
t1.side2 = 4.5;
t1.side3 = 4.5;
//Copy all the fields in t2 object also
t2 = t1;
//call the method for checking if Triangle is Equilateral or not
int checkForEquilateral = isEquilateral(t2);
if(checkForEquilateral == 0)
{
printf("Equilateral Triangle");
}
else
{
printf("Not an Equilateral Triangle");
}
return 0;
}

Snapshots of Code:

Output:

a.)

bool isEquilateral( struct Triangle object)
{
enum Boolean b;
if ( object.side1 == object.side2 && object.side2 == object.side3)
b = TRUE;
else
b = FALSE;
return b;
//Returns either True (0) or False (1)
}

b.)

t1.side1 = 2.5;
t1.side2 = 4.5;
t1.side3 = 4.5;

c.)

int checkForEquilateral = isEquilateral(t1);

d.)

t2 = t1;

Add a comment
Know the answer?
Add Answer to:
C programming. (30 pts) Consider the following code. struct Triangle { float side1; float side2; float...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C language not C++ 1. Write the statements to do the following: (2 pts) a. Define...

    C language not C++ 1. Write the statements to do the following: (2 pts) a. Define a struct with member variables width, height, topleft x, topleft y all floats). Use a tag to call it Rectangle. b. Declare a variable struct type Rectangle 2. Consider the following variables: struct int x; float y; char zi var1; union nt x; float y; char[20] z;) var2 f float and int are stored using 4 bytes each, what is the size (in bytes)...

  • (JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore,...

    (JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore, your class will have THREE private member variables → side1, side2 and side3. Use the double data type to represent the triangle sides. In addition, please provide public methods that perform the following FIVE tasks: ▪ An input method that obtains the appropriate values for the three sides from the user. While entering the values of the three sides, please remember the triangle property that...

  • Consider the following code: 1. float foo (int a, int b, int c, int d, float...

    Consider the following code: 1. float foo (int a, int b, int c, int d, float e) { 2. float e; 3. if (a == 0) { 4. return 0; 5. } 6. int x = 0; 7. if ((a==b) || ((c == d) && bug(a) )) { 8. x=1; 9. } 10. e = 1/x; 11. return e; 12. } Function bug(a) should return a value of true when passed a value of a=1. Build: • a test suite...

  • I need this to be in C Write the implementation file, priority queue.c, for the interface...

    I need this to be in C Write the implementation file, priority queue.c, for the interface in the given header file, priority queue.h. Turn in your priority queue.c file and a suitable main program, main.c, that tests the opaque object. priority queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a...

  • Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c...

    Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c, that tests the opaque object. priority_queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a heap will not receive any credit. #ifndef...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...

    C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT