iOS Development

Course No. 10-152-139

Assignment 11-2

Structs!

  • Create a new Xcode command-line project. Name the project Assignment-11-2.
  • Add #include <objc/objc.h> to the main.c file.
  • NOTE: This program will contain 4 total functions including main.
  • Define a struct named Triangle that will hold the angles for a triangle. It will have the following fields.
    • A double for each of the three angles. (3 doubles)

The validateTriangle function

  • Write a function named validateTriangle that has one parameter that is a pointer to the above struct and returns a BOOL. The function will perform the following by calling other functions.
    • Check the three angles by calling the checkAngles function and passing the input struct to it. If the checkAngles function returns NO then this function should return NO and end.
    • Calculate the possible missing angle by calling the calculateTriangleAngle function. If this function returns NO then the validateTriangle function should return NO.
    • If both functions return YES that means we have a valid triangle. Return YES and the function ends.

The checkAngles function

  • Write a function named checkAngles that has one parameter and returns a BOOL. The parameter is a pointer to the Triangle struct.
  • The function will check that there are no missing angles or that there is only one missing angle.
    • If there are no missing angles the function should return YES.
    • If there is only one missing angle the function should return YES.
    • The function should return NO if there are more than one missing angles.

The calculateTriangleAngle function

  • Write a function named calculateTriangleAngle that has one parameter and returns a BOOL. The parameter is a pointer to the Triangle struct.
  • The function first determine if the triangle can be created with the data that is in the struct. It will perform the following checks.
    • If the total sum of all the angles is greater than 180 then return NO and end the function.
    • If all of the angles have values and the sum of them doesn’t add up to 180 return NO and end the function.
    • If there is one missing angle calculate its value with this formula:
      • missing angle = 180 - (angle1 + angle2);
    • If you calculate the missing value then return YES and the function is done.

In the main function

  • In your program’s main function you will need to create a variable of the Triangle struct. Assign values to the angles in the struct.
  • Call the validateTriangle function and send the address of the struct to the function as an argument.
  • If the validateTriangle function returns YES then output the angles of the triangle in an output statement that looks something like this:
  • If the validateTriangle function returns NO then output:
  • Test your program with several valid and invalid triangles.