Monday, October 2, 2017

Day 2 - Input/Output

AGENDA:

  1. Modify the massgrave.c program
  2. Complete Amino Acid Molecular Weights program
OVERVIEW:
Today we learned about the priority of operators. Priority of operators is important because much like the order of operations in mathematics(PEMDAS), the way that we write an equation in C can drastically change the way it is evaluated. As of today, the following graph illustrates the precedence of the operators we have been introduced to thus far:
This chart means that parentheses are evaluated first. If there are other operations within the parentheses, then those operations take precedence over anything outside them. The next items evaluated are unary operators. These include the increment and decrement operators: n++ and n--. The next items evaluated are the binary operators such as multiplication(*), division(/), and the modulus operator(%); where binary addition and subtraction are evaluated last. 

We also learned about the concepts of overflow and underflow. These concepts are pertinent to coding in C because our computer only allows a specific range of values for computations. If we exceed those limits, then we may get an error, a zero, or a nonsensical integer in our program. For example, we learned in class that integers in C can only go from -32,768 to 32,768. These values correspond to 16 bits of information---or 2^16 numerical values. If our calculations exceed the specified values, then we receive an overflow error. If our calculations are below the specified range, then we receive an underflow error.

Our class also learned more about the proper syntax behind printf() statements and scanf() statements. Consider the following example:

/*-Degrees2Radians----------------------------------*/
/*----This program takes user input in degrees and--*/
/*----computes the angle measure in radians---------*/
//Call the necessary preprocessing directives
#include <stdio.h>
#include <math.h>
//Declare a symbolic constant for pi
#define PI 3.141593

int main (void) {
//Declare and initialize variables
double angle_rad, angle_deg;
//Ask for user input from keyboard in degress
printf("Input values in degrees: \n");
scanf("%lf", &angle_deg);
//Compute the angle measure conversion
angle_rad = (angle_deg*PI)/180;
//Print out the angle measure in radians
printf("Angle = %5.4f radians \n", angle_rad);
//Ends program
return 0;
}

Let's break it down below:
Consider the scanf() statement highlighted in green. scanf() statements are utilized to take user input from the keyboard. In essence, it takes values inputted by the user and stores them into a variable of the coder's choosing. It takes two arguments: a control string in double quotation marks and memory location---indicated by the ampersand(&) symbol---for the variable itself. In the control string, the coder must input a type specifier---e.g. %i for int, %f for float,%u for unsigned int, etc--- to determine the type of variable the user will type. I chose %lf for long float because my variable angle_deg is a double. I used the following chart to find the appropriate  type specifiers for my variables:

Now consider the printf() statement in blue. As the name suggests, this function's purpose is to print out statements of the coder's choosing. Much like scanf(), the printf() statement also has a control string; the major difference being that you can input text to be displayed within the control string. The \n at the end of the control string is called the newline operator. This lets the program know to move over to a new line in the display window after that part of the code has been run. The number in front of the type specifier---%5.4f--- does two things: (a) the 5 tells the program the width of the number and (b) the 4 tells the number of decimal places after the decimal point. After the control string, there is a comma to separate itself from the next argument: the variable itself. More specifically, it tells the program that you want it to display the value stored in the variable angle_rad in place of the type specifier in the control string.

So using this information, we can predict exactly what the program will display given these statements and a degree measure of 30.

Input values in degrees: from printf()
30                       from scanf()
Angle = 0.5236 radians   from printf()

Let's see if repl.it agrees with out prediction:

Looks like our prediction was correct.


CLASSWORK:
Height from Bone Length:
In class, we created and examined a program that estimated the height of a human being based on the  length of their femur and the length of their humerus. First, we created a decomposition outline. Next, we wrote pseudocode that emulated the steps we would take to write the program. Lastly, we wrote the program.




HOMEWORK:
Bone to Height Modifications:
For homework, I made five different modification to this program from class. However, I just want to share one modification in this blog because I think it was the most interesting. The goal:  Modify the program so that it reads the input values in inches and then estimates the output heights using feet and inches. (Note that you are printing two output values for each height estimate).

/*----------------------------------------------------------------*/
/*  Bone2Height  HW 2 - #3                                        */
/*  This program estimates the height                             */
/*  of a person based on bone length                              */
#include <stdio.h>
#include <math.h>

int main(void)
{
  /*  Declare and initialize variables. */ 
  float humerus, femur;
  /* m = male, f = female, ht = height, in = inches, ft = feet, 
     rem = remainder */

  /* These variables give the whole height in inches */
  float m_femur_ht_in, m_humerus_ht_in, f_femur_ht_in,                   f_humerus_ht_in;
  /* These variables give the leftover height in inches */
  float m_femur_rem_in,m_humerus_rem_in, f_femur_rem_in,                 f_humerus_rem_in;
  /* These variables give the whole number height in feet */
  float m_femur_ht_ft, m_humerus_ht_ft, f_femur_ht_ft,                   f_humerus_ht_ft;
  
  /*  Receive user input from keyboard. */
  printf("Enter Values in Inches: \n");
  printf("Length of femur? \n");
  scanf("%f", &femur);
  printf("Length of humerus? \n");
  scanf("%f", &humerus);
  
  /*  Height estimation from femur length in Inches */
  m_femur_ht_in = ((femur * 1.88) + 32);
  f_femur_ht_in = ((femur * 1.94) + 28.7);
  /*  Height estimation from humerus length in Inches */
  m_humerus_ht_in = ((humerus * 2.9) + 27.9);
  f_humerus_ht_in = ((humerus * 2.8) + 28.2);
  
  /* Remainder of height in Inches */
  m_femur_rem_in = fmod(m_femur_ht_in, 12);
  f_femur_rem_in = fmod(f_femur_ht_in, 12);
  m_humerus_rem_in = fmod(m_humerus_ht_in, 12);
  f_humerus_rem_in = fmod(f_humerus_ht_in, 12);
  
  /* Height estimation from femur in Feet */
  m_femur_ht_ft = (m_femur_ht_in - m_femur_rem_in) / 12;
  f_femur_ht_ft = (f_femur_ht_in - f_femur_rem_in) / 12;
  /* Height estimation from humerus in Feet */
  m_humerus_ht_ft = (m_humerus_ht_in - m_humerus_rem_in) / 12;
  f_humerus_ht_ft = (f_humerus_ht_in - f_humerus_rem_in) / 12;
  
  /*Print out height estimations */
  printf("\nHeight Estimates in Feet and Inches:\n");
  printf("Femur Male Height: %5.1f feet %5.1f inches\n",                 m_femur_ht_ft, 
     m_femur_rem_in);
  printf("Femur Female Height: %5.1f feet %5.1f inches\n",               f_femur_ht_ft, 
     f_femur_rem_in);
  printf("Humerus Male Height: %5.1f feet %5.1f inches\n",               m_humerus_ht_ft, 
     m_humerus_rem_in);
  printf("Humerus Female Height %5.1f feet %5.1f inches\n",               f_humerus_ht_ft, 
     f_humerus_rem_in);
  /*  Exit program */
  return 0;
}

This program took me the longest to complete out of all of the modifications because I had trouble trying to calculate the remainder of a floating point value. First, I tried the modulus operator, but it wouldn't work with floating point values. So I searched online for other methods when I came across this function called fmod(). It functioned just like the modulus operator only that it worked with floating point values. Much like the modulus operator, it takes two arguments A and B. In practice, I've found that:

fmod(A,B) = A%B where A and B are floats for fmod and ints for %.

Here is the output of the program:
If we compare the values we obtained from the previous iteration---under classwork section--- to this program, we can see that our height estimates are exactly the same, but in a different form.

Amino Acid Molecular Weights:
This program asks for the user to input the number of atoms of each of the five elements (O, C, N, S, H) and output the molecular weight of its corresponding amino acid. I used this chart for reference.

Below is the code I used wrote for this assignment:

/*----------------------------------------------------------------*/
/*  Molecular_Weight HW 2.5 #1                                    */
/*    This program computes the molecular                         */
/*    weight of glycine                                           */
#include <stdio.h>
#include <math.h>

/*  Assigns the molecular weights of common elements on periodic table*/
#define O 15.9994
#define C 12.011
#define N 14.00674
#define S 32.066
#define H 1.00794

int main(void)
{
  /*  Define and initialize variables */
  int oxygen, carbon, nitrogen, sulfur, hydrogen;
  double mol_weight;
  /*  Get user input from keyboard */
  printf("Enter Whole Number Values:\n");
  printf("Number of Oxygen atoms?\n");
  scanf("%i",&oxygen);
  printf("Number of Carbon atoms?\n");
  scanf("%i",&carbon);
  printf("Number of Nitrogen atoms?\n");
  scanf("%i",&nitrogen);
  printf("Number of Sulfur atoms?\n");
  scanf("%i",&sulfur);
  printf("Number of Hydrogen atoms?\n");
  scanf("%i",&hydrogen);
  /*  Calculate the molecular weight */
  mol_weight = (O*oxygen) + (C*carbon) + (N*nitrogen) + (S*sulfur) + (H*hydrogen); 
  /*  Print molecular weights */
  printf("\nAmino Acid Molecular Weight:\n");
  printf("Glycine: %f grams", mol_weight);
  return 0;
  
Here is the code in action within the display window:

Right now, it only displays Glycine. However, if I add the following lines of code into the aforementioned program, then it should output the molecular weight with the name of its corresponding amino acid. This excerpt of code assigns each amino acid a unique 5 digit code based on the number of molecules for each of the five elements.

int code;
code = (oxygen*10000) + (carbon*1000) + (nitrogen*100) + (sulfur*10) + hydrogen

if (code==23107) {
printf("Alanine: %f grams", mol_weight);

else if (code==26415) {
printf("Arginine: %f grams", mol_weight);

...
else if (code==26113) {
printf("(Iso)leucine: %f grams", mol_weight);

...
else if (code==25121) {
printf("Methionine: %f grams", mol_weight);

...
else if (code==31211) {
printf("Tryptophan: %f grams", mol_weight);

... etc.

I tried out my code in repl.it, and made a few tweaks:

The final else statement on line 97 is there to ensure that the program knows what to print out if none of the other else if statements are true. I added a second condition to some of my if statements to ensure that users didn't type an erroneous combination of elements and also yield a molecular weight.
It works just as I intended it to:
Working correctly
Redundancy Check




No comments:

Post a Comment