AGENDA:
- Sign up to Edmodo.com
- Register to repl.it to save our codes in C
- Create a blog and add Professor Mason as an author.
OVERVIEW:
Today, we were introduced to the computer programming language C and how we could use it to solve science and engineering problems. C is a relatively mature language, with approximately 40 years in the public domain. However, it has aged quite well considering its popularity among students and professionals, making it an invaluable language to learn for STEM applications. We also learned about the importance of decomposition outlines and how to create them to help us relate real-world problems to coding in C. More specifically, a decomposition outline is a problem-solving strategy that consists of five basic steps:
- State the problem clearly.
- Describe the input and output information.
- Work the problem by hand (or with a calculator) for a simple set of data.
- Develop a solution and convert it to a computer program.
- Test the solution with a variety of data.
We also learned about the basic structure of codes in C. In general, when you strip a program down to its bare bones you are left with these main components:
preprocessing directives
int main(void)
{
declarations;
statements;
}
Today, we used a decomposition outline to write pseudocode for a program to compute the distance between two points on a plane. We also learned about how to properly label identifiers in our program. Identifiers are important because they are what allow us to properly label variables within C without error.
CLASSWORK:
The distance between two points:
This program uses the Pythagorean theorem to calculate the distance between two points (x,y). You can change the coordinates in line 9 and it displays the answer when it is compiled. In hindsight, I could have added a scanf() statement to ask the user for input from the display window itself. I think that would have been much more user-friendly.
The following image shows the aforementioned code in action. It's funny looking back at my code because I notice the tiny mistakes that I made. For example, the /n is not supposed to be printed out like that. I was trying to use the newline operator \n in order to move the cursor to a new line. This mistake is a recurrent theme for my first few codes.
HOMEWORK:
Meters to Miles:
As the name suggests, this program converts meters into miles. You can change the number of meters on line 11 and when the program is compiled, it displays the number of miles traveled.

On line 11, I inputted 1760 meters--- which I figured was approximately equal to 1760 yards, and therefore 1 mile--- and I received an answer of 1.09, which is very plausible.
But just to be safe, I checked my answer on Google. Looks accurate.
Area of a Sector of a Circle:
This program takes keyboard input in degrees, makes the necessary conversion into radians, and outputs the sector area in the display window. In line 11, I declare pi as a constant. My approximation isn't super precise, but it will suffice for my purposes. Note to self: declare symbolic constants before the main code, it's much neater. For example, in line 7, I could have written:
#define PI 3.141593

For degrees, d = 30 and radius, r = 5, we obtain:
Other than that, I think this code turned out well. Although an if statement prohibiting negative values would have been a nice touch. After line 14, I could have written:
if (d < 0)
printf("Error: Negative distance makes no sense. \n");
else
printf("The area of the sector...
The alternative is the following. If you got to line 11 and let d equal some negative value, my program displays this:
Negative area is nonsensical thereby rendering my program unfinished.
This program computes the area of an ellipse given the semiaxes a and b. A quick Google search revealed that the semiaxes are just the radii with respect to both the x- and y-axes.
This is what my program outputs. Unfortunately, I don't know if there is a way to get rid of the text that reads: gcc version 4.6.3. I'll have to look into that later.
Much like a Celsius to Fahrenheit scale, this program takes degrees in Celsius and converts them into degrees Rankine.
Here is the what my program displays:
The reason my answer is displayed with three trailing zeros is that the float operator %f (see line 16) displays six decimal places after the decimal point. If I wanted to constrain the number of decimal places of this floating point value, then I would have to put a number in front of the "f". For example, if I replaced lines 15-16 with:
printf("The temperature is "
"%5.2f degrees Rankine \n," Rankine);
then my program would have displayed:
The temperature is 491.69 degrees Rankine
In this case, the 5 controls the total number of decimal places and the 2 controls the number of decimal places after the decimal point.










No comments:
Post a Comment