Logo  

CS256 - Fundamentals of Structured Programming

Displaying exercises/e5/solution/h5.c

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

/**
 * Print the prompt "db? " and read a double (a decibel level) from the user
 * and using the below list of dB levels print a description of what that dB
 * represents, i.e. dB < 10 is "Threshold of Hearing", dB < 100 is "Large
 * Orchestra" and anything from 160 and above is "Instant Perforation of
 * Eardrum". Hint: Consider using an if-else chain.
 *
 * Description:				Decibel:
 * Threshold of Hearing			  0 dB
 * Rustling Leaves			 10 dB
 * Whisper				 20 dB
 * Normal Conversation			 60 dB
 * Busy Street Traffic			 70 dB
 * Vacuum Cleaner			 80 dB
 * Large Orchestra			 98 dB
 * Walkman at Maximum Level		100 dB
 * Front Rows of Rock Concert		110 dB
 * Threshold of Pain			130 dB
 * Military Jet Takeoff			140 dB
 * Instant Perforation of Eardrum	160 dB
 */


int main(void)
{
  double db;
  printf("db? ");
  scanf("%lf", &db);

  if (db < 10) printf("Threshold of Hearing\n");
  else if (db < 20) printf("Rustling Leaves\n");
  else if (db < 60) printf("Whisper\n");
  else if (db < 70) printf("Normal Conversation\n");
  else if (db < 80) printf("Busy Street Traffic\n");
  else if (db < 98) printf("Vacuum Cleaner\n");
  else if (db < 100) printf("Large Orchestra\n");
  else if (db < 110) printf("Walkman at Maximum Level\n");
  else if (db < 130) printf("Front Rows of Rock Concert\n");
  else if (db < 140) printf("Threshold of Pain\n");
  else if (db < 160) printf("Military Jet Takeoff\n");
  else printf("Instant Perforation of Eardrum\n");

  return 0;
}