Solutions to Homework 2

[ Problem 3.10 ] [ Problem 6.5 ] [ Problem 7.8 ]


Problem 3.10

The details of main.c are as follows:

/*
 *  ==================================================
 *  Compute and print force due to wind gusts 
 *  ==================================================
 */

#include <stdio.h>

int main ( void ) {
float fTime;
float WindForce( float );

    printf("     Time      Thrust\n");
    printf("(seconds)        (kN)\n");
    printf("=====================\n");

    for (fTime = 0.0; fTime <= 3.0; fTime = fTime + 0.25)
         printf(" %8.2f %11.2f\n", fTime, WindForce(fTime)) ;
}

The details of windforce.c are as follows:

/*
 *  ================================================
 *  Compute wind force..
 *  ================================================
 */

#include <math.h>

float WindForce ( float fTime ) {
float fWindForce, fT;

   fT = fTime - floor ( fTime );
   if ( fT <= 0.3 )
        fWindForce = 4 + 15.0*fT - 135.0*pow(fT, 3.0);
   else
        fWindForce = (731.0 - 171*fT )/140.0;

   return fWindForce;
}


Problem 6.5

/*
 *  ==============================================================
 *  prog_dectobinary.c -- Convert an integer in base 10 to base 2.
 *
 *  Written By: David Mazzoni and Mark Austin         October 1996
 *  ==============================================================
 */

#include <stdio.h>
#include <math.h>

/* 
 *  ===============================================
 *  Return the power of iBase to iPow for iPow >= 0
 *  ===============================================
 */

int power( int iBase, int iPow ) {
int iMult = 1;

  while( iPow > 0 ) {
    iMult *= iBase;
    iPow--;
  }
  return iMult;

}

int main ( void ) {
int iVal;
int iDig;
  
  printf("Enter a decimal value from 0..255 -->" );
  scanf( "%d%*c", &iVal );  
  printf("\nThe entered number is %i\n", iVal );

  for( iDig = 7; iDig >= 0; iDig-- ) {
    if( iVal / power( 2, iDig ) == 1 )
      putchar('1');
    else
      putchar('0');
    iVal = iVal % power( 2, iDig );
  }
    
  printf( "\nHit ..." );
  getchar();
  return 0;


Problem 7.8

/*
 *  ======================================================================
 *  prog_cuberoot.c : Compute cube root of a number           
 *
 *  Written By : Rachel Albrecht and Mark Austin              October 1996
 *  ======================================================================
 */

#include <stdio.h>
#include <math.h>

int main(void){
float fN, fCr;
float CubeRoot(float fN);

   /* [a] : prompt user for number */

   printf("\n");
   printf("Please enter a value of N : ");
   scanf("%f%*c",&fN);
   printf("The value of N is : %f\n",fN);

   /* [b] : compute and print cube root */

   fCr = CubeRoot (fN);
   printf("The cube root of N is : %f\n",fCr );
}

/*
 *  ======================================================================
 *  CubeRoot() : Compute cube root of a number           
 *
 *  Input  : float fPassed -- Number that cube root is to be computed.
 *  Output : float fX      -- Cube root of fPassed.
 *  ======================================================================
 */

#define EPSILON 0.00001

float CubeRoot(float fPassed) {
int iCount = 0, iConvergenceAchieved;
float fX, fXnext;

   /* [a] : No iteration is required for trivial cases */

   if (fPassed == 1 || fPassed == 0 || fPassed == -1 ){
       return fPassed;
   }

   /* [b] : Compute cube root */

   fX = fPassed/2.0;  /* initial guess for cube root of fPassed */

   iConvergenceAchieved = 0;
   while(iConvergenceAchieved == 0) {

        /* [b.1] : Compute next estimate of cube root */

        fXnext = (2*fX*fX*fX + fPassed)/(3*fX*fX);

        /* [b.3] : Has algorithm converged */

        if(fabs((fXnext -fX)/fX) < EPSILON )
           iConvergenceAchieved = 1;

        /* [b.3] : Update variables */

        fX     = fXnext;
        iCount = iCount + 1;
   }

   /* [c] : Print results on convergence and return result */

   printf("Cube Root algorithm converged in %3d iterations\n", iCount );

   return fX;
}

Example : The script of code

     prompt >>
     prompt >> CUBEROOT
     Please enter a value of N : 20
     The value of N is : 20.000000
     Cube Root algorithm converged in 7 iterations
     The cube root of N is : 2.714417
     prompt >>

shows the required program input and output for computing the cube root of 20.