CS110-Lab 8
CS110-Lab 8
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.
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)
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;
}
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
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;
}