May 28, 2013 at 2:32pm UTC
Write your question here.
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define LIMIT 0.999999999999999
#define MAXTERMS 100
double taylor( double x, double acc, int *terms ) ;
double evalf( double x );
/***************************************/
int main(int argc, char *argv[])
{
double xvalue, accuracy, logx ;
int check, num ;
char ch ;
FILE *fp = 0 ;
if ( argc == 2 )
fp = fopen( argv[1], "w" );
else
fp = fopen( "output.txt", "w" ) ;
if ( !fp )
printf("\nError opening output file - output will not be saved") ;
else
fprintf( fp, "\nX\t\tAccuracy\tTaylor\t\tLibrary\n" );
/************************************************/
printf("\n\nTaylor series computation of ln (1+x) ... \n\n" );
do {
_flushall();
check = 0 ;
while ( check !=2 )
{
printf("\n\nEnter the value x, -1 < x < 1, and the accuracy \nrequired in the approximation: " ) ;
check = scanf( "%lf %lf", &xvalue, &accuracy ) ;
_flushall() ;
if ( xvalue < -LIMIT || xvalue > LIMIT )
{
printf("\nValue of x is outside valid range - reenter values" );
check = 0 ;
}
}
/************************************************/
logx = taylor( xvalue, accuracy, &num );
printf( "\nX\t\tAccuracy\tTaylor\t\tLibrary" );
printf( "\n%10.8lf\t%10.8lf\t%10.8lf\t%10.8lf", xvalue,
accuracy, logx, evalf( xvalue ) );
if ( num == MAXTERMS )
printf( "\n\nSummation truncated at maximum computation limit, %d, without achieving desired accuracy", num );
else
printf( "\n\n%d terms were required to achieve accuracy", num );
if ( fp )
fprintf( fp, "\n%10.8lf\t%10.8lf\t%10.8lf\t%10.8lf", xvalue,
accuracy, logx, evalf( xvalue ) );
printf("\n\ncontinue (y/n):") ;
_flushall();
} while ( (ch = getch()) != 'n' ) ;
if ( fp )
fclose( fp ) ;
return 0 ;
}
/***************************************/
double evalf( double x )
{
return log( 1.0 + x ) ;
}
/***************************************/
double taylor( double x, double acc, int *terms )
{
double res, termn, xpowern ;
int n ;
res = xpowern = x ;
n = 2 ;
do {
xpowern *= -x ;
termn = xpowern / n ;
res += termn ;
n++ ;
} while ( fabs(termn) > acc && n < MAXTERMS) ;
*terms = n ;
return res ;
}
/***************************************/
/************************************************/
1(a)
double x, y ;
int pt, ret ;
printf("Enter coordinates using the format \"pt = (x, y)\" where pt is the coordinate # :");
ret = scanf( "%d = (%lf, %lf)", &pt, &x, &y) ;
if ( ret == 3 )
printf("\n\nPt# %d = (%.2lf,%.2lf)\n\n",pt, x,y);
else
printf("\n\nUnable to convert input successfully\n\n") ;
/************************************************/
1(b)
void triangle( unsigned int n )
{
unsigned int i, j ;
putchar('\n') ;
for (i=1; i <= n ; i++ )
{
for (j = 1; j <= i; j++ )
putchar('*') ;
putchar('\n') ;
}
putchar('\n') ;
}
/************************************************/
1(c)
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned val, temp, sum = 0;
printf("\nEnter integer value : ");
scanf( "%u", &val );
temp = val ;
while ( temp )
{
sum += temp % 10 ;
temp = temp / 10 ;
}
printf( "\n\nSum of digits in %u = %u\n", val, sum ) ;
system( "PAUSE" ) ;
}
/************************************************/
1(d)
char *my_strchr( const char *str, char ch )
{
char *ptr = str ;
while ( *ptr )
{
if ( *ptr == ch )
return ptr ;
ptr++ ;
}
return 0 ;
}
....
char string[] = "TestString" ;
char *p = my_strchr( string, 'r') ;
if ( p )
puts( p ) ;
else
puts( "Not Found" );
/************************************************/
1(e)
double evalp( double coeff[], double x, int n )
{
double *ptr = coeff ;
double value = 0.0, xval = x ;
int i ;
value = *ptr++ ;
for ( i=1; i < n; i++ )
{
value += xval * *ptr++ ;
xval *= x ;
}
return value ;
}
/************************************************/
1(f)
struct record {
char Name[20] ;
unsigned ID ;
double Grade ;
} ;
struct record *ptr, *p2 ;
struct record temp = {
"Blank Name", 99999, 0.00 } ;
int i ;
ptr = (struct record *) malloc( sizeof( struct record ) * 100 ) ;
p2 = ptr ;
for ( i=0; i< 100; i++)
*p2++ = temp ;
/************************************************/
1(g)
int *ptr, *ptr2 ;
int m ;
ptr = (int *) malloc( sizeof(int) * 100 );
if (!ptr)
{
printf( "\nERROR : Unable to allocate memory\n");
system( "PAUSE" ) ;
return -1;
}
ptr2 = ptr ;
for ( m = 0; m < 100; m++ )
*ptr2++ = 2 ;
ptr2 = ptr + 2 ;
for ( m=0; m < 20; m++ )
{
*ptr2 = 1 ;
ptr2 += 5 ;
}
free( ptr ) ;
/************************************************/
1(h)
double buff[10] = { 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.1,10.1} ;
FILE *fp;
double x ;
fp = fopen( "data.bin", "wb+" );
if ( fp == NULL )
{
printf("\nFailed to open file - data.bin\n");
system( "PAUSE" ) ;
return -1;
}
fwrite( buff, sizeof(double), 10, fp );
fseek ( fp, ( 2 * sizeof( double ) ), SEEK_SET ) ;
fread( &x, sizeof(double), 1, fp ) ;
fclose( fp ) ;
/************************************************/
1(i)
Problems:
(A) Loop counter range is 1 to 100 covering 100 elements instead of 10 in the array.
(B) C has zero based indexing so the index should run from 0 to 9 inclusive for a 10 element array.
(C) Pointer variable ptr is not initialised to point at the first array element:
ptr = array ;
required before loop.
double array[10], *ptr, x = 0.0 ;
int i ;
ptr = array ;
for ( i=1; i <= 10; i++)
{
*ptr++ = x / i ;
x += 0.01 ;
}
/************************************************/