Home     Mosaic     CLIP     Octave     R Project     Processing     Contact  
Search
<http://cnfolio.com/IntroComputingPractical03>
Introduction to Computing – B142L

B142L Computing practical – Working with numbers

Academic year 2009/10


Learning outcomes

  1. Identify C expressions, operators and operands.
  2. Use evaluation trees to indicate orders of precedence for specific expressions.
  3. Evaluate source code and write C programs that use arithmetic, assignment, comparison and logic operators.
  4. Practice using the printf() function to display output.
  5. Practice reading error messages from the compiler when debugging source code.



Exercise 1: Automatic data type conversion


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   int num1;
   float num2 = 4.25;
   printf( "num1 = %d and num2 = %f \n", num1, num2 );
   num1 = num2;
   printf( "num1 = %d and num2 = %f \n", num1, num2 );
   num2 = num1;
   printf( "num1 = %d and num2 = %f \n", num1, num2 );
}

What are the reasons for the displayed output?



The C compiler will automatically convert the values to match the data type of the variable on the left hand side of the assignment operation.



Exercise 2: Operators and operands


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   int num1;
   num1++;
   printf( "num1 = %d \n", num1 );
   num1 = num1 + 20;
   printf( "num1 = %d \n", num1 );
   num1 = ( num1 > 0 ? 5 : -5 );
   printf( "num1 = %d \n", num1 );
}

Which are operators and operands in the program?



An expression is a combination of operators and operands.
  1. Unary operators require only 1 operand (e.g. line 6 in Exercise 2).
  2. Binary operators require 2 operands (e.g. line 8 in Exercise 2).
  3. There is only one ternary operator in the C language, which is the conditional operator ? : that requires 3 operands (e.g. line 10 in Exercise 2).



Arithmetic operators include:

OperatorExampleMeaning
*x * yMultiply x and y
/x / yDivide x by y
%x % yThe remainder after dividing x by y
+x + yAdd x and y
-x - ySubtract y from x
++xValue of x
--xArithmetic negative of x
++++xIncrease x by 1 before using x
----xDecrease x by 1 before using x
++x++Increase x by 1 after using x
--x--Decrease x by 1 after using x



Exercise 3: Arithmetic operators


Write C programs that display multiples of 5 from zero to 50, i.e. the multiplication table for 5. Find different ways using all the arithmetic operators in the above section.
#include <stdio.h>

int main( void )
{
   int counter;
   
   counter = 1;
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );
   printf( "%d \n", counter++ * 5 );

   counter = 0;
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
   printf( "%d \n", ++counter * 5 );
}



Exercise 4: Precedence of arithmetic operators


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   printf( "%d \n", 14 + 23 * 34 + 12 );
   printf( "%d \n", 14 + 23 * ( 34 + 12 ) );

   printf( "%d \n", -2 -4 * 86 );
   printf( "%d \n", ( -2 -4 ) * 86 );
}

What are the evaluation trees for the arithmetic expressions?



Operators in C arithmetic expressions follow the same order of precedence as normal math expressions.
  1. Parentheses are the highest precedence
  2. Unary operators, such as ++ or --
  3. Positive and negative sign operators, e.g. + or -
  4. Multiplication and division operators
  5. Addition and subtraction operators
  6. Assignment operators are the lowest precedence, such as = or *=



Like math expressions, parentheses can be used to indicate specific orders of precedence. The use of extra parentheses in a complex arithmetic expression can make it easier to read and change later.



Exercise 5: Limitations of output display


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   printf( "%.2f \n", 6 / 4 );
   printf( "%d \n", 6 / 4 );

   printf( "%.2f \n", 6 % 4 );
   printf( "%d \n", 6 % 4 );

   printf( "%.2f \n", 6.0 / 4.0 );
   printf( "%d \n", 6.0 / 4.0 );
}

What is the data type at each statement? How does the data type restrict the output display?



The result of division using all integer operands is an integer value. If a fractional result is required, then one of the operands must be forced to be float or double type.



Assignment operators include:

OperatorExampleMeaning
=x = yAssign the value of y to the variable x
*=x *= yAssign the product of x multiplied by y to the variable x
/=x /= yAssign the quotient of x divided by y to the variable x
+=x += yAssign the sum of x and y to the variable x
-=x -= yAssign the result of subtracting y from x to the variable x



Exercise 6: Assignment operators


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   int num1, num2;

   num1 = 5;
   num2 = num1 + 10;
   printf( "num1 is %d and num2 is %d \n", num1, num2 );
   num1 = 50;
   printf( "num1 is %d and num2 is %d \n", num1, num2 );
   num1 += 34 / 17;
   num2 *= 34 - 17;
   printf( "num1 is %d and num2 is %d \n", num1, num2 );
}




When using the combination assignment operators (e.g. *=), the expression on the right side of the operator is fully evaluated before the assignment operator is applied.

x *= 37 - 15;


is the same as

x *= ( 37 - 15 );




Comparison operators have only 2 possible results:

OperatorExampleMeaning
<x < y1 if x is less than y
<=x <= y1 if x is less than or equal to y
>x > y1 if x is greater than y
>=x >= y1 if x is greater than or equal to y
==x == y1 if x is equal to y
!=x != y1 if x is not equal to y



Exercise 7: Comparison operators


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   printf( "%d \n", 4 > 4 );
   printf( "%d \n", 4 >= 4 );

   printf( "%d \n", 6 == 6 );
   printf( "%d \n", 6 != 6 );
}

Practice using different values and different comparison operators.



Errors often happen with the use of the equal signs:



Logic operators have only 2 possible results:

OperatorExampleMeaning
&&x && y1 if the values of x and y are both not 0
||x || y1 if the values of x or y are not 0
!!x1 if the value of x is equal to zero.



Exercise 8: Logic operators


Type the following source code into the C compiler.
#include <stdio.h>

int main( void )
{
   printf( "%d \n", 6 > 5 && 8 > 9 );
   printf( "%d \n", 6 > 5 || 8 > 9 );

   printf( "%d \n", !( 6 > 5 ) );
   printf( "%d \n", !( 8 > 9 ) );
}

Does the displayed output match your expectations of the logic operations?



Exercise 9: Distance conversion


Write a C program to display the following calculations:

The conversion formulas are available at Wikipedia:

#include <stdio.h>

int main( void )
{
   double furlong = 50;
   double miles = furlong * 0.125;
   double kilometers = furlong * 0.2011680;
   
   printf( "%.0lf furlongs is equivalent to %.2lf miles \n", furlong, miles );
   printf( "%.0lf furlongs is equivalent to %.2lf kilometers", furlong, kilometers );
}



Exercise 10: Temperature conversion


Write a C program to display the following calculations:

The conversion formulas are available at Wikipedia:

#include <stdio.h>

int main( void )
{
   double kelvin;
   
   kelvin = 28;
   printf( "%.0lf Kelvin is equivalent to %.2lf Celsius \n", kelvin, kelvin - 273.15 );
   kelvin = 324;
   printf( "%.0lf Kelvin is equivalent to %.2lf Fahrenheit", kelvin, (kelvin * (9 / 5)) - 459.67 );
}