Vous êtes sur la page 1sur 7

SIMPLE ARITHMETIC

LAB 03
SECTION U

SUBMITTED BY:
RICHARD MELNICK
SUBMISSION DATE:
9/25/2014

Problem:
This lab asked us to determine basic syntax errors in code to preform simple arithmetic. It
then asked us to compile code of certain simple arithmetic programs. First we had to figure out
the best approach to putting this code in, and then we had to discover the semantics errors in the
code itself, due to issues with integer and floating point math. Next we had to create equations to
solve slightly more complex problems, such as solving for area. Finally, we looked at a more
complicated program that took input from the Esplora, and we had to analyze it.
Analysis:
The first problem stated that we had to determine the syntax errors in the basic code. We
had to determine what solution would be best and then how to fix it. Next, we had to determine
the most efficient way to get our program to run the simple math, and we had to format the
output accordingly. The complex equations we created required that we use mathematical
formulas and convert them to C language in order to put them into our program. The best way to
do this was with constants and variables. Finally, we had to determine how the equation for
magnitude of the Esplora was relevant to analyzing the output, which required looking at the
input the program itself was getting from the Esplora.
Design:
The first solution was simple: Replace the syntax errors. The second was also fairly
simple, we had to write code for basic arithmetic. We were able to use the code we already had,
along with the sample output, to format this. The complex equations required that we break down
the formula into constants, such as conversion factors, and then use variables to solve. Finally,
the Esplora problem required that we determine exactly what the input was before we were able
to correctly analyze the equation.
Testing:
We found multiple solutions to the first problem, and we had to test which made the most
sense. For example, you could have replaced the floating point division with integer division
instead of changing the format specifier, but that would have simply given a useless answer. We
also had to test our formulas for the complex equations with our own calculators to ensure we
had made no syntax errors. Finally, in order to analyze the Esplora equation, we had to test the
program itself in multiple ways.
Comments:
In this lab I learned that the way the program runs through equations is not always the
same logical way a person would think. I realized that the computer cannot distinguish between a
reasonable answer and an unreasonable one, and we have to be careful. This only enforced the
idea that we must check our work thoroughly.

Problem 1:
1. // CprE 185: Lab 3
2. // Problem 1: Mysterious Output
3. //ALL CHANGES TO CODING IN BOLD!
4.
5. #include <stdio.h>
6.
7. int main()
8. {
9.
int integerResult;
10.
double decimalResult;
11.
12.
decimalResult = 77 / 5;
13.
printf("The value of 77/5 is %d\n", decimalResult);
14.//Format specifier is for double (%lf) when it should be for int. This only
matters if you meant to
15.//do integer division. If you meant to use floating point division, we need to
use the variable
16.//decimalResult instead of integerResult.
17.
18.
integerResult = 2 + 3;
19.
printf("The value of 2+3 is %d\n, integerResult");
20.//Failed to pass integerResult as an argument in the printf statement.
21.
22.
decimalResult = 1.0 / 22.0;
23.
printf("The value 1.0/22.0 is %lf\n", decimalResult);
24.// Format specifier is for int (%d) when it should be for double. Technically
changing the variable
25.//to an int would solve the syntax error, but the result would be essentially
useless.
26.
27.
return 0;
28.}

Problem 2:

//The following is the code with our explanations for l, m, and n. The entire
program is not placed here.

void EquationMath(void)
{
// Declaring variable for l.
double circumference = 23.567;

double radius = 0.0;


double area = 0.0;

// Declaring variables and constants for m.


double feet = 14.0;
double meters = 0.0;
const double CONV_FACTOR_FT_TO_M = 0.3048; // Conversion factor from 1
foot to 0.3048 meters.

// Declaring variables for n.


double degF = 76.0;
double degC = 0.0;

// l. Calculate area of the circle


radius = circumference / (2.0 * M_PI); // Calculate the radius from the
circumference.
//
circumference = 2 * * radius, radius = circumference / (2 * )
area = M_PI * pow(radius, 2);
radius.

// Calculate the area from the


// area = *

radius^2
printf("l. The area of a circle with circumference of %.3lf is %.2lf (see the code
for the formula).\n", circumference, area);

// m. Convert feet to meters


meters = feet * CONV_FACTOR_FT_TO_M;

// Convert feet to meters.


// feet *

(meters / feet) = meters


printf("m. %.2lf ft = %.2lf m\n", feet, meters);

// n. Convert degrees F to degrees C.

degC = (degF - 32.0) / 1.8;


the given formula.

// Convert degrees F to degrees C with


// degC = (degF - 32) /

1.8)
printf("n. %.2lf F = %.2lf C\n", degF, degC);

return;
}

Raw output:
a. 6427 + 1725 = 8152
b. (6971 * 3925) - 95 = 27361080
c. 79 + 12 / 5 = 81.00
d. 3640.0 / 107.9 = 33.73
e. (22 / 3) * 3 = 21
f. 22 / (3 * 3) = 2
g. 22 / (3 * 3) = 2.00
h. 22 / 3 * 3 = 21.00
i. (22.0 / 3) * 3.0 = 22.00
j. 22.0 / (3 * 3.0) = 2
k. 22.0 / 3.0 * 3.0 = 22.00
l. The area of a circle with circumference of 23.567 is 44.20 (see the code for the
formula).
m. 14.00 ft = 4.27 m
n. 76.00 F = 24.44 C

a. Is correct.
b. Is correct.
c. The program did integer math for 12/5, giving 2 instead of 2.4. The correct
answer is 81.4.
d. Is correct to the specified precision.

e. The program did integer math for 22/3, giving 7 instead of 7.33 The correct
answer is 22.
f. The program did integer math for 22/9, giving 2 instead of 2.4 The correct
answer is 2.4
g. Same mistake as f, simply assigned answer to a double.
h. The program did integer math for 22/3, giving 7 instead of 7.33 The correct
answer is 22.
i. Is correct
j. The program assigned the result into an int, when it did all the math with
doubles. The correct answer is 2.4
k. Is correct.

Problem 3:

#include <stdio.h>
#include <math.h>
//declares necessary headers

int main() {
double x, y, z;
//declares necessary variables.
while (1) {
//unending while loop to ensure constant input.

scanf("%lf , %lf , %lf", &x, &y, &z);


//reads input from the Esplora, placing the data in variables x, y, and z.
printf("Magnitude of (%5.2lf,%5.2lf,%5.2lf) is: %6.2lf\n",
x, y, z, sqrt(x*x+y*y+z*z) );
//prints out the magnitude of the Esploras current position. This
expression, sqrt(x*x+y*y+z*z), could be useful in interpreting the outputs because
it is the equation of a concentric circle, which determines the position of the Esplora
on the x, y, and z axes. This can therefore be used to find much more data about
what is currently happening to the Esplora.
}
return 0;

Vous aimerez peut-être aussi