Chapter 7

arraydef.h

    /* File: araydef.h */
     #define MAX 20
     #define SIZE 100

paydef.h

/* paydef.h */
#define REG_LIMIT 40
#define OT_FACTOR 1.5

paytab.c

/*  File: paytab.c
    Other Source Files: payutil.c
    Header FIles: paydef.h, payutil.h
    Program calculates and stores payroll data for a number of id's. Gets
    data, calculates pay, and prints data for all id's.
*/
#include <stdio.h>
#include "paydef.h"
#include "payutil.h"
#define MAX 10

main()
{   int n, id[MAX];
    float hrs[MAX], rate[MAX], regpay[MAX], overpay[MAX];

    printf("***Payroll Program***\n\n");
    n = getdata(id, hrs, rate, MAX);
    calcpay(hrs, rate, regpay, overpay, n);
    printdata(id, hrs, rate, regpay, overpay, n);
}

payutil.c

     /* File: payutil.c */
     #include <stdio.h>
     #include "paydef.h"
     #include "payutil.h"
     /* Gets data for all valid id's and returns the number of id's */
     int getdata(int id[], float hrs[], float rate[], int lim)
     {   int n = 0;
         while (n < lim) {
              printf("ID <zero to quit>: ");
              scanf("%d", id + n);          /* id + n  is same as &id[n] */
              if (id[n] <= 0) return n;
              printf("Hours Worked: ");
              scanf("%f", hrs + n);         /* hrs + n is same as &hrs[n] */
              printf("Rate of Pay: ");
              scanf("%f", rate + n);        /* rate + n is same as &rate[n] */
              n++;
         }
         printf("No more space for data - processing data\n");
         return n;
     }
     /* Calculates regular and overtime pay for each id */
     void calcpay(float hrs[], float rate[], float reg[], float over[], int n)
     {   int i;
         for (i = 0; i < n; i++) {
              if (hrs[i] <= REG_LIMIT) {
                   reg[i] = hrs[i] * rate[i];
                   over[i] = 0;
              }
              else {
                   reg[i] = REG_LIMIT * rate[i];
                   over[i] = (hrs[i] - REG_LIMIT) * OT_FACTOR * rate[i];
              }
         }
     }

     /* Prints a table of payroll data for all id's. */
     void printdata(int id[], float hrs[], float rate[],
                             float reg[], float over[], int n)
     {   int i;
         printf("***PAYROLL: FINAL REPORT***\n\n");
         printf("%4s\t%5s\t%5s\t%6s\t%6s\t%6s\n", "ID", "HRS", "RATE",
                   "REG", "OVER", "TOT");
         for (i = 0; i < n; i++)
              printf("%4d\t%5.2f\t%5.2f\t%6.2f\t%6.2f\t%6.2f\n",
                        id[i], hrs[i], rate[i], reg[i], over[i],
                        reg[i] + over[i]);
     }

payutil.h

     /* File: payutil.h */
     int getdata(int id[], float hrs[], float rate[], int lim);
     void calcpay(float hrs[], float rate[], float reg[], float over[], int n);
     void printdata(int id[], float hrs[], float rate[],
                                  float reg[], float over[], int n);

scores.c

 /*  File: scores.c
    This program reads a list of integer exam scores and prints them out.
*/
#include <stdio.h>
#define MAX 100

main()
{   int exam_scores[MAX], index, n, count;

    printf("***List of Exam Scores***\n\n");
    printf("Type scores, EOF to quit\n");

    /* read scores and store them in an array */
    index = 0;
    while ((index < MAX) && (scanf("%d", &n) != EOF))
         exam_scores[index++] = n;
    count = index;

    /* print scores from the array */
    printf("\n***Exam Scores***\n\n");
    for (index = 0; index < count; index++)
         printf("%d\n", exam_scores[index]);
}

scores2.c

/*  File: scores2.c
    This program uses functions to read scores into an array and to print
    the scores.
*/
#include <stdio.h>
#define MAX 10

int read_intaray(int scores[], int lim);
void print_intaray(int scores[], int lim);
main()
{   int n, exam_scores[MAX];

    printf("***List of Exam Scores***\n\n");
    n = read_intaray(exam_scores, MAX);
    print_intaray(exam_scores, n);
}

/* Function reads scores in an array. */
int read_intaray(int scores[], int lim)
{   int n, count = 0;

    printf("Type scores, EOF to quit\n");

    while ((count < lim) && (scanf("%d", &n) != EOF)) {
         scores[count] = n;
         count++;
    }
    return count;
}

/* Function prints lim elements in the array scores. */
void print_intaray(int scores[], int lim)
{   int i;

    printf("\n***Exam Scores***\n\n");
    for (i = 0; i < lim; i++)
         printf("%d\n", scores[i]);
}

string.c

     /*  File: string.c
         This program reads characters until a newline, stores them in an
         array, and terminates the string with a NULL character. It then prints
         out the string.
     */

     #include <stdio.h>
     #include "araydef.h"

     main()
     {   char msg[SIZE], ch;
         int i = 0;

         printf("***Character Strings***\n\n");
         printf("Type characters terminated by a RETURN or ENTER\n");

         while ((ch = getchar()) != '\n')
              msg[i++] = ch;

         msg[i] = '\0';

         i = 0;
         while (msg[i] != '\0')
              putchar(msg[i++]);
         printf("\n");
     }

string2.c

/* File: string2.c
   This program reads and writes strings until an empty string is
   read. It uses functions to read and print strings to standard
   files.
*/
#include <stdio.h>
#define SIZE 100
void print_str(char s[]);
void read_str(char s[]);

main()
{  char str[SIZE];

   do {
     read_str(str);
     print_str(str);
   } while (str[0]);
}

/* Function reads a string from standard input until a newline is
   read. A NULL is appended.
*/
void read_str(char *s)
{  int i;
   char c;

   for (i = 0; (c = getchar()) != '\n'; i++)
     s[i] = c;
   s[i] = NULL;
}

/* Function prints a string to standard output and terminates with a
   newline.
*/
void print_str(char *s)
{  int i;

   for (i = 0; s[i]; i++)
     putchar(s[i]);
   putchar('\n');
}