Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C The National Earthquake Information Center has asked you to wri

ID: 3622163 • Letter: P

Question

Programming in C The National Earthquake Information Center has asked you to write a program implementing the following decision table to characterize an earthquake based on its Richter scale number.

Richter Scale Number (n) Characterization
n < 5.0 Little or no damage

5.0 = n < 5.5 Some damage

5.5 = n < 6.5 Serious damage: walls may crack or fall

6.5 = n < 7.5 Disaster: houses and buildings may collapse

Higher Catastrophe: most buildings destroyed

Could you handle this problem with a switch statement? If so use a switch statement; if not explain why.

Explanation / Answer

#include /* printf, scanf definitions */void instruct(void);int main(void){ double number; /* input - the value of the earthquake on the Richter Scale *//*the following used to be able to repeat the program without closing the program itself*/ char again; char endline; for (again = 'y'; again != 'n'; scanf ("%c%c", &endline, &again) ) { instruct(); /* links to the instruct function to provide instructions to the user */ printf("Enter the number value of the earthquake on the Richter Scale: >"); /* user inputs the value of the earthquake on the Richter Scale */ scanf("%lf", &number); printf(" [You have entered a value of %.2f] ", number); /* echos number input back to user */ if (number < 5.0) printf("Based on an earthquake of %.2f on the Richter Scale; there will be little or no damage.", number); else if ( number >= 5.0 && number < 5.5) printf("Based on an earthquake of %.2f on the Richter Scale; there will be some damage.", number); else if ( number >= 5.5 && number < 6.5) printf("Based on an earthquake of %.2f on the Richter Scale; there will be serious damage: walls may crack or fall.", number); else if ( number >= 6.5 && number < 7.5) printf("Based on an earthquake of %.2f on the Richter Scale; there will be a disaster: houses and buildings may collapse.", number); else if (number > 7.5) printf("Based on an earthquake of %.2f on the Richter Scale; there will be a catastrophe: most buildings will be destroyed.", number); printf(" ******************** "); printf(" Repeat Problem? (y/n): > "); /* asking to repeat the problem, comes from p.286-p.287 of the text*/ }return (0);}void instruct(void){ printf(" **************************************** "); printf("This program will determine the level of "); /* program description for user */ printf("damage that will be sustained by an earthquake "); printf("of a given magnitude on the Richter Scale. "); printf("One input will be needed: "); printf("The value of the earthquake on the Richter Scale. "); printf(" ******************** ");}/* Answer: No, a switch statement can not be used for several reasons, mainly the input needs to be a double data type to support a decimal entry, however the switch statement can only be used for a char or int data type. */