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...
Write a program (polygon.py) that asks the user to enter the number of sides in a...
Write a program (polygon.py) that asks the user to enter the number of sides in a regular polygon. For example, an equilateral triangle is a regular 3-sided polygon, a square is a regular 4-sided polygon, and a pentagon is a regular 5-sided polygon. If a user enters a number of sides between 3 and 25, inclusive, draw the polygon and wait for the user to click on the screen to quit the program. If the user enters a number less...
Write a C ++ program that asks the user for the speed of a vehicle (in...
Write a C ++ program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------- 1           40 2           80...
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)....
Write a c++ program that asks the user to choose his preferred shape, and asks then...
Write a c++ program that asks the user to choose his preferred shape, and asks then for two numbers (positive but not necessary integers) in order to calculate the shape's area. The possible shapes are: Triangle, Ring and Rectangle. If the user chose a different shape, you will warn him and wait for a valid shape. The area of a circle is πr12-r22 (suppose π= 3.4 ). The area of a triangle is base*height/2. The area of a rectangle is...
c program Write a program that asks the user to enter a sequence of 15 integers,...
c program Write a program that asks the user to enter a sequence of 15 integers, each either being 0, 1, or 2, and then prints the number of times the user has entered a "2" immediately following a "1". Arrays are not allowed to appear in your code. Include ONLY THE SCREENSHOT OF YOUR CODE in an image file and submit the file.
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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT