/* File: maxabs.c
Programmer: Programmer Name
Date: Current Date
This program reads in a sequence of integers until an end of file.
Among the numbers read, the program determines the largest absolute value.
*/
#include <stdio.h>
int absolute(int n);
main()
{ int largest = 0,
n, flag;
printf("***Largest Absolute Integer***\n\n");
printf("Type integers, EOF to quit: "
"^Z for DOS, ^D for Unix\n");
flag = scanf("%d", &n);
while (flag != EOF) {
if (absolute(n) > largest)
largest = absolute(n);
flag = scanf("%d", &n);
}
printf("Largest absolute value = %d\n", largest);
}
/* Function returns the absolute value of n */
int absolute(int n)
{
if (n < 0)
return -n;
else
return n;
}
/* File: niceday.c
Programmer: Programmer Name
Date: Current Date
This program counts the number of nice days in a set of high
temperature data.
*/
int nice_day(int temp);
void print_results(int nice, int bad, int temp_sum);
main()
{ /* declarations */
int temperature, /* daily temperature */
total = 0, /* cumulative total */
num_nice_days = 0,
num_bad_days = 0;
/* print title and prompt */
printf("***Count Nice Days***\n\n");
printf("Type daily high temperature readings (0 to quit): ");
/* read the first temperature */
scanf("%d", &temperature);
while (temperature != 0) {
/* process one temperature */
if ( nice_day(temperature))
num_nice_days = num_nice_days + 1;
else
num_bad_days = num_bad_days + 1;
/* accumulate total of temperatures */
total = total + temperature;
/* read next temperature */
scanf("%d", &temperature);
}
print_results(num_nice_days, num_bad_days, total);
}
/* File: niceday.h
Programmer: Programmer Name
This file contains the definitions of macros and prototypes
for functions used by the niceday program.
*/
#define TOO_COLD 80
#define TOO_HOT 90
#define HOT_DAY(t) ((t) > TOO_HOT)
#define COLD_DAY(t) ((t) < TOO_COLD)
#define ANY_DAYS(n,b) (((n) + (b)) > 0)
int nice_day(int temp);
void print_results(int nice, int bad, int temp_sum);
/* File: niceday2.c
Programmer: Programmer Name
Date: Current Date
This program counts the number of nice days in a set of high
temperature data.
*/
#include <stdio.h>
#include "tfdef.h"
#include "niceday.h"
main()
{ /* declarations */
int temperature, /* daily temperature */
total = 0, /* cumulative total */
num_nice_days = 0,
num_bad_days = 0;
/* print title and prompt */
printf("***Count Nice Days***\n\n");
printf("Type daily high temperature readings (0 to quit): ");
/* read the first temperature */
scanf("%d", &temperature);
while (temperature != 0) {
/* process one temperature */
if ( nice_day(temperature))
num_nice_days = num_nice_days + 1;
else
num_bad_days = num_bad_days + 1;
/* accumulate total of temperatures */
total = total + temperature;
#ifdef DEBUG
printf("debug: %d temps read, total = %d\n",
num_nice_days + num_bad_days, total);
#endif
/* read next temperature */
scanf("%d", &temperature);
}
print_results(num_nice_days, num_bad_days, total);
}
/* Function to test for a nice day given the temperature */
int nice_day(int temp)
{
if( COLD_DAY(temp)) return FALSE;
if( HOT_DAY(temp)) return FALSE;
return TRUE;
}
/* Function to print results given number of nice and bad days */
/* and total of temperatures */
void print_results( int nice_days, int bad_days, int total)
{
float average_temp;
printf("There were %d nice days and %d bad days\n",
nice_days, bad_days);
if ( ANY_DAYS(nice_days,bad_days)) {
average_temp = (float) total / (float) (nice_days + bad_days);
printf("The average temperature for %d days was %f\n",
nice_days + bad_days, average_temp);
}
}
/* File: pay5.c
Programmer: Programmer Name
Date: Current Date
The program gets payroll data, calculates pay, and prints out
the results for a number of people. A separate function is used
to calculate total pay.
*/
#define REG_LIMIT 40.0
#define OT_FACTOR 1.5
main()
{
/* declarations */
int id_number;
float hours_worked, rate_of_pay, total_pay;
float calc_pay(float hours, float rate);
/* print title */
printf("***Pay Calculation***\n");
/* initialize loop variables */
printf("\nType ID Number, zero to quit: ");
scanf("%d", &id_number);
while (id_number > 0) {
/* read data into variables */
printf("Hours Worked: ");
scanf("%f", &hours_worked);
printf("Hourly Rate: ");
scanf("%f", &rate_of_pay);
/* calculate pay */
total_pay = calc_pay(hours_worked, rate_of_pay);
/* print data and results */
printf("\nID Number = %d\n", id_number);
printf("Hours Worked = %f, Rate of Pay = $%6.2f\n",
hours_worked, rate_of_pay);
printf("Total Pay = $%10.2f\n", total_pay);
/* update loop variables */
printf("\nType ID Number, zero to quit: ");
scanf("%d", &id_number);
}
}
/* Function calculates and returns total pay */
float calc_pay(float hours, float rate)
{ float regular, overtime, total;
printf("\ndebug:entering calc_pay(): hours = %f, rate = %f\n",
hours, rate);
if (hours > REG_LIMIT) {
regular = REG_LIMIT * rate;
overtime = OT_FACTOR * rate * (hours - REG_LIMIT);
}
else {
regular = hours * rate;
overtime = 0;
}
total = regular + overtime;
printf("debug:returning from calc_pay(): %f\n", total);
return total;
}
/* File: tfdef.h
Programmer: Programmer Name
This file contains the definitions of TRUE and FALSE
*/
#define TRUE 1
#define FALSE 0