Docsity

Lab 2: Data Types & Arithmetic Exercises, Lab Reports of Computer Science

Toggle sidebar
bg1
Lab 2: Data Types & Arithmetic
Today’s lab has us playing with data types.
Problem 0:
Which of the following are valid variable names? Feel free to
compile and check for errors.
int 4variable;
int intVariable;
int variable;
int variable 1;
int variable ;
int i 4;
Problem 1:
double a = 1.0e2;
double b = .01;
printf(”a in scienfitic %e\n”,a);
printf(”b in scienfitic %e\n”,b);
printf(”a in standard %f\n”,a);
printf(”b in standard %e\n”,b);
Record the output from the above code:
Problem 2:
Given:
int a = 1;
int b = 2;
int c = 3;
int result;
For each expression write down what you expect to get as the
solution. Then code the expression and write down what out-
put of the program is using:
printf(”Output %d\n”, result );
(1) result =a−5∗b/c + 1
Expected:
Executed:
(2) result =a−5∗b/(c+ 1)
Expected:
Executed:
(3) result =a−5∗(b/c)+1
Expected:
Executed:
(4) result = (a−5) ∗b/c + 1
Expected:
CPS 196: Introduction to C (Summer 2009) Page 1
pf2

Partial preview of the text

Download Lab 2: Data Types & Arithmetic Exercises and more Lab Reports Computer Science in PDF only on Docsity!

Lab 2: Data Types & Arithmetic

Today’s lab has us playing with data types.

Problem 0: Which of the following are valid variable names? Feel free to compile and check for errors.

int 4variable;

int intVariable;

int variable;

int variable 1;

int variable ;

int i 4;

Problem 1: double a = 1.0e2; double b = .01; printf(”a in scienfitic %e\n”,a); printf(”b in scienfitic %e\n”,b); printf(”a in standard %f\n”,a);

printf(”b in standard %e\n”,b);

Record the output from the above code:

Problem 2: Given: int a = 1; int b = 2; int c = 3; int result; For each expression write down what you expect to get as the solution. Then code the expression and write down what out- put of the program is using: printf(”Output %d\n”, result );

(1) result = a − 5 ∗ b/c + 1 Expected: Executed:

(2) result = a − 5 ∗ b/(c + 1) Expected: Executed:

(3) result = a − 5 ∗ (b/c) + 1 Expected: Executed:

(4) result = (a − 5) ∗ b/c + 1 Expected:

CPS 196: Introduction to C (Summer 2009) Page 1

Lab 2: Data Types & Arithmetic

Executed:

(5) result = a − 5 ∗ b/c + 1 Expected: Executed:

Problem 3: Execute the following code and record the result: int x = 5 / 0; Result:

Problem 4: Write a program that squares double input from the user (via scanf) and displays the result.

Problem 5: Write a program to converts a degrees in Fahrenheit (F) to Celsius (C) using the formula:

C =

F − 32

Get the value of F from the user via scanf.

Problem 6: Write a program to evaluate the polynomial:

3 x^3 − 5 x^2 + 6 Get the value of x from the user via scanf.

CPS 196: Introduction to C (Summer 2009) Page 2