www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback


/*---------------------------------------------------------------------+00010000
|                Copyright (c) 1996, SAS Institute Inc.                |
|                  Unpublished - All Rights Reserved                   |
|                      S A S / C   S A M P L E                         |
|                                                                      |00030100
|         NAME: PI4                                                    |00050000
|     LANGUAGE: C                                                      |00051000
|      PURPOSE: SAS/C DEBUGGER example program                         |00060000
|               This program computes pi/4 using a slowly converging   |00060000
|               infinite series for atan(1.0). The SIGINT signal can   |00060000
|               be generated to terminate summation or print an        |00060000
|               indication of progress so far.                         |00060000
|   MISC NOTES: This example is part of Debug Session 12 which shows   |00060000
|               how to use the attn command.                           |00060000
|               See SAS/C Debugger User's Guide and Reference Third    |00060000
|               Edition, Chapter 6, pg. 175.                           |00060000
| INPUT/OUTPUT: stdin/stdout                                           |
|               Since the input is stdin, you could type some lines in |00051000
|               a file and redirect stdin to the file. However, the    |00060000
|               simplest way to enter the lines is from the Termin     |
|               window.                                                |
|                                                                      |00030100
|   MVS -                                                              |00300000
| COMPILE,LINK: SUBMIT prefix.SAMPLE.AUX(PI4)                          |
|               make sure to use the =DEBUG compile option             |
|               where "prefix" is the installation defined high-level  |
|               qualifier for the SAS/C product.                       |
|      EXECUTE: execute under TSO, see below                           |
|   TSO -                                                              |
|      COMPILE: LC370 CLIST                                            |
|         LINK: CLK370 CLIST                                           |
|      EXECUTE: CALL your.load.lib(PI4) '=DEBUG'                       |
|   CMS -                                                              |
|      COMPILE: LC370 PI4 (DEBUG                                       |
|         LINK: CLINK PI4 (GENMOD                                      |
|      EXECUTE: PI4 =DEBUG                                             |
|                                                                      |00030100
+---------------------------------------------------------------------*/
#eject

#include <lcsignal.h>
#include <stdlib.h>
#include <stdio.h>

   /* This program computes pi/4 using a slowly converging    */
   /* infinite series for atan(1.0). The SIGINT signal can be */
   /* generated to terminate summation or print an indication */
   /* of progress so far.                                     */
   /*                                                         */
int iter;                /* number of iterations */
double sum = 0.0;        /* pi/4 value */
double lastterm = 2.0/3.0;

void attn(int);          /* attn handler */

void main()
{
signal(SIGINT, &attn);   /* define attention handler */

for(iter = 1; lastterm > 1.00E-6; ++iter) {
   sum += lastterm;      /* add in previous term */
   lastterm = 2.0/((iter*4 + 1)*(iter*4 + 3));
      /* compute next term */
   sigchk();             /* check for interruption */
   }

printf("pi/4 computed as %.10f in %d iterations\n",sum, iter);
exit(0);
}

void attn(int signum)

{
char reply(|20|);

printf("Do you want to terminate, or print a status report?  \n");
while(1) {
   printf("Enter \"t\" or \"p\" \n");
   fgets(reply, 20, stdin);

   switch(tolower(reply(|0|))) {
     case 't': printf("Execution terminated.\n");
               exit(1);
     case 'p': printf("Number of iterations = %d\n", iter);
               printf("Sum so far           = %.10f\n", sum);
               printf("Last term computed   = %.12f\n", lastterm);
               signal(SIGINT, &attn); /* Reinstate attn handling */
               return;
     default:  printf("Incorrect response.  Please correct.  \n");
               break;
      }
   }
}

Copyright (c) 2000 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement