Question to come later

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 ;
}

/************************************************/




Part (a)

printf("%.3lf\n", d );
printf("%+12.2lf\n", d );
printf("%012.4lf\n", d );
printf("%12.2e\n",d);


Part (b)


int i, j ;

for ( i=1; i<=4; i++)
{
for ( j=i; j < i + 8; j += 2 )
printf( "%4d", j );
printf("\n");
}



Part (c)

int get_even_max( unsigned values[ ], int numvalues )
{

unsigned max = 0, cnt ;
unsigned *ptr = values ;

for ( cnt = 0; cnt < numvalues; cnt++ )
{

if ( (*ptr % 2 == 0) && *ptr > max )
max = *ptr ;

ptr++ ;
}

return max ;

}



Part (d)

char *concatenate( char *str1, const char *str2 )
{
char *ptr1, *ptr2 ;

ptr1 = str1 ;

while ( *ptr1++ ) ;

ptr2 = str2 ;

while ( *ptr2 )
*ptr1++ = *ptr2++ ;

*ptr1 = '\0' ;

return str1 ;

}


Part (e)

#define PI 3.14159

...

double table[21][2] ;
int i ;
double A = 0.0 ;

for ( i=0; i < 21; i++ )
{
table[i][0] = A ;
table[i][1] = sin( A ) ;
A += PI * 0.1 ;
}

Part (f)

struct cplx {
double real, imag ;
} ;

struct cplx copycplx( struct cplx *p1, struct cplx *p2 )
{
struct cplx result ;

result.real = p1->real + p2->real ;
result.imag = p1->imag + p2->imag ;

return result ;
}


Part (g)

void binprint( unsigned int val)
{
unsigned mask ;
unsigned size = sizeof( unsigned ) * 8 - 1 ;

mask = 1 << size ;

while ( mask )
{
if ( val & mask )
putchar( '1') ;
else
putchar('0');

mask >>= 1 ;
}
}


Part (h)

...

int i ;
double *data, *ptr ;

data = (double *) malloc( sizeof( double) * 100) ;

if ( !data )
{
printf("Failed to allocate memory - terminating" ) ;
exit(1);
}

ptr = data ;

for ( i=1; i <= 100; i++ )
*ptr++ = (double) i ;

...

free( data ) ;


Part (i)

...

double data[10] = { ... } ;
double *ptr = data ;
int i ;
FILE *fp ;

fp = fopen( "data.txt", "w" );

if ( !fp )
{
printf("Failed to open / create file : data.txt" );
exit(1) ;
}

for ( i=1; i <= 10; i++ )
fprintf( "\n%d %.3lf", i, *ptr++ );

fclose( fp ) ;


Topic archived. No new replies allowed.