PPS Unit-III

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Unit – III, Basic Algorithms

 Algorithm: In programming, algorithm is a set of well-defined instructions in sequence to solve the


problem.
 Algorithms are universal. The algorithm you use in C programming language is also the same algorithm
you use in every other language.
 Generally, the algorithms are written in natural languages, irrespective to the computer language used to
solve the problem.

Qualities of a good algorithm:


1. Input and output should be defined precisely.
2. Each step in algorithm should be clear and unambiguous.
3. Algorithm should be most effective among many different ways to solve a problem.
4. An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a way
that, it can be used in similar programming languages.
SORTING
 One of the most common applications in computer science is sorting.
 Sorting is the process through which data are arranged according to their values.
 If the data are not arranged in an order, it will take hours trying to find a single piece of information.
 Example: Finding some one’s telephone number in a telephone directory if it is not ordered in name
sequence is a difficult job.
 Sorting is a combination of two different operations: compare and swap. In the comparison operation two
different element values of the array are compared. After comparison based on the requirement the values
are swapped. Swapping is interchanging the values in elements of the array.

Program: Sorting list of elements


#include<stdio.h>
int main(void)
{
int a[5] = {9,1,4,3,5},i,j,temp;
for(i=0;i<5;i++)
for(j=i;j<5;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("Sorted List:\n");
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch();
return 0;
}
Output:
Sorted List:
13459

 There are different sorting mechanisms: Selection sort, Bubble sort, Merge sort, Heap sort … etc.
SEARCHING

 Another common operation in computer science is searching.


 Searching is a process used to find the location of a target among a list of objects.
 In an array searching means that given a value, finding the location (index) of the first element in the array
that contains that value.

 There are two basic searching techniques that can be applied to an array of elements.
 The Linear/Sequential search is used whenever the list is not ordered, small in size and not searched too
often.
 The Binary search is used, if the list is ordered, large in size and searched too often.

SELECTION SORT:

 In the selection sort, the list is divided into two sub-lists, sorted and unsorted, which are divided by an
imaginary wall.
 The smallest element from the unsorted sub-list is found and swapped with the element at the beginning of
the unsorted sub-list.
 After each selection and swapping, the imaginary wall between the two sub-lists moves one element ahead,
increasing the number of sorted elements and decreasing the number of unsorted elements.
 Each time an element is moved from the unsorted sub-list to the sorted sub-list, we call it a sort pass is
completed.
 To sort a list of n-elements, n-1 sort passes are needed.
// C program for implementation of selection sort
#include <stdio.h>
void swap(int*,int*);
void selectionSort(int[],int);
void printArray(int[],int);

// Driver program to test above functions


int main()
{
int list[] = {64, 25, 12, 22, 11};
int last = sizeof(list)/sizeof(list[0]);
selectionSort(list, last-1);
printf("Sorted array: \n");
printArray(list, last);
return 0;
}

void swap(int* xp, int* yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int list[], int last)


{
int cur, walk, smlst;

// One by one move boundary of unsorted subarray


for (cur = 0; cur < last; cur++)
{
// Find the minimum element in unsorted array
smlst = cur;
for (walk = cur+1; walk <= last; walk++)
if (list[walk] < list[smlst])
smlst = walk;

// Swap the found minimum element with the first element


swap(&list[smlst], &list[cur]);
}
}

/* Function to print the list */


void printArray(int list[], int last)
{
int i;
for (i=0; i < last; i++)
printf("%d ", list[i]);
printf("\n");
}

Output:
Sorted array:
11 12 22 25 64

BUBBLE SORT:

 In the bubble sort, the list is divided into two sub-lists, sorted and unsorted.
 The smallest element is bubbled from unsorted sub-list and moved to the sorted sub-list.
 After moving the smallest element to the sorted sub-list, the imaginary wall moves one element ahead,
increasing the number of sorted elements and decreasing the number of unsorted elements.
 Moving one element each time from unsorted sub-list to the sorted sub-list completes one sort pass.
 Given a list of n-elements, the bubble sort takes n-1 sort passes to sort the data.
 Remember the bubble sort concept starts from the end of the list.
// C program for implementation of Bubble sort
#include <stdio.h>
void swap(int*, int*);
void bubbleSort(int[],int);
void printArray(int[],int);

int main()
{
int list[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(list)/sizeof(list[0]);
bubbleSort(list, size);
printf("Sorted array: \n");
printArray(list, size);
return 0;
}

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort


void bubbleSort(int list[], int size)
{
int cur, walk;
for (cur = 0; cur < size; cur++)
for (walk = size-1; walk > cur; walk--)
if (list[walk] < list[walk-1])
swap(&list[walk-1], &list[walk]);
}

/* Function to print an array */


void printArray(int list[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", list[i]);
}
Output:
Sorted array:
11 12 22 25 34 64 90

LINEAR/SEQUENTIAL SEARCH

 In sequential search, the searching for the target will start from the beginning of the list, and will continue
until the target is found or until the end of the list is reached (no target found case).

Example:

 The following figure lists the steps to find 62. The process will first compare the value with data at index 0,
then 1, 2, and 3 before finding the element at 5th location (index 4).
 If the target value is not in the list, then the comparison operation will reach the end of the list, making sure
that the target is not in the list. The figure also lists the steps for finding the value 72, which is not there in
the list.
// C-Program for Linear Search.
#include<stdio.h>
int seqSearch(int[],int,int);
int main(void)
{
int list[20], target, index, i, size;
system("cls"); //clrscr();
printf("Enter the size of the list:");
scanf("%d", &size);
printf("Enter any %d elements:\n", size);
for (i = 0; i < size; i++)
scanf("%d", &list[i]);

printf("Enter a target value to search:");


scanf("%d", &target);
index=seqSearch(list,target,size);

if (index==-1)
printf("%d isn't present in the list.\n", target);
else
printf("%d is present at index %d in the list",target, index);
getch();
return 0;
}

int seqSearch(int list[],int target,int size)


{
int index;
for (index = 0; index < size; index++)
if (list[index] == target)
return index;
return -1;
}
Output:
Enter the size of the list:5
Enter any 5 elements:
43679
Enter a target value to search:7
7 is present at index 3 in the list

BINARY SEARCH

 The sequential search algorithm is very slow.


 If we have an array of 1 million elements, we must do 1 million comparisons in the worst case.
 If the array is not sorted, this is the only solution.
 If the array is sorted, we can use a more efficient algorithm called binary search.
 We have to use binary search whenever the list contains more than 50 elements.

How it works:

 The binary search starts by comparing the data in the element in the middle of the list. This determines if
the target is in the first half or second half of the list.
 If the target is in the first half, no need to search the second half and if the target is in the second half, then
no need to search the first half.
 Either way half of the list will be eliminated from consideration.
 The same process will be repeated until the target is found or it’ll be confirmed that the target is not in the
list.
 The two important cases in binary searching process are:
o The target found case.
o The target not found case.

To find the middle of the list, we need three variables, one to identify the beginning of the list (first), one to
identify the middle of the list (mid), and one to identify the end of the list (last).

Target Found:

 The following figure shows finding 22 in a sorted list. In our case first will be 0 and last will be 11 and mid
can be calculated as follows.
mid = (first + last) / 2;
 Since the index mid is an integer, the result of will be an integral value: mid = ( 0 + 11 ) / 2 = 11 / 2 = 5
 At index location 5, the target is greater than the list value ( 22 > 21 ). Therefore the list location from 0
through 5 can be eliminated.
 Now in the step the first will be made as: first = mid + 1 i.e. first = 5 + 1 = 6. And further calculates the mid
by using same formulae: mid = (first + mid)/2 i.e. mid = ( 6 + 11 ) / 2 = 17 / 2 = 8.
 Again the target will be tested with value at mid, this time the target is less than the list value ( 22 < 62 ).
 This eliminates locations 8 through 11.
 This time last will be adjusted as: last = mid – 1 i.e. last = 8 – 1 = 7 and mid = ( first + last ) / 2 = ( 6 + 7 ) /
2 = 6.
 Now at mid location the value matches with the target. This will stop the search.

Target Not Found:

 The target not found in the binary search is something when we searched all the possibilities and didn’t
find the target in the list.
 This is done in the binary search when first becomes greater than last.
 Thus only two conditions terminate the binary search algorithm: Either the target is found or first
becomes larger than last.
//Binary Search algorithm
#include <stdio.h>
int binSearch(int[],int,int);
int main(void)
{
int i, index, size, target, list[20];
system("cls"); //clrscr();
printf("Enter size of the list:");
scanf("%d",&size);
printf("Enter any %d elements:\n", size);
for(i = 0; i < size; i++)
scanf("%d", &list[i]);
printf("Enter target value:\n");
scanf("%d", &target);
index=binSearch(list,target,size);
if(index==-1)
printf("%d isn't found in the list",target);
else
printf("%d is fount at %d in the list",target,index);
getch();
return 0;
}

int binSearch(int list[], int target, int size)


{
int first, mid, last;
first = 0;
last = size - 1;

while (first <= last)


{
mid = (first+last)/2;
if(target == list[mid])
return mid;
else if (target > list[mid])
first = mid+1;
else
last = mid-1;
}
return -1;
}
Output:
Enter size of the list:10
Enter any 10 elements:
2 5 7 9 13 17 21 27 29 31
Enter target value:
21
21 is fount at 6 in the list

FUNCTIONS:

 The programs we have presented so far have been very simple. They solved problems that could be
understood without too much effort.
 As the program size grows and it is common practice to divide the complex program into smaller
elementary parts.
 The planning for large programs is simpler. First we must understand the problem as a whole; then break it
into simpler and smaller understandable parts. We call each of these parts of a program a module and
subdividing a problem into manageable parts top-down design.
 The technique used to pass data to a function is known as parameter passing.

Functions in C:

 In C, the idea of top-down design is done using functions. A C program is made of one or more functions,
but one and only one of which must be named main.
 The execution of the program always starts and ends with main, but it can call other functions to do some
of the job.
 A function in main including main is an independent module that will be called to do a specific task. A
called function receives control from calling function.
 When the called function completes its task, it returns control back to the calling function. It may or may
not return a value to the caller.
 The function main is called by the operating system; main in turn calls other functions. When main is
complete, control return to the operating system.
 In general, the purpose of a function is to receive zero or more pieces of data, operate on them, and return
at most one piece of data.

Advantages of using functions in C:

 Problems can be divided into understandable and manageable steps.


 Functions provide a way to reuse code that can be used in more than one place in the program.
 Functions are used to protect data. The local data described in a function is available only to the function
and only while the function is executing. Local data in a function cannot be seen or changed by a function
outside of its scope.
User-Defined Functions:

 Like every other object in C, functions must be declared, defined and called when needed.
 The function declaration needs to done before the function call, mentions the name of the function, the
return type and type and order of the formal parameters. The declaration uses only the header of the
function and ended with a semicolon.
 The function definition can be coded after the function call contains the code needed to complete the task.
 The function call comes within a function which calls it to get the portion of the job to be done.
 A function name is used three times: for declaration, in a call and for definition.
 Example programs:

// Simple greetings program using functions


#include <stdio.h>
void greetings(void);
void main()
{
greetings();
}

void greetings()
{
printf("Hello World\n");
}
Output:
Hello World

Basic Function Design:

 The classification of the basic function designs is done by their return values and their parameter list.
 Functions either return a value or don’t. Functions that don’t return a value are known as void functions.
 Based on requirement parameter list is also optional. Either they can have parameters or don’t.
 Combining the return values and parameter lists functions can be classified into four basic designs:
1. void functions without parameters
2. void functions with parameters
3. non void functions without parameters
4. non void functions with parameters

1. void functions without parameters:

 A void function can be written without any parameters.


 As the void function does not have a return value, it can be used only as a statement. It cannot be used in an
expression.
result = greeting(); // error. void function

// void functions without parameters


#include <stdio.h>
void add(void);
void main()
{
add();
}

void add(void)
{
int a=10,b=20,sum;
sum=a+b;
printf("Sum of %d,%d is:%d",a,b,sum);
}
Output:
Sum of 10,20 is:30

2. void function with parameters

 A void function can have parameters.


 Parameters are helpful in passing the values from the calling function to the called function. Passing
parameters is a technique of sending the values to the called function.
 As the void function does not have a return value, it can be used only as a statement. It cannot be used in an
expression.

// void functions with parameters


#include <stdio.h>
void add(int,int);
void main(void)
{
int a=10,b=20;
add(a,b);
}

void add(int x, int y)


{
int sum;
sum=x+y;
printf("Sum of %d,%d is:%d",x,y,sum);
}
Output:
Sum of 10,20 is:30

3. Non void functions without parameters

 This type of functions returns a value but don’t have any parameters.
 The most common use of this design reads data from the keyboard or file and returns to the calling
function.
 As this function returns a specific type of value, the function call must be initialized with variable of that
specific type.

// Non - void functions without parameters


#include <stdio.h>
int add(void);
void main(void)
{
int sum;
sum=add();
printf("Sum of a,b is:%d",sum);
}

int add()
{
int a=10,b=20,sum;
sum=a+b;
return sum;
}
Output:
Sum of a,b is:30
//Programs for getValue() function
#include <stdio.h>
int getValue(void);
void main(void)
{
int val1,val2;
val1=getValue();
val2=getValue();
printf("Val1:%d, val2:%d",val1,val2);
}

int getValue(void)
{
int val;
printf("Enter Value:");
scanf("%d",&val);
return val;
}
Output:
Enter Value:24
Enter Value:36
Val1:24, val2:36

4. Non-void functions with parameter

 This type of functions passes parameters and returns a value.


 As this function returns a specific type of value, the function call must be initialized with variable of that
specific type.

//Non-void function with parameters


#include <stdio.h>
int add(int,int);
void main(void)
{
int a=10,b=20,sum;
sum=add(a,b);
printf("Sum of a,b is:%d",sum);
}

int add(int x,int y)


{
int res;
res=x+y;
return res;
}
Output:
Sum of a,b is:30

User Defined Functions:

o Like every other object in C, functions must be declared, defined and initialized (calling of the function).
o The functions defined by the user who is writing the program, but not by the developers of the language are
known as user defined functions.
o To use functions in a C-Program one has to do, the declaration, definition and initialization of the function.
o The function declaration has to be done before function call, which will give the complete picture of the
function.
o And definition will succeed function call, which actually contains the statements (body) of the function.

Function Declaration:

o Function declaration consists of only a function header. It contains no code (body of the function).
o Function declaration consists of three parts: the return type, the function name and the formal parameter
list.
o Function declaration will be terminated by a semicolon.
o The return type is the type of value the function is going to return after performing its job. If there is no
return type the type will be void type.
o In declaration the parameter list will indicate only the valid type and order of the parameters; the variable
identifier can be omitted here in this case.
o Generally the declarations are placed in the global declaration area of the program. That is before the main
function.

Function call:

o The function call is postfix expression.


o The operand in the function call is the function name and the operator is the parenthesis set, which contains
the actual parameters.
o The actual parameters hold the actual values that are to be sent to the called function. They must match
with the function definition’s formal parameter list in both type and order. While specifying actual
parameters variable identifiers are required.
o If the function call is having multiple parameters to be passed, they must be separated by commas.
o Functions can return values. If a functions return type is void it, then it cannot be used as part of an
expression, it has to be called as a stand-alone.
o But if a function is having a return value, then either its call must be initialized with a suitable type variable
or it has to be used in an expression. If the function is called like a stand-alone then the return value will be
discarded.

Function Definition:

o The function definition contains the code for a function.


o It is made up of two parts: the function header and the function body.

function header

 A function header consists of three parts: the return type, the function name, and the formal parameter list.
 A semicolon is not required at the end of the function definition header.
 The return-type should come before the function name; return-type specifies the type of value a function
will return. If the function has nothing to return, the return type will be void.
 The formal parameter list must match with the type and order of the actual parameters. Here while
specifying the formal parameters the variable identifier is must, which will hold the values passed by actual
parameters.

function body

 The function body contains the local declarations and the function statements.
 The body starts with local definitions that specify the variables needed by the function. After local
definition, the function statements, terminating with a return statement.
 If a functions return type is void, it can be written without a return statement.

Formal Parameter List:

 In the definition of a function, the parameters are contained in the formal parameter list.
 This list declares and defines the variables that will contain the data received by the function.
 If the function has no parameters; if it is not receive any data from the calling function, then the parameter
list can be void.

Local Variables:

A local variable is a variable that is defined inside a function body and used without having any role in the
communication between functions.

//Example program for functions


#include<stdio.h>
int add(int,int);
int getValue();
void putValue(int);
void main(void)
{
int a,b,c;
a=getvalue();
b=getvalue();
c=add(a,b);
putvalue(c);
}

int add(int x,int y)


{
int z;
z=x+y;
return z;
}

int getvalue()
{
int temp;
printf("Enter value:");
scanf("%d",&temp);
return temp;
}

void putvalue(int res)


{
printf("Result: %d",res);
}
Output:
Enter value: 24
Enter value: 32
Result: 56
INTER-FUNCTION COMMUNICATION:

 Inter function communication is a concept through which calling function and called function will
communicate with each other, and exchange the data.
 The data flow between the called and calling function can be divided into three strategies:
I. Downward flow
II. Upward flow
III. Bi-directional flow

I. Downward flow:

 In downward communication, the calling function sends data to the called function.
 No data flows in the opposite directions.
 The called function may change the values passed, but the original values in the calling function remains
untouched.
 The pass by value or call by value mechanism is a perfect solution for downward flow.
 A variable is declared and defined in the called function for each value to be received from the calling
function.
 Downward communication is one-way communication. The calling function can send data to the called
function, but the called function cannot send any data to the calling function.

//downward communication
#include<stdio.h>
void downFun(int,int);
void main(void)
{
int a=5;
downFun(a,15);
printf("Value of a is:%d",a);
}

void downFun(int x,int y)


{
x=x+y;
}
Output:
Value of a is: 5
II. Upward Communication:

 Upward communication occurs when the called function sends data back to the calling function without
receiving any data from it.
 C provides only one way for upward flow, the return statement.
 Using return statement only one piece of data item can be returned.
 To send multiple values back to the calling function, we have to use references of variables. References are
memory locations which carries the data from called function to the calling function.

//upward communication
#include<stdio.h>
void upFun(int*,int*);
void main(void)
{
int a,b;
upFun(&a,&b);
printf("Value of a, b are:%d,%d",a,b);
}

void upFun(int* aptr,int* bptr)


{
*aptr = 24;
*bptr = 32;
}
Output:
Value of a, b are: 24,32

III. Bi-direction Communication:

 Bi-directional communication occurs when the calling function sends data down to the called function.
After processing the called function sends data up to the calling.
 The strategy described for upward communication can also be used for bi-direction communication, but
with a little modification.
 The only change is that the indirect reference must be used in both sides of the assignment statement.
 With this change the variable in the called function first is accessed for retrieving data using address
variable in the right hand side.
 The same parameter is used again to store a value in the left-hand side.
 Note: both upward and bidirectional communications are examples for pass by reference or call by
reference mechanisms.

//Bi-directional communication:
#include<stdio.h>
void biFun(int*,int*);
void main(void)
{
int a=2,b=3;
biFun(&a,&b);
printf("Value of a, b are:%d,%d",a,b);
}
void biFun(int* aptr,int* bptr)
{
*aptr = *aptr+24;
*bptr = *bptr+32;
}
Value of a,b are: 26, 35

STANDARD FUNCTIONS (LIBRARY FUNCTIONS):

 C provides a rich collection of standard functions whose definitions have been written and are ready to be
used in our programs.
 To use these functions, we must include their function declarations into our program.
 The function declarations are generally found in header files.
 Instead of adding each declaration separately, a simple statement with header file can be included on top of
the program.

Math functions

 Several library functions are available for mathematical calculations.


 Most of them are in either math header file (math.h) or standard library (stdlib.h).

Absolute value functions:

 These functions will return the absolute value of a number.


 An absolute value is the positive value of the value regardless of its sign.
 There are 3-integer and 3-floating point functions.
 The integer functions are abs, labs, and llabs.
 For abs the parameter must be an int and it returns an int. For labs the parameter must be a long int, and it
returns a long int. For llabs the parameter must be a lond int, and it returns a long long int.
 General Syntax:
 int abs(int);
 long labs(long);
 long long llabs(long long);

 The real functions are fabs, fabsf and fabsl. For fabs the parameter is a double, and it returns a double. For
fabsf the parameter is a float and returns a float. For fabsl the parameter is a long double and returns a long
double.
 General Syntax:
 double fabs(double);
 float fabsf(float);
 long double fabsl(long double);
// Absolute value functions
#include<stdio.h>
#include<math.h>
void main(void)
{
float val=-2.674;
float res;
res = fabsf(val);
printf("Absolute value of %f is %f", val, res);
}
Output:
Absolute value of -2.674000 is 2.674000

Ceiling Functions

 A ceiling is the smallest integral value greater than or equal to a number.


Example: Ceiling of 3.00001 is 4.
 If the complete numbers are considered as continues range from minus infinity to plus infinity, this
function moves the number right towards an integral value.

 The declarations for ceiling function are:


 double ceil (double);
 float ceilf(float);
 long double ceill(long double);

 Example: ceil (-1.9) returns -1.0


ceil (1.1) returns 2.0

// Ceiling Functions
#include<stdio.h>
#include<math.h>
void main(void)
{
float val = 2.674;
float res;
res = ceilf(val);
printf("Ceiling value of %f is %f", val, res);
val = -2.674;
res = ceilf(val);
printf("\nCeiling value of %f is %f", val, res);
}
Output:
Ceiling value of 2.674000 is 3.000000
Ceiling value of -2.674000 is -2.000000
Floor Functions
 A floor is the largest integral value that is equal to or less than a number.
 Example: Floor of 3.99999 is 3.0.
 On number series, this function moves the number left towards an integral value.

 The declarations for floor function are:


 double floor (double);
 float floorf (float);
 long double floorl (long double);

 Example: floor (-1.1) returns -2.0


floor (1.9) returns 1.0
// Floor Functions
#include<stdio.h>
#include<math.h>
void main(void)
{
float val = 2.674;
float res;
res = floorf(val);
printf("Floor value of %f is %f", val, res);
val = -2.674;
res = floorf(val);
printf("\nFloor value of %f is %f", val, res);
}
Output:
Floor value of 2.674000 is 2.000000
Floor value of -2.674000 is -3.000000

Truncate Functions
 The truncate functions return the integral value in the direction of 0.
 They are same as floor functions for positive numbers and same as ceiling function for negative numbers.
 Their function declarations are:
 double trunc (double);
 float truncf (float);
 long double truncl (long double);

 Example: trunc (-1.1) returns -1.0


trunc (1.9) returns 1.0
// Truncate Functions
#include<stdio.h>
#include<math.h>
void main(void)
{
float val = 2.674;
float res;
res = truncf(val);
printf("Truncate value of %f is %f", val, res);
val = -2.674;
res = truncf(val);
printf("\nTruncate value of %f is %f", val, res);
}
Output:
Truncate value of 2.674000 is 2.000000
Truncate value of -2.674000 is -2.000000

Round Functions
 The round functions return the nearest integral value.
 Their function declarations are:
 double round (double);
 float roundf(float);
 long double roundl(long double);

 Example: round (-1.1) returns -1.0


round (1.9) returns 2.0
// Round Functions
#include<stdio.h>
#include<math.h>
void main(void)
{
float val = 2.674;
float res;
res = roundf(val);
printf("Round value of %f is %f", val, res);
val = -2.674;
res = roundf(val);
printf("\nRound value of %f is %f", val, res);
}
Output:
Round value of 2.674000 is 3.000000
Round value of -2.674000 is -3.000000

Power Functions:
 The power (pow) function return the value of the x raised to the power of y.
 An error occurs if the base (x) is negative and the exponent (y) is not an interger, or if the base is zero and
the exponent is not positive.

 The power function declarations are:


 double pow (double, double);
 float powf (float, float);
 long double powlf (long double, long double);

 Example: pow (3.0, 4.0) returns 81.0

Square Root Functions:


 The square root functions return the non-negative square root of a number.
 An error occurs if the number is negative.
 The square root function declarations are:
 double sqrt (double);
 float sqrtf (float);
 long double sqrtlf(long double);

 Example: sqrt (25) returns 5.0

// Power and Square root functions


#include<stdio.h>
#include<math.h>
void main(void)
{
float val1 = 3;
float val2 = 4;
float res1, res2;
res1 = powf(val1,val2);
printf("%f power of %f is %f", val1, val2, res1);
res2 = sqrtf(res1);
printf("\nSquare root of %f is %f", res1, res2);
}
Output:
3.000000 power of 4.000000 is 81.000000
Square root of 81.000000 is 9.000000

PASSING ARRAY TO FUNCTIONS

 Arrays can also be passed as parameters to functions when required.


 There are two possibilities in which arrays can be passed:
a) Passing individual elements
b) Passing the whole array

Passing Individual elements

Individual elements can be either by their values or by their addresses.

Passing data values

 Individual data values of arrays can be passed using their index along with array name.
 As long as the type is matching, the called function is unaware whether the value is coming from an array
or a normal variable.

// Passing data values from an array


#include<stdio.h>
void arrAccess(int);
void main(void)
{
int a[5]={2,4,3,6,9};
arrAccess(a[3]);
printf("Array element after function call: %d",a[3]);
}

void arrAccess(int temp)


{
printf(“Parameter received from calling function: %d”,temp);
temp = 36;
}
Output:
Parameter received from calling function: 6
Array element after function call: 6

Passing Addresses

 The address of the elements of the array can also be passed to the function.
 The individual element addresses can be passed just like any other variable address.
 To pass an array element’s address, the address operator can be prefixed to the element’s indexed
reference.
 Example: The address of 4th element in the array can be passed in following way: &arr[3];

Passing an address of an array element requires two changes in the called function.

 First, it must declare that it is receiving an address.


 Second, it must use the indirection operator (*) to refer to the elements value.
// Passing the address of an array element
#include<stdio.h>
void arrAccess(int*);
void main(void)
{
int a[5]={2,4,3,6,9};
arrAccess(&a[3]);
printf("Array element after function call: %d",a[3]);
}

void arrAccess(int* temp)


{
printf(“Parameter received from calling function: %d”,*temp);
*temp = 36;
}

Output:
Parameter received from calling function: 6
Array element after function call: 36

Passing the whole array

 In C, the name of an array holds the address of the first element in the array.
 Using array name and index values the complete array elements can be accessed.
 Passing the array name instead of a single element, allows the called function to refer to the same array
back in the calling function.
//Passing whole array to called function
#include<stdio.h>
void arrAccess(int[]);
void main(void)
{
int a[5]={2,4,3,6,9},i;
arrAccess(a);
printf("\nAfter function call Elements in Array:");
for(i=0;i<5;i++)
printf("%d ", a[i]);
}

void arrAccess(int arrTemp[])


{
int i;
printf("Elements in Array:");
for(i=0;i<5;i++)
printf("%d ", arrTemp[i]);
for(i=0;i<5;i++)
arrTemp[i] = i;
}
Output:
Elements in Array:2 4 3 6 9
After function call Elements in Array:0 1 2 3 4

You might also like