Friday, March 4, 2011

User-Defined Functions

Develop a modular interactive program using functions that reads the values of three sides of a triangle and displays either its area or its perimeter as per the request of the user. Given the three sides a, b and c.
Perimeter = a +b +c
Area = sqrt((s-a)(s-b)(s-c))
where s = (a+b+c)/2

#include <stdio.h>
#include <math.h>
void perimeter(float a,float b,float c);
void area(float a,float b,float c);
float a,b,c;
void main()
{
int res;
clrscr();
printf("Enter the three sides of triangle\n");
scanf("%f %f %f",&a,&b,&c);
printf("If you want to perimeter press 1\n");
printf("Or You want to Area press 2:");
scanf("%d",&res);
if(res==1)
perimeter(a,b,c);
if(res==2)
area(a,b,c);
getch();
}
void perimeter(float a,float b,float c)
{
float p;
p=a+b+c;
printf("Perimeter of Triangle: %5.2f",p);
}
void area(float a,float b,float c)
{
float A,S;
S=(a+b+c)/2;
A=sqrt((S-a)*(S-b)*(S-c));
printf("Area of Triangle is: %5.2f",A);
}

OUTPUT
Enter the three sides of triangle
5  8  10
If you want to Perimeter press 1
Or you want to Area press 2 : 1
Perimeter of Triangle : 23.00

Enter the three sides of triangle
5  8  10
If you want to Perimeter press 1
Or you want to Area press 2 : 2
Area of triangle is : 5.84

3 comments:

  1. Develop a user defined function named ‘read_sides’ that obtains the three sides of a triangle from the user. Next, develop two user defined functions named ‘calc_perimeter’ and ‘calc_area’, respectively, to compute the perimeter and the area of the triangle. Finally, develop a user defined function named ‘display_triangle’ to display the perimeter and the area of the triangle. Call these functions suitably from your ‘main’ function to obtain the three sides of the triangle, to compute its perimeter and area, and to display the result.

    ReplyDelete
  2. question

    The test mark obtained by ten students is shown below:

    100 89 56 90 35 20 99 78 65 88

    (a) Write a user defined function in C to read the above data into an integer type array. Name the function as ‘read_array’. (b) Write a user defined function named ‘display_array’ that will display the integer data array on the monitor. (c) Develop a function named ‘deter_mean’ that will take an integer array, compute and return the mean value of the data array. (d) Incorporate the above functions into the ‘main’ function and display the mean value.

    ReplyDelete