ARITHMETIC EXPRESSIONS
Operator | Meaning | Example | Output |
---|
+ | Addition | int a = 2, b=5; int c = a+b; printf(“%d + %d = %d”, a,b,c); | 2 + 5 = 7 |
- | Subtraction | int a = 2, b=5; int c = a-b; printf(“%d - %d = %d”, a,b,c); | 2 - 5 = -3 |
* | Multiplication | int a = 2, b=5; int c = a*b; printf(“%d x %d = %d”, a,b,c); | 2 x 5 = 10 |
/ | Division | int a = 2, b=6; int c = b/a; printf(“%d / %d = %d”,b,a,c); | 6 / 2 = 3 |
% | Remainder (Modulus) | int a = 2, b=5; int c = b%a; printf(“%d Modulus %d = %d”, b,a,c); | 5 Modulus 2 = 1 |
Formatting Values of Type int
To format an integer display, you simply add a number between the % and the d of the %d placeholder in the printf format string. This number specifies the field width —the number of columns to use for the display of the value.
Example
printf("Results: %3dmeters = %4d ft. %2d in.\n", meters, feet, inches);
The statement indicates that 3 columns will be used to display the value of meters , 4 columns will be used for feet , and 2 columns will be used for inches (a number between 0 and 11 ).If meters is 21 , feet is 68 , and inches is 11 , the program output will be
Results: 21 meters = 68 ft. 11 in.
In this line, notice that there is an extra space before the value of meters (21 ) and two extra spaces before the value of feet ( 68 ). The reason is that the placeholder for meters( %3d ) allows space for 3 digits to be printed. Because the value of meters is between 10 and 99 , its two digits are displayed right-justified, preceded by one blank space. Because the placeholder for feet (%4d) allows room for 4 digits,printing its two-digit value right-justified leaves two extra blank spaces.
Formatting Values of Type double
To describe the format specification for a type double value, we must indicate both the total field width needed and the number of decimal places desired. The total field width should be large enough to accommodate all digits before and after the decimal point. There will be at least one digit before the decimal point because a zero is printed as the whole-number part of fractions that are less than 1.0 and greater than −1.0 . We should also include a display column for the decimal point and for the minus sign if the number can be negative.
The form of the format string placeholder is % n . m f where n is a number representing the total field width, and m is the desired number of decimal places.
Program Style Eliminating Leading Blanks
A value whose whole-number part requires fewer display columns than are specified by the format field width is displayed with leading blanks. To eliminate extra leading blanks, omit the field width from the format string placeholder. The simple placeholder %d will cause an integer value to be displayed with no leading blanks. A placeholder of the form % . m f has the same
effect for values of type double , and this placeholder still allows you to choose the number of decimal places you wish.
FUNCTIONS
A function is a block of code which performs a specific task.
Library Functions
C promotes reuse by providing many predefined functions that can be used to perform mathematical computations. For example C’s standard math library defines a function named sqrt that performs the square root computation. The function call in the assignment statement activates the code for function sqrt , passing the argument x to the function. You activate a function by writing a function call. After the function executes, the function result is substituted for the function call.
Figure: sqrt function call from the math library
If x is 16.0 , the assignment statement above is evaluated as follows:
1. x is 16.0 , so function sqrt computes the √16.0
2. The function result, 4.0 , is assigned to y .
The table below shows examples of more C Libraryfunctions and their usage
Function | Header File | Purpose | Argument(s) | Result |
abs(x) | <stdlib.h> | Returns the absolute value of its integer argument: if x is −5, abs(x) is 5 | int | int |
ceil(x) | <math.h> | Returns the smallest integral value that is not less than x: if x is 45.23, ceil(x) is 46.0 | double | double |
cos(x) | <math.h> | Returns the cosine of angle x: if x is 0.0, cos(x) is 1.0 | double (radians) | double |
exp(x) | <math.h> | Returns ex where e = 2.71828...: if x is 1.0, exp(x) is 2.71828 | double | double |
fabs(x) | <math.h> | Returns the absolute value of its type double argument: if x is −8.432, fabs(x) is 8.432 | double | double |
floor(x) | <math.h> | Returns the largest integral value that is not greater than x: if x is 45.23, floor(x) is 45.0 | double | double |
log(x) | <math.h> | Returns the natural logarithm of x for x > 0.0: if x is 2.71828, log(x) is 1.0 | double | double |
log10(x) | <math.h> | Returns the base-10 logarithm of x for x > 0.0: if x is 100.0, log10(x) is 2.0 | double | double |
pow(x, y) | <math.h> | Returns x y . If x is negative, y must be integral: if x is 0.16 and y is 0.5, pow(x,y) is 0.4 | double, double | double |
sin(x) | <math.h> | Returns the sine of angle x: if x is 1.5708, sin(x) is 1.0 | double(radians) | double |
sqrt(x) | <math.h> | Returns the nonnegative square root of x (1x) for x ≥ 0.0: if x is 2.25, sqrt(x) is 1.5 | double | double |
tan(x) | <math.h> | Returns the tangent of angle x: if x is 0.0, tan(x) is 0.0 | double (radians) | double |
EXERCISE
Create a project and name it week_3_ex_1 and write aprogram that takes a number and output the square root of that number.
SAMPLE CODE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
doublenumber,root;
printf("Enter a number>");
scanf("%lf",&number);
root = sqrt(number);
printf("The square root of %f is %f",number,root);
return 0;
}
NOTE
The mathematics standard library (math.h) is included in the header file in order to enable us use the sqrt function.
User-defined Functions
These are functions written by the programmer.
Syntax:
data type functionName(input data for processing or empty){
Statementto accomplish the task of the function comes here
return the result if not a void data type;
}
Explanation
data type
The data type can be void, int, double, char.
void data type means the function doesn’t return any result.
if the data type is int, the function must have to return a value of the data type int.
if the data type is double, the function must have to return a value of the data type double.
if the data type is char, the function must have to return a value of the data type char.
functionName
This is the name you give to your function. The name follows the rules for naming variables. It should not begin with a digit or have space in it. Ideally function names are lowercase even though you can decide to use lowercase or uppercase or mix and it is recommended to use underscores (_) to separate words that make your function name.
input data for processing or empty
If your function needs some information to be passed that will be processed, you state the data type of the input(s) and the name(s) for the input(s).
If your function doesn’t need any information to be passed which will be processed, then the place is left empty. Thus, the () will be empty.
return the result if not a void data type
As explained earlier, if the function is not void, youhave to return a result and the value to return must have the same data type asthe function.
Example
1. Write a function to welcome a user to C Programming. Thus your program should simply display WELCOME TO CPROGRAMMING on the screen
2. Write a function which takes a number and returns the square of that number
3. Write a function which takes two numbers and return the sum of the two numbers
4. Write a function which takes two numbers and return the difference of the two numbers
5. Write a function which takes two numbers and return the product of the two numbers
6. Write a function which takes the radius of a circle and returns the area of the circle
7. Write a function which takes the radius of a circle and returns the circumference of the circle.
Solution
1.
void welcome(){
printf("WELCOME TO C PROGRAMMING");
}
2.
double square(double number){
returnnumber*number;
}
3.
double sum(double a, double b){
return a+b;
}
4.
double difference(double a, double b){
return a-b;
}
5.
double product(double a, double b){
return a*b;
}
6.
double area_of_circle(double radius){
return PI*radius*radius;
}
7.
double circum_of_circle(double radius){
return 2*PI*radius;
}
Function Prototype
Before you implement your function in your c file, you need to declare your function prototype before your int main function.
A function prototype tells the C compiler the datatype of the function, the function name, and information about the arguments that the function expects.
The data type of a function is determined by the type of value returned by the function.
A function prototype does not specify the function definition.
Syntax:
data type functionName(input data type without names or empty);
Examples
Declare the data type for the functions written above.
1.
void welcome();
2.
double square(double);
3.
double sum(double,double);
4.
double difference(double,double);
5.
double product(double,double);
6.
double area_of_circle(double);
7.
double circum_of_circle(double);
CALLING A FUNCTION
As explained with the sqrt function, a function is called using the function name and passing in the input data of the type specified in the function.
For instance to call the square function in the example function code above, the syntax below could be written:
double number = 2;
double sq;
sq = square(number); // calling the function
printf(“The square of %f is %f”, number,sq); //display the result
EXERCISE
Create a project and name it week_3_ex_2.
Write the function prototypes for the example functions above.
Implement the function and call the functions in the int main function.
Run your project to see the output.
SAMPLE CODE
#include <stdio.h>
#include <stdlib.h>
#define PI 3.142
void welcome();
double square(double);
double sum(double,double);
double difference(double,double);
double product(double,double);
double area_of_circle(double);
double circum_of_circle(double);
int main()
{
welcome();
double number = 3;
double sq;
//calling the square function
sq =square(number);
//Displaying the result
printf("%f square is %f\n",number,sq);
double a =2, b=4, radius = 3.5;
double area,circum;
//calling the sum function
double c =sum(a,b);
printf("%f + %f = %f\n",a,b,c);
//calling the difference function
printf("%f - %f = %f\n",a,b,difference(a,b));
//calling the product function
printf("%f x %f = %f\n",a,b,product(a,b));
//calling area function
area =area_of_circle(radius);
//Displaying the result
printf("The area of a circle of radius %f is%f\n",radius,area);
//calling circumference function
circum = circum_of_circle(radius);
//Displaying the result
printf("The circumference of a circle of radius %f is%f\n",radius,circum);
return 0;
}
void welcome(){
printf("WELCOME TO C PROGRAMMING\n");
}
void hello(){
printf("HELLO WORLD\n");
}
double square(double number){
return number*number;
}
double sum(double a, double b){
return a+b;
}
double difference(double a, double b){
return a-b;
}
double product(double a, double b){
return a*b;
}
double area_of_circle(double radius){
return PI*radius*radius;
}
double circum_of_circle(double radius){
return 2*PI*radius;
}
ASSIGNMENT
Write a program that takes the x1,y1,x2,y2 of two points and calculates the distance between the two points. Write a function to calculate the distance.
Formula: distance = √(x2-x1)2 +(y2-y1)2