You are on page 1of 5

# INDIAN INSTITUTE OF TECHNOLOGY PATNA

CS110-Lab 8
1.

Write a C Program to write sin(x) , cos(x) and tan(x) to a data file. Sample program is
shown in appendix (plot the data using Excel). Also plot a random number graph (use
rand()).
5 ( points)
2. Write your own functions for my_sin(x), my_cos(x),and my_exp using Taylor series
expansion . Test the functions appropriate test cases and compare with built in
functions. ( A sample sinx function is given in appendix)
10 ( points)
3.

Develop, and implement C program that reads N integer numbers and arrange them in
ascending order using one of the sorting techniques. Write the numbers to a file.
10 ( points)

4.

## An approximate empirical formula that relates atmospheric pressure to altitude is given

by: P(h)= k*exp(-0.12*h); where pressure P is in units of millibars (gm/cm^2), and
height h is in kilometers. The formula applies to heights less than about 80 km. . Write a
program that prints a table of pressures from sea level to 80 km in l0km steps for
different values of k ranging from 1000 to 1050. Plot the graph using Excel.
10 ( points)

5. Although C includes the hyperbolic functions among its intrinsic functions, it doesn't
include the inverse hyperbolic functions
sinh-1(x) = In[x + (x^2 + 1) 1/2]
cosh-1 (x) = In[x + (x^2 - 1)1/2]
tanh-1 (x) = In[(l + x)/(l - x)]/2
Write a program that displays the hyperbolic functions and their inverses, using our own
functions for the inverse functions.
10 ( points)
6. A sample program for calculating execution time is shown in appendix . By
appropriately modifying the above programs find their execution time. (optional:
Compare execution time using different Compilers)
5 ( points)

## The grading for this lab is as follows:

40 points for the program and demo ( as listed above) and 10 points report. Report should
contain the algorithm either in flow chart or in steps and the source code. Submit a single
document file (either in pdf or word). Report submission deadline is Monday 11.59 PM.
Submission.
Where to submit?
Email to : cs110.iitp@gmail.com
(use email subject as L8_your roll number ).

## Appendix: sin(x) to file

#include <stdio.h>
#include <math.h>
double Pressure(double height);
int main()
{
float height;
FILE *fp1;
float pi;
pi=(float) 22/7;
printf("Ploting functions\n");
printf(" sin(x)\n");
printf("-----------------\n");
fp1 = fopen("plot2.dat","w");
for(height=0; height<=2*pi; height+=.1){
fprintf(fp1,"%f
%f\n",height, sin(height));
printf("%f %f\n",height, sin(height));
}
printf("closing data files\n");
fclose(fp1);
return 0;
}

My_sinx function
double my_sinx( int ang_deg, int no_of_terms )
{
int term, j;
double value = 0.0, ang_rad = 0.0;
ang_rad = ( double ) ang_deg * PI / 180;
for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
{
value += ( double ) pow( -1.0, j ) * pow( ang_rad, term ) / fact( term );
}
return value;
}
int fact( int num )
{
int f = 0;
if ( num == 1 )
return 1;
else
f = num * fact( num - 1 );
return f;
}

## Calculate table of atmospheric pressure.

#include <stdio.h>
#include <math.h>
double Pressure(double height);
int main()
{
int height;
FILE *fp1;
printf("height pressure\n");
printf(" k in gm/cm'2\n");

printf("-----------------\n");
fp1 = fopen("plot11.dat","w");
for(height=0; height<=80; height+=1){
fprintf(fp1,"%d
%f\n",height,Pressure(height));
printf("%d %f\n",height,Pressure(height));
}
printf("closing data files\n");
fclose(fp1);
return 0;
}

pressure, gm/cm2

## double Pressure(double height)

{
return 1035*exp(-0.12*height);
}

Execution Timing

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CSEC (double)(CLOCKS_PER_SEC)
int main(void)
{
clock_t c1, c2;
int i, j;
c1 = clock();
for(i=0; i<100000000; i++)
j = i * 2;
c2 = clock();
printf("%f\n", (double)(c2-c1)/CSEC);
return 0;
}