September 1998
Intermediate to advanced
848 pages
20h 13m
English
Let's start with a simple example of an if statement shown in Listing 7.1. This program reads in a list of daily low temperatures (in Celsius) and reports the total number of entries and the percentage that were below freezing. It uses scanf() in a loop to read in the values. Once during each loop cycle, it increments a counter to keep track of the number of entries. An if statement detects temperatures below freezing and keeps track of their number separately.
/* colddays.c -- finds percentage of days below freezing */ #include <stdio.h> #define SCALE "Celsius" #define FREEZING 0 int main(void) { float temperature; int cold_days = 0; int all_days = 0; printf("Enter the list of daily low ... |
Read now
Unlock full access