Vous êtes sur la page 1sur 48

C Programming & Data Structures

UNIT 3

Page |1

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.

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

Page |2

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.

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

Page |3

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 wont 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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

Page |4

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

Page |5

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

Page |6

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

Page |7

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

Page |8

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 parameters any 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) are swapped.

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

Page |9

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 10

INTER FUNCTION COMMUNICATION: The data flow between the calling function and called function is divided into 3 categories. 1. Downward flow 2. Upward flow 3. Bidirectional flow 1. 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 mechanism Call by value mechanism

2. UPWARD FLOW:

In the upward data flow the data flows from the called function to the calling function. 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 to called function and vice-versa.

Ex: Functions with Arguments and with return value mechanism Call by ref mechanism
VIGNAN Institute of Technology & Science Ravi Krishna

Dept. of Computer Science

C Programming & Data Structures

UNIT 3

P a g e | 11

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 recursive function 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)); }
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 12

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 13

/*PROGRAM

TO

PRINT

THE

GCD

&

LCM

OF

NUMBERS

USING

RECURSION*/ #include<stdio.h> int gcd(int a, int b) { if(a<0) if(b<0) a=-a; 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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 14

/*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 CLASSES Along 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 of variables. 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.
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 15

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 as automatic variable. TO ILLUSTRATE /*PROGRAM 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 O/P: i=3 i=2 i=1 } printf("\ti=%d",i); getch(); } printf("\ti=%d",i); auto int i=1; clrscr(); { int i=2; { auto int i=3; printf("\ti=%d",i); TO ILLUSTRATE

/*PROGRAM INT*/ #include<stdio.h> void main() {

ABOUT STORAGE CLASSES AUTO

ABOUT STORAGE CLASSES AUTO

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 16

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 Visibility Life time Default value : : : : Global Thorough out the program Till the program execution ends 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 declared somewhere else in the program. We can also use external declaration in the other programs to access the variable .

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 17

/*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=523 Default 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 executed for the first time. Scope : Local Visibility : Inside a block or a fun Life time : Till the program execution ends Default value : 0

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 18

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 19

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 Visibility Life time Default value : : : : Local Inside a block/a fun Till the function execution ends. 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 SCOPE VISIBILITY EXTERN STATIC REGISTER

Local Inside a block (or) a function Till the function / block execution ends Garbage value

Global Thorough out the program Till the program execution ends 0

Local/Global Inside a block or a fun Till the program execution ends 0

Local Inside a block / a function Till the function / block execution ends. Garbage value.

LIFE TIME DEFAULT VALUE

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 20

TYPE QUALIFIERS: Type qualifiers changes the behavior of the variables There are 3 types of qualifiers 1. const 2. volatile 3. restric 1) const: This qualifier declares a variable as a constant Ex: 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( ) 5. round( ) 6. pow( ) 7. sqrt( )

4. trunc( )

1. abs( ) This fun gives the absolute value of a number (positive number) of given number ex: 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 or equal to a given number. Ex: ceil(1.1) gives 2.0 ceil(-1.9) gives -1.0 3. floar( ) This function gives a number less than or equal to the given number. Ex: floar (1.1) = 1.0 floar (-1.9) = -2.0 4. trunc( ) The truncate function gives a number towards the direction of zero. It is same as ceil function for negative numbers and floar function for positive numbers
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 21

Ex:

trunc (1.1) = 1.0 trunc (-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.0 round(1.5) =2.0 round(-1.5)=-1 round(1.4) =1.0 round(-1.4)=-1 round(-0.5)=0 6. pow( ) It finds the power of a given number Syntax: 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 the compiler. All the processor commands begin with # (hash or pound) 1. file inclusion 2. macro definition 3. conditional compilation 1. FILE INCLUSION: The command for file inclusion is # include. This command loads the specified file into the program. Syntax: # include <filename.h> # include "filename.h" If the file name given in angular brackets < > then the file will be searched only (in the system directories) specified list of directions only. If the file name is given in double quotes , then the file will be searched in the current directory and also in the specified list of directories as mentioned in the include search path that might have been setup. Include search path is nothing but a list of directories that would be searched for the file being 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.
VIGNAN Institute of Technology & Science Ravi Krishna

Dept. of Computer Science

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 22

c:\tc\lib; c:\mylib; d:\libraries EXAMPLE: #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 with value/statement. The value will be replaced for every occurrence of the identifiers in the program. Syntax: #define identifier Ex: /* 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.142 void main( ) { float r=1, area; area=p1*r*r; printf(\n\t area= %f,area); } O/P: area=3.142000
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 23

In the above top &p1 are called Macro Templates and 10, 3.142 are called Macro Expansions. 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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 24

3. CONDITIONAL COMPILATION: If we want, we can have the compiler skip over part of a source code by inserting preprocessing commands #ifdef and #endif. Syntax: #ifdef identifier stt-1; #else stt-2; #endif EX: void main( ) { #ifdef INTEL Code suitable for intel pc #else code suitable for motorola pc #endif Next 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; #else a = 30; b = 40; #endif printf("\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).

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 25

ARRAYS
Suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such case we have two options Construct 100 variables to store 100 percentage marks, construct one variable (called an array) capable of storing or holding all the 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 that can 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} ;

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 26

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 an error.

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++) {
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 27

} o/p:

} getch();

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

Enter a0=1 Enter a1=2 Enter 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]= 2 Enter 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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 28

/*.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]; else osum+=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=3 Enter 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++) {
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 29

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

} O/P

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

a0= 6 a1= 3 a2= 0 a3= 2 a4= 9 a0=6 a1=3 min = 0 max=9

Enter Enter Enter Enter Enter

a2=0

a3=2

a4=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 arrays data 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


VIGNAN Institute of Technology & Science Ravi Krishna

Dept. of Computer Science

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 30

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(); }

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 31

/*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
Dept. of Computer Science

Enter a21=8 Enter a22=9 Enter b00=9 Enter b01=8 Enter b02=7 Enter b10=6 Enter b11=5

10 10 10

Enter Enter Enter Enter 10 10 10

b12=4 b20=3 b21=2 b22=1 10 10 10

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 32

/*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++) {
VIGNAN Institute of Technology & Science Ravi Krishna

/*

*/

Dept. of Computer Science

C Programming & Data Structures

UNIT 3

P a g e | 33

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]);

/*

*/ } o/p:

} for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t%d",c[i][j]); } } getch();

} printf("\n");

Enter Enter Enter Enter Enter Enter Enter Enter Enter

a00=1 a01=2 a02=3 a10=4 a11=5 a12=6 a20=7 a21=8 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

Product of the above two matrices is 21 66 111 18 57 96 15 48 81

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 34

/*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 a21= 8 Enter a01= 2 Enter a22= 9 Enter a02= 3 Enter a10= 4 THE TRANSPOSE OF MATRIX Enter a11= 5 b00=1 b01=4 b02=7 Enter a12= 6 b10=2 b11=5 b12=8 Enter a20= 7 b20=3 b21=6 b22=9

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 35

/*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); }

} O/P: Enter Enter Enter Enter Enter Enter

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

a[0][0]=2 a[0][1]=3 a[0][2]=7 a[1][0]=3 a[1][1]=4 a[1][2]=8

Enter a[2][0]=7 Enter a[2][1]=8 Enter a[2][2]=5 The matrix is symmetric

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 36

/*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(); Enter a12= -8 Enter a20= 7 Enter a21= 8 Enter a22= 0 The Matrix is Skew Symmetric

} O/P:

Enter Enter Enter Enter Enter

a00= a01= a02= a10= a11=

0 -3 -7 3 0

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 37

/*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: 34 Enter element3: 21 Enter element4:44 Enter element5:12 Sorted elets are: 12 21 34 44 99 Note that the above program is Just logic of Bubble sort which you will learn in Unit 7. As a token of application of Arrays I mentioned here.

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 38

MULTI DIMENSIONAL ARRAYS An 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 array datatype 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]); }

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 39

} o/p:

} 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();

enter enter enter enter enter enter enter enter

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

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 40

INTER FUNCTION COMMUNICATION: We can pass arrays to functions in two ways. 1. Passing individual elements 2. 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 Enter Enter Enter Enter

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

1 2 3 4 5 2 3 4 5

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 41

/* 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]); }
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 42

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 NEW NEW NEW NEW NEW a1=2 ARRAY ARRAY ARRAY ARRAY ARRAY a2=3 a3=4 a0=10 a1=11 a2=12 a3=13 a4=14 a4=5

Observe that any changes made in the function affecting the original array, which means sending 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 the two dim in the function call. The function must be called by passing array name and the size of rows and columns.

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 43

In the function definition, the formal parameters must be an array type and the size of second 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(); }

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 44

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 NEW ARRAY NEW ARRAY NEW ARRAY NEW ARRAY NEW ARRAY NEW ARRAY

a10=4 a00=10 a01=11 a02=12 a10=13 a11=14 a12=15

a11=5

a12=6

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 45

SUMMARY OF ALL PROGRAMS FROM UNIT 3


1) 2) 3) 4) 5) 6) 7) 8) 9) 10) 11) 12) 13) PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHOUT RETURN VALUE PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITH RETURN VALUE PROGRAM TO ILLUSTRATE FUNCTION WITH ARGUMENTS AND WITHOUT RETURN VALUE PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHOUT RETURN VALUE PROGRAM TO ILLUSTRATE ABOUT CALL BY VALUE PROGRAM TO ILLUSTRATE ABOUT CALL BY REFERENCE PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING RECURSION ROGRAM TO PRINT THE FIBONACCI SERIES USING RECURSION PROGRAM TO PRINT THE nth FIBONACCI NUMBER USING RECURSION PROGRAM TO PRINT THE GCD & LCM OF 2 NUMBERS USING RECURSION PROGRAM TO PRINT ADDTION OF TWO NUMBERS WITHOUT USING ARITHMATIC OPERATOR USING RECURSION PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES AUTO INT a. PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT^ b. PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT^ ^ Different examples given over there.

14) 15) 16) 17)

PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES STATIC INT PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES REGISTER INT PROGRAM TO ILLUSTRATE ABOUT MACROS- FILE INCLUSION a. b. c. d. PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION^ PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION^ PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION^ PROGRAM TO ILLUSTRATE ABOUT MACROS- MACROS WITH ARGUMENTS ^ Three different examples given over there. Prefer c & d for exams/Records

18) 19) 20) 21) 22) 23) 24) 25) 26)

PROGRAM TO ILLUSTRATE ABOUT MACROS: CONDITIONAL COMPILATION PROGRAM TO READ AND PRINT A 1DIM ARRAY PROGRAM TO READ AND PRINT THE NUMBERS AND THEIR SUM USING ARRAYS PROGRAM TO PRINT THE ODD SUM AND EVEN SUM OF N ARRAY ELEMENTS WHERE N IS SUPPLIED BY THE USER. PROGRAM TO FIND MIN AND MAX USING ARRAYS PROGRAM TO READ AND PRINT THE ELEMENTS OF A 2 DIM ARRAY (3X3 MATRIX) PROGRAM TO ADD AND PRINT SUM OF TWO MATRICES USING ARRAYS PROGRAM TO PRINT THE PRODUCT OF TWO 3X3 MATRICES USING ARRAYS PROGRAM TO FIND THE TRANSPOSE OF A MATRIX

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3

P a g e | 46

27) 28) 29) 30) 31) 32)

PROGRAM TO CHECK WHETHER A MATRIX IS SYMMETRIC OR NOT PROGRAM TO CHECK WHETHER A MATRIX IS SKEW SYMMETRIC OR NOT PROGRAM TO READ AND PRINT A 3-D ARRAY PROGRAM TO PASSING ADDRESS OF INDIVIDUAL ARRAY ELEMENT TO FUN PROGRAM TO PASSING A WHOLE ARRAY A TO A FUNCTION PROGRAM TO PASSING A WHOLE 2-D ARRAY TO A FUN

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

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

C Programming & Data Structures

UNIT 3

P a g e | 47

LAB PROGRAMS FROM UNIT 3

LAB 7
7.1) 7.2) 7.3) 7.4) PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITH RETURN VALUE PROGRAM TO ILLUSTRATE FUNCTION WITHOUT ARGUMENTS AND WITHOUT RETURN VALUE PROGRAM TO ILLUSTRATE ABOUT CALL BY VALUE PROGRAM TO ILLUSTRATE ABOUT CALL BY REFERENCE

LAB 8
8.1) 8.2) 8.3) 8.4) 8.5) 8.6) ROGRAM TO PRINT THE FIBONACCI SERIES USING RECURSION PROGRAM TO PRINT THE GCD & LCM OF 2 NUMBERS USING RECURSION PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES EXTERN INT PROGRAM TO ILLUSTRATE ABOUT STORAGE CLASSES STATIC INT PROGRAM TO ILLUSTRATE ABOUT MACROS- MACRO DEFINITION PROGRAM TO ILLUSTRATE ABOUT MACROS- MACROS WITH ARGUMENTS

LAB 9
9.1) 9.2) 9.3) 9.4) 9.5) PROGRAM TO PRINT THE ODD SUM AND EVEN SUM OF N ARRAY ELEMENTS, N SUPPLIED BY THE USER. PROGRAM TO FIND MIN AND MAX USING ARRAYS PROGRAM TO ADD AND PRINT SUM OF TWO MATRICES USING ARRAYS PROGRAM TO PRINT THE PRODUCT OF TWO 3X3 MATRICES USING ARRAYS PROGRAM TO READ AND PRINT A 3-D ARRAY

LAB 10
10.1) 10.2) 10.3) 10.4) 10.5) 10.6) PROGRAM TO FIND THE TRANSPOSE OF A MATRIX PROGRAM TO CHECK WHETHER A MATRIX IS SYMMETRIC OR NOT PROGRAM TO CHECK WHETHER A MATRIX IS SKEW SYMMETRIC OR NOT PROGRAM TO PASSING ADDRESS OF INDIVIDUAL ARRAY ELEMENT TO FUN PROGRAM TO PASSING A WHOLE ARRAY A TO A FUNCTION PROGRAM TO PASSING A WHOLE 2-D ARRAY TO A FUN

Dept. of Computer Science

VIGNAN Institute of Technology & Science

Ravi Krishna

FUNCTIONS & ARRAYS

UNIT 3 ASSIGNMENT / UNIVERSITY QUESTIONS

P a g e | 48

JUN2011

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

JUN2011
3.1) 3.2) 3.3) 3.4) Explain the following storage classes with examples: auto, register, extern. Explain how two dimensional arrays can be used to represent matrices. Write C code to perform matrix addition and matrix multiplication. Explain about memory allocation functions in C. 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? Explain how matrices can be represented using two dimensional arrays. Explain with code how Transpose of a matrix can be done. 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. 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.
n.

3.5) 3.6)

3.7)

MAY-DEC 2010 3.8) 3.9)

3.10)

3.11) 3.12) 3.13) 3.14) 3.15) 3.16) 3.17) 3.18) 3.19) 3.20) 3.21)
Note:

Write a recursive function double power(double x, int n) that returns x Write an equivalent iterative version. Compare them. 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 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 ) 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. Write C-function float max(float a[], int n) that returns the maximum value of the first n positions of array Differentiate between call by value and call by reference with suitable examples? Write a C program using functions to check whether the given 3x3 matrix is symmetric or not? What is the need for user-defined functions? What are the different ways in which 1-dimensional arrays can be declared and initialized? Write a C program using recursion for finding GCD (Greatest Common Divisor) of two given numbers? Discuss with suitable examples the storage classes available in C? Derive the expression for finding the address of any element of a 1-dimensional array? Explain different categories of functions in C with simple illustrative examples? Write a C program using functions to calculate the factorial of a given number?

Complete and submit the above assignment within 15 Days. The Submitted Assignment is supposed to be Valid Completion only if it is submitted along with the pending/ incomplete questions from the previous assignments 1 & 2. ===========================END OF UNIT 3 =============================
Dept. of Computer Science VIGNAN Institute of Technology & Science Ravi Krishna

Vous aimerez peut-être aussi