Question

In: Computer Science

In C write a program that asks the user for the dimensions of 3 sides of...

In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle

1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions: o Function to validate the range of values required for each side o Function to validate if the triangle is a valid triangle o Function to calculate the area 3. These functions must be written as generic as possible. They must not display anything 4. Must use C math functions 5. Input values must be between 1.00 and 100.00 including 1.00 and 100.00 6. Only one digits after the decimal point for your output

Solutions

Expert Solution

Code:

#include<stdio.h>
#include<math.h>
int valid_sides(float a,float b,float c)
{
   /*Here we return 1 for valid 0 for invalid*/
   return ((a>=1.0 && a<=100.0)&&(b>=1.0 && b<=100.0)&&(c>=1.0 && c<=100.0));
  
}
int triangle_valid(float a,float b,float c)
{
   /*If sum of two sides are greater than other then the triangle is valid*/
   return(a+b>c && b+c>a && a+c>b);
}
void area_perimeter(float a,float b,float c)
{
   /*By using herons formula we calculate the area of the triangle*/
   float s=(a+b+c)/2,area;
   printf("Perimeter of the triangle is: %.1f\n",a+b+c);
   /*Here we print the perimeter of triangle*/
   area=sqrt(s*(s-a)*(s-b)*(s-c));
   /*Here we calculate and print the area of the triangle*/
   printf("Area of the triangle is: %.1f",area);
      
}
int main()
{
   float a,b,c;
   printf("Enter the three sides of the triangle: ");
   scanf("%f%f%f",&a,&b,&c);
   /*Reading sides form the user*/
   if(valid_sides(a,b,c))
   /*here we call valid sides to check wheter sides are valid or not*/
   {
       if(triangle_valid(a,b,c))
       /*Here we check wheter the triangle is valid or not*/
       {
           /*If boath sides and triangle are valid then only we calculate area
           and perimeter*/
           area_perimeter(a,b,c);
       }
       else
       {
           /*If triangle is not valid we print not a valid triangle*/
           printf("Triangle is not valid");
       }
   }
   else
   {
       /*If sides are not valid we print invalid sides*/
       printf("Sides are not valid");
   }
  
}

Output:

Indentation:


Related Solutions

Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
Write a c++ program that asks the user for a date (Month and Day only) and...
Write a c++ program that asks the user for a date (Month and Day only) and displays the season in this day.The program asks then the user if he needs to enter another date, if he answers with ’Y’ or ’y’, the programwill take another date from the user and displays the season in this day.The program will keep repeating this until the user enters a character different than ’Y’ and ’y’.•Fall starts in September 21st•Winter starts in December 21st•Spring...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
Coding in C++: For this verse, you need to write a program that asks the user...
Coding in C++: For this verse, you need to write a program that asks the user to input the inventory id number (integer) and the price (float) for 5 inventory items. Once the 5 inventory items are input from the user, output the results to the screen. Please ensure you use meaningful names for your variables. If your variables are not named meaningfully, points will be deducted.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT