+ All Categories
Home > Documents > Cpds Notes Unit3 30-1-12 100am

Cpds Notes Unit3 30-1-12 100am

Date post: 06-Apr-2018
Category:
Upload: bolisettyvaas
View: 223 times
Download: 0 times
Share this document with a friend
48
C Programming & Data Structures UNIT 3 Page | 1 Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna UNIT III FUNCTIONS & ARRAYS Designing Structured Programs: To design lengthy programs, first we must understand the problem and then it should be divided into diff parts/sections. Each part is called a "Module". The process of dividing a problem into different parts is called as Top-Down Design Approach”.   A module which is calling diff sub modules is called as "Calling Module".  A module which is called by another module is called "Called Module". FUNCTIONS: "A function is a self contained block of statements that perform a coherent task of some kind". (A function is something like hiring a person to do a specific job for you) Types of functions: 1. Library functions/ standard functions 2. User-defined functions. 1. Library functions: These are pre-defined functions. Ex: printf(), scanf(), sqrt() 2. User defined functions: These are the functions defined by the user. Ex: main(), sh ow( ), fun( ), add( ) Advantages of functions: It uses top-down modular programming structure. It is easy to de bug and test the programs. Avoids the repetition of statements. The length of the program could be reduced using functions. A function can be used by many other programs.
Transcript
Page 1: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 1/48

C Programming & Data Structures U N I T 3 P a g e | 1

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

UNIT III

FUNCTIONS & ARRAYS

Designing Structured Programs:

To design lengthy programs, first we must understand the problem and then it

should be divided into diff parts/sections. Each part is called a "Module".

The process of dividing a problem into different parts is called as Top-Down Design

Approach”. 

A module which is calling diff sub modules is called as "Calling Module". 

A module which is called by another module is called "Called Module".

FUNCTIONS:

"A function is a self contained block of statements that perform a coherent task of

some kind".

(A function is something like hiring a person to do a specific job for you)Types of functions:

1. Library functions/ standard functions

2. User-defined functions.

1. Library functions:

These are pre-defined functions.

Ex: printf(), scanf(), sqrt()

2. User defined functions:

These are the functions defined by the user.

Ex: main(), show( ), fun( ), add( )

Advantages of functions:

It uses top-down modular programming structure.

It is easy to de bug and test the programs.

Avoids the repetition of statements.

The length of the program could be reduced using functions.

A function can be used by many other programs.

Page 2: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 2/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 2

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

Elements of user defined functions:

To create user defined functions we have to write three elements in a program

1. Function definition

2. Function declaration/ function prototype

3. Function call.

Ex:#include<stdio.h>void msg(); /*function prototype declaration*/void main(){

msg(); /*function call*/printf("\n I am main");

}

void msg() /*function definition*/{

printf("\n I am in message");}

O/P: I am in message I am in main.

1. Function definition:

It is an independent module which contains statements that perform some

operation.

Syntax:

return type function_name (list of formal parameters){

Local variable declaration

statements;

 _____ 

 _____ 

 _____ 

return(value);}

Return type / Value:

Return type is a data type of a value returned by the function.

If no return type is specified, then default return type will be integer.

If a function does not return any value then the return type must be void.

The function name can be any valid identifier.

The parameters that are listed in the variable definition receive the values sent by the

calling function.

The parameters are also called arguments.

Page 3: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 3/48

C Programming & Data Structures U N I T 3 P a g e | 3

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

A local variable is a variable which is declared inside a function and it will be used

only inside the function.

Return statement is used to return a value from a called function to a calling function.

Syntax: return (value);

: return;

This statement will return the control back to the calling function.

A function must return only a single value.

We can write more than one return statement using if-else statement.

2. Function Declaration / Function Prototype:

Just like variables, a function must be declared at the beginning of a program.

Syntax:

return type function name (list of parameters);

The function declaration must match with the function header in the function

declaration.

Function declaration is also called function prototype.

Ex: void add(); int add (int, int); int add();

NOTE:

In the function declaration if we write int add(int a, int b); the compliers won’t bother 

about the variable names a,b , i.e., in this example presence of a and b are optional.

3. Function call:

To execute a function, it must be called from another function.Syntax:

function_name (List of Actual Arguments);

There is no need of return type in the function call.

Ex: add(); add(a,b); c=add();

CATEGORIES OF FUNCTION  – FUNCTION CALL:

Function without arguments and without return value

Function with Arguments without return value

Function without arguments and with return value

Function with arguments and with return value

Page 4: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 4/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 4

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

1. FUNCTION WITHOUT ARGUMENTS AND WITHOUT RETURN VALUE:

In this category, the calling function will not sent any values to the called function.

Similarly, the called function will not return any value to the calling function.

In effect, there is no data transfer between calling function and the called function.

/*PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND

WITHOUT RETURN VALUE*/

#include<stdio.h>#include<conio.h>void add( ); /*FUNCTION DECLARATION*/void main( ){

clrscr( );add( ); /*FUNCTION CALL*/

}void add( ) /*FUNCTION DEFINITION*/

{ int a=10, b=20;printf("\nSum=%d",a+b);getch();

}o/p: Sum=30

Page 5: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 5/48

C Programming & Data Structures U N I T 3 P a g e | 5

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

2. FUNCTION WITHOUT ARGUMENTS AND WITH RETURN VALUE:

In this the calling function will not sent any values to the called function. But the called

function will send back a value to the called function. 

/*PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND

WITH RETURN VALUE*/

#include<stdio.h>#include<conio.h>int add( ); /*FUNCTION DECLARATION*/void main( )

{int c;clrscr( );c=add( ); /*FUNCTION CALL*/printf("\nSum=%d",c);getch( );

}int add( ) /*FUNCTION DEFINITION*/{

int x,y,z;printf("\nEnter 2 integers: ");scanf("%d%d",&x,&y);z=x+y;return z;

}o/p:

Enter 2 integers: 10 20

Sum=30 

Page 6: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 6/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 6

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

3. FUNCTION WITH ARGUMENTS AND WITHOUT RETURN VALUE:

In this category

Calling Function will send the values to the called function.

The values will be processed to the called function, but no return value will be sent

back to the calling function.

/*PROGRAM TO ILLUSTRATE FUNCTION WITH ARGUMENTS AND

WITHOUT RETURN VALUE*/

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

void add(int, int); /*FUNCTION DECLARATION*/void main(){

int a,b;clrscr();printf("\nEnter 2 integers: ");scanf("%d%d",&a,&b);add(a,b); /*FUNCTION CALL*/

getch();}void add(int x, int y) /*FUNCTION DEFINITION*/{

Printf(“\nSum=%d”, x+y);}o/p:

Enter 2 integers: 10 20

Sum=30

Page 7: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 7/48

C Programming & Data Structures U N I T 3 P a g e | 7

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

4. FUNCTION WITH ARGUMENTS AND WITH RETURN VALUE

In this category, the calling function will send the values to the called function; also

the called function sends back a return value to the calling function.

/*PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND

WITHOUT RETURN VALUE*/

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

int add(int, int); /*FUNCTION DECLARATION*/void main()

{int c;clrscr();printf(“\nEnter 2 Numbers: “); scanf(“%d%d”,&a,&b); c=add(a,b); /*FUNCTION CALL*/printf("\nSum=%d",c);getch();

}

int add(int x, int y) /*FUNCTION DEFINITION*/{

return x+y;}o/p: Enter 2 Numbers: 10 20 

Sum=30

Page 8: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 8/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 8

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

Actual parameters:The parameters are used in a function call are called actual parameters.

Formal parameters:The parameters used in a function definition are called as formal parameters. 

Parameter passing:There are 2 ways in which we can pass arguments to a function, they are

1. Call by value (or pass by value)2. Call by reference ( or pass by reference (or) address)

1. CALL BY VALUE:In this mechanism, the values of actual parameters will be sent to the formal parametersany changes made to the formal parameters will not affect the actual arguments.

Ex: swapping two numbers using functions 

/*PROGRAM TO ILLUSTRATE ABOUT CALL BY VALUE*/#include<stdio.h>void Swap(int x, int y);void main(){

int a=10, b=20;clrscr();printf("\n\n\t In main before swap a=%d, b=%d",a,b)Swap(a,b);

printf("\n\n\t In main After swap a=%d, b=%d",a,b);getch();

}void Swap(int x, int y){

int c;c=x;x=y; y=c;

printf( "\n\n\t In SWAP FUN a=%d, b=%d", x,y);}O/P:

In Main before swap a=10, b=20

In Main After swap a=10, b=20

In SWAP FUN b=20, b=10

In the above program observe that a & b values are same before and after swap,but in swap function x and y(those carries the values of a=10 and b=20) areswapped.

Page 9: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 9/48

C Programming & Data Structures U N I T 3 P a g e | 9

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

2. CAL BY REFERENCE:In this mechanism, addresses are passed through a function. The formal

parameters will be the pointers to the actual arguments.Any changes made to the formal arguments, will also be taken place in the actual

arguments.

Ex:- Program to swap a , b values

/*PROGRAM TO ILLUSTRATE ABOUT CALL BY REFERENCE*/

#include<stdio.h>void Swap(int *,int *);main(){

int a=10, b=20;clrscr();

printf(" \n\n\t In main, The values before swap: a=%d b=%d", a,b);Swap(&a, &b);printf(" \n\n\t In main, The values after swap: a=%d b=%d", a,b);getch();

}void Swap(int *x, int *y){

int c;printf(" \n\n\t in swap x=%u \t y=%u", &x,&y);c=*x;*x=*y;*y=c;printf(" \n\n\t The values after swap in swap function: *x=%d *y=%d", *x,*y);

}O/P:

In main, The values before swap: a=10 b=20

The values after swap in swap function: *x=20 *y=20

In main, The values after swap: a=20 b=10 

Page 10: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 10/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 10

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

INTER FUNCTION COMMUNICATION:

The data flow between the calling function and called function is divided into 3categories.1. Downward flow2. Upward flow3. Bidirectional flow1. Downward flow:

In the downward flow, data flows from the calling function to the called function. Any changes made to the called function will not take effect the calling function.

Ex:-Functions with Arguments and without return value mechanismCall by value mechanism

2. UPWARD FLOW:

In the upward data flow the data flows from the called function to the callingfunction.

Using the return statement we can send the data back to the calling function. If we want to return more than one value we can use the call by ref mechanism.

Ex: Functions without Arguments and with return value mechanism

3. BI DIRECTIONAL FLOW:

In this category the data flows in both the directions i.e., from calling function tocalled function and vice-versa. 

Ex:

Functions with Arguments and with return value mechanism Call by ref mechanism

Page 11: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 11/48

C Programming & Data Structures U N I T 3 P a g e | 11

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

RECURSION:Recursion is the process in which a function calls itself also called "circular function". 

Using recursion we can avoid loops.

Whenever recursion is used we have to write if condition to terminate recursivefunction calls.

/*PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING RECURSION*/

#include<stdio.h>

int fact(int);

void main()

{

int n,f;

printf("\n Enter Number");

scanf("%d",&n);f=fact(n);

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

getch();

}

int fact(int m)

{

if(m==1)

return 1;else

return m*fact(m-1);

}

o/p: Enter Number5 5!=120

/*PROGRAM TO PRINT THE FIBONACCI SERIES USING RECURSION*/

term(int);

void main()

{

int i,n;

printf("\nHow many Fibonacci Numbers you need? ");

scanf("%d",&n);

printf("\nThe Fibo Series of %d Terms is:",n);

for(i=1;i<=n;i++)

printf("\t %d",term(i));}

Page 12: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 12/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 12

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

term(int n)

{

if(n==1)

return 0;

if(n==1 || n==2)return 1;

else

return (term(n-1)+term(n-2));

}

O/P:

How many Fibonacci Numbers you need? 5

The Fibo Series of 5 Terms is: 0 1 1 2 3

/*PROGRAM TO PRINT THE nth FIBONACCI NUMBER USING RECURSION*/

term(int);

void main()

{

int i,n;

clrscr();

printf("\nEnter the position of Fibo Number" );scanf("%d",&n);

printf("\t%d Fibo Number is: %d",n,term(n));

getch();

}

term(int n)

{

if(n==1)return 0;

if(n==1 || n==2)

return 1;

else

return (term(n-1)+term(n-2));

}

O/P: Enter the position of Fibo Number10

10 Fibo Number is: 34

Page 13: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 13/48

C Programming & Data Structures U N I T 3 P a g e | 13

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

/*PROGRAM TO PRINT THE GCD & LCM OF 2 NUMBERS USING

RECURSION*/

#include<stdio.h>

int gcd(int a, int b)

{if(a<0) a=-a;

if(b<0) b=-b;

if(a==0 || b==1 || a==b)

return b;

if(a==1 || b==0)

return a;

if(a>b)

return gcd(b,a%b);

else

return gcd(a,b%a);

}

void main()

{

int x,y,z;

clrscr();printf("\nEnter Two Numbers" );

scanf("%d%d",&x,&y);

z=gcd(x,y);

printf("\n\n GCD(%d,%d)=%d",x,y,z);

printf("\n\n LCM(%d,%d)=%d",x,y,(x*y)/z);

getch();

}

O/P:Enter Two Numbers: 36 84

GCD(36,84)=12

LCM(36,84)=252

Page 14: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 14/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 14

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

/*PROGRAM TO PRINT ADDTION OF TWO NUMBERS WITHOUT USING

ARITHMATIC OPERATOR USING RECURSION*/

#include<stdio.h>

add(int a, int b)

{if(a==0)

return b;

else

return add(--a,++b);

}

void main()

{

int a=20,b=30;

clrscr();

printf("\n\n %d+%d=%d",a,b,add(a,b));

getch();

}

O/P: 20+30=50

STORAGE CLASSESAlong with a data type of a variable, we can also use other keywords called as

“storage classes”. 

Using storage classes, we can determine the scope, visibility, and life time ofvariables.

SCOPE:The place where a variable has to be declared.

It is of two types. Local scope Global scope

VISIBILITY:The area up to which a variable can be accessed.

It can be up to a block or a function or entire program.

LIFETIME: It is the duration of time of a variable exists in memory.

Storage classes are of 4 types:

Automatic Variables.

External Variables.

Static Variables. Register Variables.

Page 15: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 15/48

C Programming & Data Structures U N I T 3 P a g e | 15

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

1. AUTOMATIC VARIABLES:

These are declared inside a function or a block.

These variables will be created when a function execution begins and destroyed

when a function execution ends.

Scope : Local

Visibility : Inside a block (or) a function.

Life time : Till the fun execution ends.

Default value: Garbage value.

An automatic variable can be declared using the keyword “auto ”.Ex: auto int a;

If no storage class is specified for a variable then by default it will be treated asautomatic variable.

/*PROGRAM TO ILLUSTRATE

ABOUT STORAGE CLASSES AUTO

INT*/

#include<stdio.h>

void main()

{

auto int i=1;

{{

{

printf("\ti=%d",i);

}

printf("\ti=%d",i);

}

printf("\ti=%d",i);

}

getch();

O/P:

i=1 i=1 i=1

/*PROGRAM TO ILLUSTRATE

ABOUT STORAGE CLASSES AUTO

INT*/

#include<stdio.h>

void main()

{

auto int i=1;

clrscr();{

int i=2;

{

auto int i=3;

printf("\ti=%d",i);

}

printf("\ti=%d",i);

}

printf("\ti=%d",i);

getch();

}

O/P:

i=3 i=2 i=1

Page 16: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 16/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 16

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

2. EXTERNAL VAIRABLES:

These are the variables which are declared outside above all the functions. These are also known as global variables These variables can be accessed anywhere in the program and in other programs

also

Scope : GlobalVisibility : Thorough out the programLife time : Till the program execution endsDefault value : 0

/*PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT*/

#include<stdio.h>

int x=21;

void main(){

extern int y;

clrscr();

printf("x=%d y=%d",x,y);

show();

getch();

}

int y=30;

show()

{

printf("\nIn show y=%d",y);

}

O/P:

x=21 y=30

In show y=30

The external declaration tells the compiler that the variable is declaredsomewhere else in the program.

We can also use external declaration in the other programs to access the variable.

Page 17: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 17/48

C Programming & Data Structures U N I T 3 P a g e | 17

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

/*PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT */

#include<stdio.h>

void main()

{

extern int a;clrscr();

printf("\na=%d",a);

show();

getch();

}

int a;

show()

{

int b;

a++;

printf("\nIn show a=%d b=%d",a,b);

}

O/P:

a=0

Observe that In function show a=1 & b=523Default value of a is 0 and value of b is garbage

STATIC VAIRABLES:

It is a variable which stores its value thorough out the program.The variable can be declared as local or global.A local static variable will be created and initialized only once when the fun is executedfor the first time.

Scope : LocalVisibility : Inside a block or a funLife time : Till the program execution endsDefault value : 0

Page 18: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 18/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 18

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

NOTE: The difference between a global static variable and a normal global variable isnormal global variable is a global variable can be accessed in the other programs where asglobal static variable can be accessed only in a single program, in which it is declared.

Page 19: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 19/48

C Programming & Data Structures U N I T 3 P a g e | 19

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

REGISTER VARIABLES: These variables will be stored in a special place called registers of CPU. As the size of the registers is limited, only few values can be stored. Some computers stores only integers and character variables in the registers.

Scope : Local

Visibility : Inside a block/a funLife time : Till the function execution ends.Default value : Garbage value.

/*PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES REGISTER INT*/

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

register int i=1;clrscr();printf("\nRegister int i=%d",i);getch();

}O/P:

Register int i=1

The following Table tells the summary of Storage Classes:

AUTO EXTERN STATIC REGISTER

SCOPE Local Global Local/Global Local

VISIBILITYInside a block (or)a function

Thorough outthe program

Inside a block or afun

Inside a block / afunction

LIFE TIMETill the function /block execution ends

Till the programexecution ends

Till the programexecution ends

Till the function /block execution ends.

DEFAULT

VALUE

Garbage value 0 0Garbage value.

Page 20: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 20/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 20

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

TYPE QUALIFIERS:Type qualifiers changes the behavior of the variablesThere are 3 types of qualifiers1. const2. volatile3. restric

1) const:This qualifier declares a variable as a constantEx: const float pi=3.14;

2) volatile:This qualifier tells the compiler that a variable is shared by other programs.Ex: volatile int a;

NOTE:const & volatile qualifiers can be applied to the variables but restric qualifier should be applied

only to pointers only.

STANDARD FUNCTIONS:Some of the standard mathematical functions are

1. abs( ) 2. ceil( ) 3. floar( ) 4. trunc( )5. round( ) 6. pow( ) 7. sqrt( )

1. abs( )This fun gives the absolute value of a number (positive number) of given numberex: abs(-3) =3;

abs(3) =3;use the printf statements to check the above..like printf(“%d”, abs(-3)); 

2. ceil( )This fun gives a number greater than orequal to a given number.

Ex: ceil(1.1) gives 2.0ceil(-1.9) gives -1.0

3. floar( )This function gives a number less than orequal to the given number.

Ex: floar (1.1) = 1.0floar (-1.9) = -2.0

4. trunc( )The truncate function gives a numbertowards the direction of zero.It is same as ceil function for negativenumbers and floar function for positivenumbers

Page 21: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 21/48

C Programming & Data Structures U N I T 3 P a g e | 21

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

Ex: trunc (1.1) = 1.0trunc (-1.9) = -1

5. round( )This function gives the nearest integer of a given number

Ex: round(1.9) =2.0 round(-1.9)=-2.0round(1.5) =2.0 round(-1.5)=-1round(1.4) =1.0 round(-1.4)=-1round(-0.5)=0

6. pow( )It finds the power of a given numberSyntax: pow (x,n);Ex: pow(4,2) = 16

pow(3.1,2) = 9.16

7. sqrt( )It finds the square root of a given number.

Ex: sqrt(4) = 2.0

NOTE:

To work with the above mathematical functions, include math.h header file. Even if so, some

functions works depends on the compiler your are using.

PREPROCESSOR COMMANDS:A pre processor is a program which takes the source program, processes it and sends to thecompiler. All the processor commands begin with # (hash or pound)

1. file inclusion2. macro definition3. conditional compilation

1. FILE INCLUSION:The command for file inclusion is # include. This command loads the specified file into theprogram.

Syntax: # include <filename.h># include "filename.h"

If the file name given in angular brackets “< >” then the file will be searched only (in thesystem directories) specified list of directions only.

If the file name is given in double quotes “ “, then the file will be searched in the currentdirectory and also in the specified list of directories as mentioned in the include searchpath that might have been setup.

Include search path is nothing but a list of directories that would be searched for the filebeing included.

If you are using turbo C/C++ compiler, then the search path can be set by selecting“Directories” from the “options” menu. In this include directories you can specify the path.We can also specify multiple include paths separated by as shown below.

Page 22: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 22/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 22

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

c:\tc\lib; c:\mylib; d:\librariesEXAMPLE: #include<stdio.h>#include “prime.c” 

/* PROGRAM TO ILLUSTRATE ABOUT MACROS- FILE INCLUSION*/

#include "stdio.h"#include "conio.h"

void main( ){

clrscr( );printf("\n Observe the preprocessor commands in the code");getch();

}O/P: Observe the preprocessor commands in the code

2. MACRO DEFINITION

“#define” statement is called the macro definition. It is used to define identifiers withvalue/statement. The value will be replaced for every occurrence of the identifiers in theprogram.

Syntax: #define identifierEx:

/* PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION*/

#include <stdio.h>#include "conio.h"#define top 10

void main( ){

int i=1;for(;i<=top;i++)

printf(“\t%d”,i); getch();

}O/P: 1 2 3 4 5 6 7 8 9 10

EX:

/* PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION*/#include <stdio.h>#include "conio.h"#define p1 3.142void main( ){

float r=1, area;area=p1*r*r;printf(“\n\t area= %f”,area); 

}O/P: area=3.142000 

Page 23: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 23/48

C Programming & Data Structures U N I T 3 P a g e | 23

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

In the above top &p1  are called “Macro Templates” and 10, 3.142   are called “MacroExpansions”. It is customary (usually but not mandatory) to use uppercase for macro template.A space between # and define is optional and macro definition is never to be terminated by“;”. 

EX:

/* PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION*/

#include<stdio.h>#define AND &&#define RANGE (a>10 AND a<50)void main( ){

int a=35;

if (RANGE)printf(“\n Within Range”); else

printf("\n Out of Range");}

O/P: Within Range

/*PROGRAM TO ILLUSTRATE ABOUT MACROS- MACROS WITH ARGUMENTS*/

#include<stdio.h>

#define AREA(x) (3.14*x*x)void main( ){

float r1=6, r2=2.5, a;clrscr();a=AREA(r1);printf("Area=%f",a);a=AREA(r2);

printf("\nArea=%f",a);getch();

}O/P:

Area = 113.040001

Area = 19.625

Page 24: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 24/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 24

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

3. CONDITIONAL COMPILATION:If we want, we can have the compiler skip over part of a source code by insertingpreprocessing commands #ifdef and #endif.

Syntax: #ifdef identifierstt-1;

#else

stt-2;#endif

EX:void main( ){

#ifdef INTELCode suitable for intel pc#elsecode suitable for motorola pc#endifNext code command to both computers.

}EX:

/*PROGRAM TO ILLUSTRATE ABOUT MACROS:

CONDITIONAL COMPILATION*/

#include <stdio.h>#define E =void main(){

int a,b;#ifdef E

a E 10;b E 20;

#elsea = 30;b = 40;

#endifprintf("\na=%d,b=%d",a,b);}OUT PUT: a = 10, b = 20

(Observe by writing any other alphabet or expression instead E in the function main( ), you will get the output as 30 and 40).

Page 25: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 25/48

C Programming & Data Structures U N I T 3 P a g e | 25

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

ARRAYS

Suppose we wish to arrange the percentage marks obtained by 100 students inascending order. In such case we have two options Construct 100 variables to store 100percentage marks, construct one variable (called an array) capable of storing or holding allthe 100 values. Obviously the second option is better. Moreover there are certain logics that

cannot be dealt with, without the use of arrays.

To process large amounts of data, C supports a derived data type known as “ARRAYS” thatcan be used for such applications.

Definition:An Array is a fixed size of sequenced collection of elements of same data type. An Array is a collection of homogeneous data items that share a common name. Arrays are used to store more than one value in a single variable.

TYPES OF ARRAYS

There are 3 types of arrays One-dimensional Array Two- dimensional Array Multi- dimensional Array

ONE-DIMENSIONAL ARRAY:An Array with single subscript is called one dimensional Array (or) single subscripted array.

Syntax: datatype array_name [size];

We can store (or) access each array element using its index. An array index starts from zero. The array elements are stored in continuous memory locations.

INTIALIZATION:An array can be initialized at two stages

Compile time Run time

Compile Time Initialization:

Syntax: datatype array_name [size] = { list of values} ;

Page 26: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 26/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 26

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

If only values are initialized then the remaining values will be automatically zero.

While initializing all the values we can omit the size of an array.

The more values are initialized then the max size of an array, then the compiler shows anerror.

Run time initialization: This is used for large size of arrays

In C, the name of an array represents the address of the first element(base address) C calculates the address of each array element using the formulae.

Element address = base address + index * Scale factor

Scale factor means size of data type (In bytes) size of the element.

Address of a[3] = Base Address of a + index * Scale Factor= 1000 + 3 * 2= 1006

/*PROGRAM TO READ AND PRINT A 1DIM ARRAY*/

#include<stdio.h>#include<conio.h>void main(){

int a[5],i;clrscr();for(i=0;i<5;i++){

printf("\n\t Enter a%d=",i);scanf("%d",&a[i]);

}

printf("\nArray entered is:");for(i=0;i<5;i++){

Page 27: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 27/48

C Programming & Data Structures U N I T 3 P a g e | 27

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

printf("\ta[%d]=%d",i,a[i]);}getch();

}o/p:

Enter a0=1

Enter a1=2Enter a2=3

Enter a3=4

Enter a4=5

Array entered is:

a[0]=1 a[1]=2 a[2]=3 a[3]=4 a[4]=5

/*PROGRAM TO READ AND PRINT THE NUMBERS AND THEIR SUM USING ARRAYS*/

void main()

{ int a[20],i,n,sum=0;clrscr();printf("\nHow many no.s you want to sum:");scanf("%d",&n);for(i=0;i<n;i++){

printf("\n\t Enter a[%d]= ",i);scanf("%d",&a[i]);

}

for(i=0;i<n;i++){

printf("\n\t a[%d]=%d",i,a[i]);sum+=a[i];

}printf("\n\t sum= %d",sum);getch();

}o/p:

How many no.s you want to sum:5

Enter a[0]= 2Enter a[1]= 5

Enter a[2]= 3

Enter a[3]= 1

Enter a[4]= 8

a[0]=2 a[1]=5 a[2]=3 a[3]=1 a[4]=8 sum= 19

Page 28: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 28/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 28

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

/*.PROGRAM TO PRINT THE ODD SUM AND EVEN SUM OF N ARRAY ELEMENTS WHERE

N IS SUPPLIED BY THE USER.*/

#include<stdio.h>#include<conio.h>void main(){

int a[50],i,n,esum=0,osum=0;clrscr();printf("\n How many elements you want to enter?");scanf("%d",&n);for(i=0;i<n;i++){printf("\n\tEnter a%d=",i);scanf("%d",&a[i]);if((a[i]%2)==0)

esum+=a[i];elseosum+=a[i];

}printf("\n The even sum and odd sum are: %d & %d", esum, osum);getch();

}o/p:

How many elements you want to enter? 5

Enter a0=2

Enter a1=3Enter a2=4

Enter a3=5

Enter a4=6

The even sum and odd sum are: 12 & 8

/*PROGRAM TO FIND MIN AND MAX USING ARRAYS*/

#include<stdio.h>#include<conio.h>void main()

{int a[5],i,min,max;for(i=0;i<5;i++){

printf("\n\tEnter a%d= ",i);scanf("%d",&a[i]);

}min=a[0];max=a[0];

for(i=0;i<5;i++){

Page 29: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 29/48

C Programming & Data Structures U N I T 3 P a g e | 29

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

printf("\n\ta%d=%d",i,a[i]);if(min>a[i])

min=a[i];if(max<a[i])

max=a[i];}

printf("\n\n\t min=%d\tmax=%d",min,max);getch();

}O/P

Enter a0= 6

Enter a1= 3

Enter a2= 0

Enter a3= 2

Enter a4= 9a0=6 a1=3 a2=0 a3=2 a4=9

min = 0 max=9

TWO DIMENSIONAL ARRAYS:An array with two subscripts is called as 2-Dim array or double subscripted array.

These are used to store a table of values.

Syntax to declare 2-D arraysdata type array name [row size][column size];

We can access each individual element using its row number and column number Ex: a[0][1] represents the value in 0th row and 1st column.

Initialization:

Initialization will be done by row by row. When an array is completely initialized with all the elements then there is no need to

specify size in the 1st subscript.

If some values are not given, then they will be zero

Page 30: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 30/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 30

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

We can initialize all the elements as zero,

/*PROGRAM TO READ AND PRINT THE ELEMENTS OF A 2 DIM ARRAY (3X3 MATRIX)

*/

void main(){

int a[3][3],i,j;clrscr();

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\n\t Enter a%d%d=",i,j);scanf("%d",&a[i][j]);

}}for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\n\n\t a%d%d=%d",i,j,a[i][j]);}

}getch();

}

Page 31: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 31/48

C Programming & Data Structures U N I T 3 P a g e | 31

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

/*PROGRAM TO ADD AND PRINT SUM OF TWO MATRICES USING ARRAYS */

#include<stdio.h>#include<conio.h>void main(){

int a[3][3],b[3][3],c[3][3],i,j;

clrscr();

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\tEnter a%d%d=",i,j);scanf("%d",&a[i][j]);

}}

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\t Enter b%d%d=",i,j);scanf("%d",&b[i][j]);

}

}

for(i=0;i<3;i++){

for(j=0;j<3;j++){

c[i][j]=a[i][j]+b[i][j];printf("\t%d",c[i][j]);

}}

getch();}o/p:

Enter a00=1

Enter a01=2

Enter a02=3

Enter a10=4

Enter a11=5

Enter a12=6

Enter a20=7

Enter a21=8

Enter a22=9

Enter b00=9

Enter b01=8

Enter b02=7

Enter b10=6

Enter b11=5

Enter b12=4

Enter b20=3

Enter b21=2

Enter b22=1

10 10 10

10 10 10

10 10 10

Page 32: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 32/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 32

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

/*PROGRAM TO PRINT THE PRODUCT OF TWO 3X3 MATRICES USING ARRAYS*/

#include<stdio.h>#include<conio.h>void main(){

int a[3][3],b[3][3],c[3][3],i,j,k;

clrscr();

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\tEnter a%d%d=",i,j);scanf("%d",&a[i][j]);

}

}

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\t Enter b%d%d=",i,j);scanf("%d",&b[i][j]);

}}

/*for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\n\ta%d%d=%d",i,j,a[i][j]);}

}for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\n\t b%d%d=%d ",i,j,b[i][j]);}

}*/

for(i=0;i<3;i++){

for(j=0;j<3;j++){

Page 33: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 33/48

C Programming & Data Structures U N I T 3 P a g e | 33

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

c[i][j]=0;for(k=0;k<2;k++){

c[i][j]=c[i][j]+a[i][k]*b[k][j];

}

printf("\t%d",c[i][j]);}printf("\n");

}/* for(i=0;i<3;i++)

{for(j=0;j<3;j++){

printf("\t%d",c[i][j]);

}}*/

getch();}

o/p:

Enter a00=1

Enter a01=2

Enter a02=3

Enter a10=4Enter a11=5

Enter a12=6

Enter a20=7

Enter a21=8

Enter a22=9

Enter b00=9

Enter b01=8

Enter b02=7Enter b10=6

Enter b11=5

Enter b12=4

Enter b20=3

Enter b21=2

Enter b22=1

Product of the above two

matrices is

21 18 15

66 57 48

111 96 81

Page 34: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 34/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 34

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

/*PROGRAM TO FIND THE TRANSPOSE OF A MATRIX*/

void main(){

int a[3][3],b[3][3],i,j;clrscr();for(i=0;i<3;i++)

{for(j=0;j<3;j++){

printf("Enter a%d%d= ",i,j);scanf("%d",&a[i][j]);

}}

for(i=0;i<3;i++){

for(j=0;j<3;j++){b[j][i]=a[i][j];

}}printf("\n THE TRANSPOSE OF MATRIX \n");for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\tb%d%d=%d",i,j,b[i][j]);}printf("\n");

}getch();

}O/P:

Enter a00= 1

Enter a01= 2

Enter a02= 3

Enter a10= 4Enter a11= 5

Enter a12= 6

Enter a20= 7

Enter a21= 8

Enter a22= 9

THE TRANSPOSE OF MATRIXb00=1 b01=4 b02=7

b10=2 b11=5 b12=8

b20=3 b21=6 b22=9

Page 35: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 35/48

C Programming & Data Structures U N I T 3 P a g e | 35

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

/*PROGRAM TO CHECK WHETHER A MATRIX IS SYMMETRIC OR NOT */

#include<stdio.h>#include<conio.h>void main(){

int a[3][3],i,j;

clrscr();for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("Enter a[%d][%d]=",i,j);scanf("%d",&a[i][j]);

}}

for(i=0;i<3;i++){for(j=0;j<3;j++){

if(a[i][j]==a[j][i])continue;

else{

printf("\n\t the matrix is not symmetric",a[i][j]);

getch();exit(0);

}}

}printf("\nThe matrix is symmetric",a[i][j]);getch();

}O/P:

Enter a[0][0]=2

Enter a[0][1]=3Enter a[0][2]=7

Enter a[1][0]=3

Enter a[1][1]=4

Enter a[1][2]=8

Enter a[2][0]=7

Enter a[2][1]=8Enter a[2][2]=5

The matrix is symmetric

Page 36: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 36/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 36

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

/*PROGRAM TO CHECK WHETHER A MATRIX IS SKEW SYMMETRIC OR NOT */

void main(){

int a[3][3],b[3][3],i,j;

clrscr();

for(i=0;i<3;i++){

for(j=0;j<3;j++){

printf("\n\t Enter a%d%d= ",i,j);scanf("%d",&a[i][j]);

}}for(i=0;i<3;i++)

{ for(j=0;j<3;j++){

if(a[i][j]==-a[j][i])continue;

else{

printf("Matrix is not skew symmetric");getch();exit(0);

}}

}printf("\nThe Matrix is Skew Symmetric");getch();

}O/P:

Enter a00= 0

Enter a01= -3

Enter a02= -7

Enter a10= 3Enter a11= 0

Enter a12= -8

Enter a20= 7

Enter a21= 8

Enter a22= 0

The Matrix is Skew Symmetric

Page 37: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 37/48

C Programming & Data Structures U N I T 3 P a g e | 37

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

/*PROGRAM TO SORT THE ELEMENTS OF AN ARRAY IN ASCENDING ORDER*/

void main(){

int a[10],i,n,j,t,k;clrscr();

printf("\nHow many no.s you enter?");scanf("%d",&n);for(i=0;i<n;i++){

printf("Enter element %d: ",i+1);scanf("%d",&a[i]);

}for(i=0;i<n;i++){

for(j=0;j<n-i-1;j++){

if(a[j]>a[j+1]){

t=a[j];a[j]=a[j+1];a[j+1]=t;

}}

}printf("\n Sorted elets are: \n");for(i=0;i<n;i++)

printf("\t%d",a[i]);getch();

}o/p:

How many no.s you enter? 5

Enter element1: 99

Enter element2: 34Enter element3: 21

Enter element4:44

Enter element5:12

Sorted elets are:

12 21 34 44 99

Note that the above program is Justlogic of Bubble sort which you will learnin Unit 7. As a token of application of

Arrays I mentioned here.

Page 38: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 38/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 38

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

MULTI DIMENSIONAL ARRAYSAn array with more than two dim is called as multi dim array.

Syntax:datatype array name [index] [index] [index].............;

Syntax to declare a 3-d arraydatatype array_name[planes][rows][cols];

Ex: int a[3][2][3];The first subscript represents the number of tables or planes to be created.The second and third subscript represents the number Of rows and cols for each plane.

Initialization:

int a[3][2][3]= { { {1, 2, 3}, {4, 5, 6} }, { {7, 8, 9}, { 10, 11, 12 } }, { {13, 14, 15}, { 16, 17, 18 } } };

/* PROGRAM TO READ AND PRINT A 3-D ARRAY */

#include<stdio.h>#include<conio.h>void main(){

int a[2][2][2],i,j,k;clrscr();for(i=0;i<2;i++){

for(j=0;j<2;j++){

for(k=0;k<2;k++){

printf("\n\t enter a[%d][%d][%d]=",i,j,k);

scanf("%d",&a[i][j][k]);}

Page 39: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 39/48

C Programming & Data Structures U N I T 3 P a g e | 39

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

}}for(i=0;i<2;i++){

for(j=0;j<2;j++)

{for(k=0;k<2;k++){

printf("\n\t a[%d][%d][%d]=%d",i,j,k,a[i][j][k]);}

}}

getch();}o/p:

enter a[0][0][0]=1

enter a[0][0][1]=2

enter a[0][1][0]=3

enter a[0][1][1]=4

enter a[1][0][0]=5

enter a[1][0][1]=6

enter a[1][1][0]=7

enter a[1][1][1]=8

a[0][0][0]=1

a[0][0][1]=2

a[0][1][0]=3

a[0][1][1]=4

a[1][0][0]=5

a[1][0][1]=6

a[1][1][0]=7

a[1][1][1]=8

Page 40: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 40/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 40

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

INTER FUNCTION COMMUNICATION:We can pass arrays to functions in two ways.1. Passing individual elements2. Passing a whole array.

1. Passing individual elements: Just like normal variables we can pass individual elements of an array to a function We can pass either the values or the address of an array to a function. To pass a value to a function, give the array name along with its index in the function

call. We can pass the address of individual array elements to a function

/*PROGRAM TO SEND AN INDIVIDUAL ELEMENT TO A FUNCTION*/

void fun(int x){

printf("\t%d",x);

}main(){

int a[5],i;clrscr();

for(i=0;i<5;i++){

printf("Enter a[%d]= ",i);scanf("%d",&a[i]);

}

for(i=0;i<5;i++){

fun(a[i]);}getch();

}o/p:

Enter a[0]= 1Enter a[1]= 2

Enter a[2]= 3

Enter a[3]= 4

Enter a[4]= 5

1 2 3 4 5

Page 41: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 41/48

C Programming & Data Structures U N I T 3 P a g e | 41

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

/* PASSING ADDRESS OF INDIVIDUAL ARRAY ELEMENT TO FUN*/

void fun(int *);void main(){

int a[5]={1,2,3,4,5},i;

clrscr();for(i=0;i<5;i++){

fun(&a[i]);}getch();

}void fun(int *x){

printf("\t%d",*x);}O/P:

1 2 3 4 5

2. Passing a whole array:

One-Dimension Array passing:

The function prototype must show that the argument is an array.

The function must be called by passing array name and size. To pass a 1-Dim array we have to specify the array name without subscript and with

size as arguments. In function definition, the formal parameters must be an array type and no need to

specify the size inside the subscript.

/* PASSING A WHOLE ARRAY A TO A FUNCTION*/

void fun(int[],int);void main(){

int a[]={1,2,3,4,5},i;clrscr();fun(a,5);getch();

}void fun(int x[],int n){

int j;for(j=0;j<n;j++)

printf("\ta%d=%d",j,x[j]);

}

Page 42: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 42/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 42

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

o/p:

a0=1 a1=2 a2=3 a3=4 a4=5

In C, the name of an array represents the address of its first element. By passing an array to a function the address of the array will be sent to the function.

The formal parameters refer to the actual array stored in the memory. Any changes made in the formal arguments will be reflected in the original array.

/* PASSING A WHOLE ARRAY A TO A FUNCTION*/

void fun(int[],int);void main(){

int a[]={1,2,3,4,5},i;clrscr();fun(a,5);

for(i=0;i<5;i++)printf(" \n\t NEW ARRAY\t a%d=%d",i,a[i]);

getch();}void fun(int x[],int n){

int j;for(j=0;j<n;j++){

printf("\ta%d=%d",j,x[j]);

x[j]+=+9;}

}

o/p:

a0=1 a1=2 a2=3 a3=4 a4=5

NEW ARRAY a0=10

NEW ARRAY a1=11

NEW ARRAY a2=12

NEW ARRAY a3=13

NEW ARRAY a4=14

Observe that any changes made in the function affecting the original array, which meanssending the whole array means sending its address.

Two Dimension Array passing:

To pass a two dimensional array to a function ,we have to specify the array name and thetwo dim in the function call.

The function must be called by passing array name and the size of rows and columns.

Page 43: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 43/48

C Programming & Data Structures U N I T 3 P a g e | 43

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

In the function definition, the formal parameters must be an array type and the size ofsecond dim should be specified.The function prototype must show that the argument is an array.

/*PASSING A WHOLE 2-D ARRAY TO A FUN*/

void fun(int[][],int,int);void main(){

int a[][2]={{1,2},{3,4}};clrscr();fun(a,2,2);

getch();}void fun(int x[][2],int m,int n)

{int i,j;

for(i=0;i<m;i++){

for(j=0;j<n;j++){

printf("\ta%d%d=%d",i,j,x[i][j]);}

}

}O/p:

a00=1 a01=2 a10=3 a11=4

/*PASSING A WHOLE 2-D ARRAY TO A FUN*/

void fun(int[][],int,int);void main(){

int a[2][3]={1,2,3,4,5,6},i,j;

clrscr();fun(a,2,3);printf("\n\n\n");for(i=0;i<2;i++){

for(j=0;j<3;j++){

printf(" \n\t NEW ARRAY\t a%d%d=%d",i,j,a[i][j]);}

}

getch();}

Page 44: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 44/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 44

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

void fun(int x[][3],int m,int n){

int i,j;

for(i=0;i<m;i++)

{for(j=0;j<n;j++){

printf("\ta%d%d=%d",i,j,x[i][j]);x[i][j]+=9;

}}

}O/P:

a00=1 a01=2 a02=3 a10=4 a11=5 a12=6

NEW ARRAY a00=10

NEW ARRAY a01=11

NEW ARRAY a02=12

NEW ARRAY a10=13

NEW ARRAY a11=14

NEW ARRAY a12=15

APPLICATIONS OF ARRAYS: Searching Sorting Matrix applications String manipulations Function parameters Structures Pointers Data structures

Page 45: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 45/48

C Programming & Data Structures U N I T 3 P a g e | 45

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

SUMMARY OF ALL PROGRAMS FROM UNIT 3 

1)  PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHOUT RETURN VALUE

2)  PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITH

RETURN VALUE3)  PROGRAM TO ILLUSTRATE FUNCTION WITH ARGUMENTS AND WITHOUT 

RETURN VALUE4)  PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHOUT 

RETURN VALUE5)  PROGRAM TO ILLUSTRATE ABOUT CALL BY VALUE6)  PROGRAM TO ILLUSTRATE ABOUT CALL BY REFERENCE7)  PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING RECURSION8)  ROGRAM TO PRINT THE FIBONACCI SERIES USING RECURSION

9)  PROGRAM TO PRINT THE nth FIBONACCI NUMBER USING RECURSION10)  PROGRAM TO PRINT THE GCD & LCM OF 2 NUMBERS USING RECURSION11)  PROGRAM TO PRINT ADDTION OF TWO NUMBERS WITHOUT USING

ARITHMATIC OPERATOR USING RECURSION12)  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES AUTO INT 13) 

a.  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT^b.  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT^

^ Different examples given over there.

14)  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES STATIC INT 15)  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES REGISTER INT 16)  PROGRAM TO ILLUSTRATE ABOUT MACROS- FILE INCLUSION17) 

a.  PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION^b.  PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION^c.  PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION^d.  PROGRAM TO ILLUSTRATE ABOUT MACROS- MACROS WITH ARGUMENTS

^ Three different examples given over there. Prefer c & d for exams/Records

18)  PROGRAM TO ILLUSTRATE ABOUT MACROS: CONDITIONAL COMPILATION19)  PROGRAM TO READ AND PRINT A 1DIM ARRAY20)  PROGRAM TO READ AND PRINT THE NUMBERS AND THEIR SUM USING ARRAYS21)  PROGRAM TO PRINT THE ODD SUM AND EVEN SUM OF N ARRAY ELEMENTS

WHERE N IS SUPPLIED BY THE USER.22)  PROGRAM TO FIND MIN AND MAX USING ARRAYS23)  PROGRAM TO READ AND PRINT THE ELEMENTS OF A 2 DIM ARRAY (3X3 MATRIX)24)  PROGRAM TO ADD AND PRINT SUM OF TWO MATRICES USING ARRAYS25)  PROGRAM TO PRINT THE PRODUCT OF TWO 3X3 MATRICES USING ARRAYS26)  PROGRAM TO FIND THE TRANSPOSE OF A MATRIX

Page 46: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 46/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 46

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna 

27)  PROGRAM TO CHECK WHETHER A MATRIX IS SYMMETRIC OR NOT 28)  PROGRAM TO CHECK WHETHER A MATRIX IS SKEW SYMMETRIC OR NOT 29)  PROGRAM TO READ AND PRINT A 3-D ARRAY30)  PROGRAM TO PASSING ADDRESS OF INDIVIDUAL ARRAY ELEMENT TO FUN31)  PROGRAM TO PASSING A WHOLE ARRAY A TO A FUNCTION

32)  PROGRAM TO PASSING A WHOLE 2-D ARRAY TO A FUN

===========================ALL THE BEST===============================

Page 47: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 47/48

C Programming & Data Structures U N I T 3 P a g e | 47

Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

LAB PROGRAMS FROM UNIT 3

LAB 7 

7.1)  PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHRETURN VALUE

7.2)  PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHOUT 

RETURN VALUE7.3)  PROGRAM TO ILLUSTRATE ABOUT CALL BY VALUE7.4)  PROGRAM TO ILLUSTRATE ABOUT CALL BY REFERENCE

LAB 8 8.1)  ROGRAM TO PRINT THE FIBONACCI SERIES USING RECURSION8.2)  PROGRAM TO PRINT THE GCD & LCM OF 2 NUMBERS USING RECURSION8.3)  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT 8.4)  PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES STATIC INT 8.5)  PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION8.6)  PROGRAM TO ILLUSTRATE ABOUT MACROS- MACROS WITH ARGUMENTS

LAB 9 9.1)  PROGRAM TO PRINT THE ODD SUM AND EVEN SUM OF N ARRAY ELEMENTS,

N SUPPLIED BY THE USER.9.2)  PROGRAM TO FIND MIN AND MAX USING ARRAYS9.3)  PROGRAM TO ADD AND PRINT SUM OF TWO MATRICES USING ARRAYS9.4)  PROGRAM TO PRINT THE PRODUCT OF TWO 3X3 MATRICES USING ARRAYS9.5)  PROGRAM TO READ AND PRINT A 3-D ARRAY

LAB 10 10.1)  PROGRAM TO FIND THE TRANSPOSE OF A MATRIX10.2)  PROGRAM TO CHECK WHETHER A MATRIX IS SYMMETRIC OR NOT 10.3)  PROGRAM TO CHECK WHETHER A MATRIX IS SKEW SYMMETRIC OR NOT 10.4)  PROGRAM TO PASSING ADDRESS OF INDIVIDUAL ARRAY ELEMENT TO FUN10.5)  PROGRAM TO PASSING A WHOLE ARRAY A TO A FUNCTION10.6)  PROGRAM TO PASSING A WHOLE 2-D ARRAY TO A FUN

Page 48: Cpds Notes Unit3 30-1-12 100am

8/3/2019 Cpds Notes Unit3 30-1-12 100am

http://slidepdf.com/reader/full/cpds-notes-unit3-30-1-12-100am 48/48

FUNCTIONS & ARRAYS U N I T 3 P a g e | 48

ASSIGNMENT / UNIVERSITY QUESTIONS

JUN2011 and MAY-DEC 2010 (4 sets from each year)

JUN2011

3.1)  Explain the following storage classes with examples: auto, register, extern.

3.2)  Explain how two dimensional arrays can be used to represent matrices. Write C code to

perform matrix addition and matrix multiplication.3.3)  Explain about memory allocation functions in C.

3.4)  What is recursion? Write a complete C program that reads a positive integer N, compute the

first N Fibonacci numbers using recursion and print the results. Illustrate how the results are

computed when the value of N is 4?

3.5)  Explain how matrices can be represented using two dimensional arrays. Explain with code

how Transpose of a matrix can be done.

3.6)  Write a complete C program to perform these functions:

i.  to return the factorial of the given number using recursion, and

ii.  to return the factorial of the given number using iteration.

3.7)  Write a complete C prog to do the following: Read data to fill a 2dim array

int table [4] [4], then print the sum of each column and sum of each row.

MAY-DEC 2010

3.8)  Write a recursive function double power(double x, int n) that returns xn.

Write an equivalentiterative version. Compare them.

3.9)  Using arrays and iteration, Write C-language program that outputs minimum number of 

currency notes required for the given amount at input. For example, an amount of Rs.4260

shall output 1000s  –  4; 100s  –  2; 50s -1; 10s-1. The currency denominations are

1000,500,100,50,20,10,5,2 and 1

3.10)  Write recursive function int gcd(int m, int n) that returns greatest common divisor of m and n

where m>n. Write an equivalent iterative version. Compare them.(In order to find gcd, if m is exactly divisible by n, then n is the value of gcd, otherwise it is

gcd of the n and the remainder of the division. For example. gcd(6,4)=gcd(4,2)=2 )

3.11)  Write C-function int minpos(float x[], int n) that returns position of the first minimum value

among the first n elements of the given array x.

3.12)  Write C-function   float max(float a[], int n) that returns the maximum value of the first n

positions of array

3.13)  Differentiate between call by value and call by reference with suitable examples?

3.14)  Write a ‘C’ program using functions to check whether the given 3x3 matrix is symmetric or

not?

3.15)  What is the need for user-defined functions?

3.16)  What are the different ways in which 1-dimensional arrays can be declared and initialized?3.17)  Write a C program using recursion for finding GCD (Greatest Common Divisor) of two

given numbers?

3.18)  Discuss with suitable examples the storage classes available in C?

3.19)  Derive the expression for finding the address of any element of a 1-dimensional array?

3.20)  Explain different categories of functions in C with simple illustrative examples?

3.21)  Write a C program using functions to calculate the factorial of a given number?

Note:

Complete and submit the above assignment within 15 Days.


Recommended