Wednesday 12 September 2012

Chapter 6 - Mathematical operations(part 1 of 2)


By now you must be confident with writing a c program and fully capable of printing the output in any given format. This chapter will teach you to incorporate mathematical operations to your code. This chapter will cover simple operations like addition, subtraction, multiplication and division.

The reason behind not dealing with advanced mathematical operations is that these are in the form of functions and require a tutorial on functions. Moreover C has so many advanced mathematical functions that it cannot be covered in a single chapter. What i can do in later chapters is teach you the method to use these advanced operations in your program with one or two examples and thus you can make use of any mathematical operation you want as per your requirement using the same procedure.

So lets begin with the tutorial

Lets start with variables 'a' and 'b' to store 2 values and we will be applying the 4 mathematical operations on them. To store the value of these  operations we will require a third variable called 'result'.

Any math operation will be of the form 
result = a (operator) b

Consider the examples

Addition:
4 = 3 + 1
Subtraction:
7 = 10 - 3
Multiplication:
42 = 7 * 6
Division
2.5 = 5 / 2

Note: In the first 3 examples the result of 2 integers will always be an integer but in case of division the result is a float (decimal) value. Hence when you write an expression in your program you need to determine what kind of variable is required to save the result in advance in order to avoid errors.

Try to determine the  answers of the given examples


  1. 3.5 + 1.4
  2. 1 - 0.56
  3. 0.0 * 1.0
  4. 5 / 3

Answers:

  1. The result will be a float
  2. The result will be a float
  3. Although the two numbers appear as decimal but they do not contain any fractional value ( just .0) hence you can store the result in an int 
  4. Although the 2 numbers are integers the division operation will yield a float hence the result will be a float
The program

/*  
 Author : Ryan Sequeira  
 Date : 12th September 2012  
 Title : prints the value of 4 mathematical operations
*/ 

#include<conio.h>
#include<stdio.h>

void main()
 {
  int a=23, b=12;
  int result;
  float divResult, fa=23.0, fb=12.0;

  clrscr();

  //adding two numbers
  result = a + b;
  printf("The result of addition is : %d", result);
  getch();

  //subtracting two numbers
  result = a - b;
  printf("\nThe result of subtraction is : %d", result);
  getch();

  //multiplying two numbers
  result = a * b;
  printf("\nThe result of multiplication is : %d", result);
  getch();

  //dividing two numbers
  divResult = fa / fb;
  printf("\nThe result of division is : %f", divResult);
  getch();
 }


Note: In the division operation to make the result accurate i have made use of float values as operands instead of integers ( although 23.0 and 12.0 are integers but they are stored in float data type) to get accurate results. If you replace them with integers a and b (instead of fa and fb) you will get a value of 1.0000 which is  incorrect.

There is a short hand way to print the results of the mathematical equations, but i recommend you to make a habit to store the results in a variable, just a good programming practice and it avoids errors.

printf("The result of addition is : %d", a + b);

printf("\nThe result of subtraction is : %d", a - b);

printf("\nThe result of multiplication is : %d", a * b);

printf("\nThe result of division is : %f", fa / fb);

So the new code looks like this
/*  
 Author : Ryan Sequeira  
 Date : 12th September 2012  
 Title : prints the value of 4 mathematical operations 
 with short hand
*/  

#include<conio.h>
#include<stdio.h>

void main()
 {
  int a=23, b=12;
  int result;
  float divResult, fa=23.0, fb=12.0;
  
  clrscr();
  
  //adding two numbers
  printf("The result of addition is : %d", a + b);
  getch();
  
  //subtracting two numbers
  printf("\nThe result of subtraction is : %d", a - b);
  getch();
  
  //multiplying two numbers
  printf("\nThe result of multiplication is : %d", a * b);
  getch();
  
  //dividing two numbers
  printf("\nThe result of division is : %f", fa / fb);
  getch();
 }


In this program we have simply used two operands , one operator and one value to store the result, but one can make use of more than one operators to form a mathematical equation.


Consider the equation:

In order to write this in our c program we will have to understand how this equation will get equated.
If we break this equation we can see that there are two operators and we can write the 2 operators individually.

So we can now combine them using parenthesis ( ) 
So our equation becomes: 

result = a /(a+ b) 

Lets make use of this equation in our program and see how it works. In order to determine if you have translated your equation correctly in the code you can solve it manually with some values and match them with the values returned by your code.


/*  
 Author : Ryan Sequeira  
 Date : 12th September 2012  
 Title : implementing a complex equation
 with short hand
*/  

#include<conio.h>
#include<stdio.h>

void main()
 {
  float result, a=23.0, b=12.0;
  
  clrscr();
  
  result = a / (a +b);
  printf("The result of our equation is : %f",result);
  
  getch();
 }

Activities:

  1. Try to change the data types for each mathematical operations and check the results
  2. Try to implement the following equations in  C


Hint: make use of multiplication ( a*a ) to calculate  squares

No comments:

Post a Comment