Вы находитесь на странице: 1из 38

JOGINPALLY B. R.

ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UNIT-II

FUNCTIONS AND ARRAYS

SHORT QUESTIONS AND ANSWERS (2M OR 3M)


-----------------------------------------------------------------------

1) What is a function? Write advantages of function?

Function:
Function means, a large program can be divided into a series of individual related programs called
modules. These modules are called functions. A function is a program segment that carries out some specific
and well defined tasks.

Advantages of User Defined Functions :

1 : Reusability : The function write once and use many times is called Reusability.
2 : Modular Program Appraoch : A large program is divided into smaller sub programs . so that each sub
program performs a specific task. This approach makes the program development more manageable.
3 : Efficiency : means by avoiding redundant instructions the size of program can be reduced. Which increase
efficiency of program.
4 : Function sharing : A function can be shared by many programmers

2) Write types of functions?


The functions can be classified into two categories.
1 : Built in Functions (OR) Library Functions
2 : User-defined Functions.

1 : Built-in Functions (OR) Library Functions : The standard C library is a collection of various types of
functions which perform some standared and predefined tasks. These function which part of the C compiler that
have been written for general purpose are called library functions. They are alo called built-in functions.
Examples :

1 : The function sqrt() is used for to find squre root of a given number.
2 : The function scanf() is used to read data from the keyboard.
3 : The function printf() is used to print the result on the display.

Advantages :
1
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 : The programmer’s job is made easier because the functions are already available.
2 : The library functions can be used whenever required.

Disadvantages : The standared library functions are limited, programmes can not completely on these library
functions. The programmer has to write his own programs.

This disadvantages of standred library functions can be overcome by using user-defined function.

Ex : #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n=36,s;
clrscr();
s=sqrt(n);
printf(“Squre root of %f:”,s);
getch();
}
2 : User Defined Functions : These functions which are written by the programmer to do some specific tasks
are called User Defined Functions.

Advantages of User Defined Functions :

1 : Reusability : The function write once and use many times is called Reusability.
2 : Modular Program Appraoch : A large program is divided into smaller sub programs . so that each sub
program performs a specific task. This approach makes the program development more manageable.
3 : Efficiency : means by avoiding redundant instructions the size of program can be reduced. Which increase
efficiency of program.
4 : Function sharing : A function can be shared by many programmers.

3) How to Decleration of function?

2
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Declaration (Function Prototype) : Like variable, all functions in a C program must be declared,
before they are invoked. The declaration of each function should end with semicolon. Ths is called function
prototype or function declaration.

The function declaration/prototype tells the compiler about the return type, the name of the function
and type of parameters which will be called and defined later in the program.

Syntax : type function-name(parameter list);


type function-name();

Ex : int add(int a,int b);


int add();
Where

type : is the return type of function.

function-name : is the name of the function.


parameter-list : Parameter list declares the variables that will receive the data sent by the calling
program.

4) What is calling function? (OR) what is function call?


Function Call : The function can be called by simply using the function name.

Syntax : function-name();
function-name(parameters);
return value = function-name(parameters);

Ex : add(); // function without arguments


add(a,b); // function with arguments.
c=fun(a,b); // function with arguments and return values

5) What is function definition? (OR) what is user defined function? (OR)


Write syntax of user defined function?
Function Definition : The program module that is written to achieve a specific task is called function
definition.

Syntax : type function-name(parameter list) // function header.


{
3
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
declaration of variables;
body of function; // Function body
return statement;
}

Where
type : return type can be int ,float,double,void etc. This is the type of the value that the function is expected to
return. If the function is not returning any value, then we need to specify the return types as void.
function-name : is the name of the function.
parameter-list : Parameter list declares the variables that will receive the data sent by the calling program.

Example of User Defined Function :

#include<stdio.h>
void add(); //function declaration/prototype
void main()
{
add(); //Function call
}
void add() // function definition
{
int a,b,c;
printf(“Enter the values of a and b”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“%d”,c);
}

6) What are the function parameters?


PARAMETERS : parameters provides the data communication between the calling function and called
function.
They are two types of parametes
1 : Actual parameters.
2 : Formal parameters.

1 : Actual Parameters : These are the parameters transferred from the calling function (main program) to
the called function (function).

2 : Formal Parameters :These are the parameters transferred into the calling function (main program) from
the called function(function).

Ex : main()
{
4
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fun1( a , b ); //Calling function
}
fun1( x, y ) //called function
{
..... .
}
Where
a, b are the Actual Parameters
x, y are the Formal Parameters

Difference between Actual Parameters and Formal Parameters

Actual Parameters Formal Parameters


1 : Actual parameters are used in calling 1 : Formal parameters are used in the
function when a function is invoked. function header of a called function.
Ex : c=add(a,b); Ex : int add(int m,int n);
Here a,b are actual parameters. Here m,n are called formal parameters.
2 : Actual parameters can be constants, 2 : Formal parametes should be only
variables or expression. variable. Expression and constants are not
Ex : c=add(a,b) //variable allowed.
c=add(a+5,b); //expression. Ex : int add(int m,n); //CORRECT
c=add(10,20); //constants. int add(int m+n,int n) //WRONG
int add(int m,10); //WRONG
3 : Actual parameters sends values to the 3 : Formal parametes receive values from
formal parameters. the actual parametes.
Ex : c=add(4,5); Ex : int add(int m,int n);
Here m will have the value 4 and n will
have the value 5.
4 : Address of actual parameters can be sent 4 : if formal parameters contains address,
to formal parameters they should be declared as pointers.

7) What are the function variables? (OR)


What are the local and global variables?

FUNCTION VARIABLES : They are two types of variables.

1 : Local Variable.
2 : Global Variable.

1 : Local Variable : The local variables are defined within the body of the function or block. These
variables can acess by that function only, other functions can not acess these variables.

Ex : fun1(int a,int b)
{
int c,d; // Here c and d are the Local variable

5
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
2 : Global Variable : Global variables are defined outside the main() function and multible functions can
use these variables.

Ex : int m=5, n=10; //Global Variable


main()
{
int a,b;
}
Here the variables ‘m’ and ‘n’ are defined outside the main() function. Hence they are global variables.

8) What is return statement in function?

Return Statement : The return statement may or may not send back any values to the calling function(main
program).

Syntax : return; // does not return any value


or
return(exp); // the specified exp value to calling function.

A function can contain more than one return statements, when the return value can return values
based on certain condition.
If a function can return some values other than int type, then we must specify the data type to be
return.

9) Write categeory of functions ?


CATEGORY OF FUNCTIONS : A function, depending on whether arguments are present or not and whether
a value is returned or not. The following are the function prototypes.

1 : Functions with no Parameters and no Return Values.


2 : Functions with no Parameters and Return Values.
3 : Functions with Parameters and no Return Values.
4 : Functions with Parameters and Return Values.

10) What is call by value? (OR) what is pass by value?


Pass by value (OR) Call by value : When a function is called with actual parameters, the values of actual
parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the
6
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
values of the actual parameters are not changed. This way of passing parameters is called pass by value or call
by value.
Ex :
#include<stdio.h>
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(i,j);
printf("After swapping:%d%d\n",i,j);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}

11) What is call by reference? (OR) what is pass by reference?


Pass by reference (OR) Call by Reference : In pass by reference, a function is called with addresses of actual
parameters. In the function header, the formal parameters receive the addresses of actual parameters. Now the
formal parameters do not contain values, instead they contain addresses. Any variable if it contains an address,
it is called a pointer variable. Using pointer variables, the values of the actual parameters can be changed. This
way of passing parameters is called call by reference or pass by reference.
Ex : #include<stdio.h>
7
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(&i ,&j);
printf("After swapping:%d%d\n",i,j);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
12) what is the difference between call by value and call by reference?

Differnce between Call by value and Call by reference


Call by value Call by Reference
1 : When a function is called the values of 1 : When a function is called the address of
variables are passed variables are passed.
2 : Change of formal parameters in the 2 : The actual parameters are changed since
function will not affect the actual the formal parameters indirectly manipulate
parameters in the calling function. the actual parametes.
3 : Execution is slower since all the values 3 : Execution is faster since only address
have to be copied into formal parameters. are copied.

13) What is the macro function?

Macro Substitution : They are two types of macro substitution.


1 : Macro substitution without arguments.
8
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 : Macro substitution with arguments.
1 : Macro substitution without arguments : It is a process to substitute the constant or value in the place of an
identifier. It is possible to achieve this with the help of directive or macro definition statement #define.

Syntax : #define identifier constant or expression

Ex : #define PI 3.142
#define MAX_MARKS 100
#define MIN_MARKS 35

Macro Substitution with Arguments :

Syntax : #define identifier(var1,var2,va3,….varn)string

Where identifier is the name of macro function with the list of macro formal parameters
var1,var2,var3,…varn like the formal parameters in a function definition.

Ex : #define PROD(x) (x*x)

14) What is an Array? Write advantages of arrays?


An array is a collection of similar data items. All the data items of an array are stored in
continuous memory locations in RAM. The elements of an array are of same data type and each item can be
accessed using the same name.

Ex : consider an array of marks of 5 students.

The representation of a array.

array name marks[0] marks[1] marks[1] marks[3] marks[4]

80 90 45 99 100
1000 1002 1004 1006 1008

9
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To refer an item in the array, we specify the name of the array along with position of the item. The
position of the item must be written within squre brackets( [] ). The position of the item enclosed within squre
brackets is called “subscript” or “index”.

marks of 5 students are stored. The marks of each student can be accessed as

marks[0] i.e, 80 – represents marks of first student.


marks[1] i.e 90 – represents marks of second student.
marks[2] i.e 45 – represents marks of third student.
marks[3] i.e 99 – represents marks of fourth student.
marks[4] i.e 100 – represents marks of fifth student.

Using marks[0] through marks[4] we can acess the marks of 5 students.

Advantages of an Array :

1:
2:
3:

15) How to decleration of array with example?


Declaration of Array :

Syntax : data_type array_name[size];

Where
- data_type : can be a basic data type such as int,float , char etc.
- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.

Ex : int a[5];

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

80 90 45 99 100
1000 1002 1004 1006 1008
16) How to initialization of array with example?

10
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Initilization of Array : assigning the required information to a variable before processing is called
initialization.we can initialize the individual elements of an array. Array elements can be initialized at the time
of declaration.
Syntax : data_type array_name[size]={v1,v2,…vn};

Where
- data_type : can be a basic data type such as int,float , char etc.
- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.
- v1,v2,..vn are the values and should be enclosed within ‘{‘ and ‘}’ separated by commas. The value v1
is assigned to the 0th location, v2 is assigned to 1st location and so on.

Ex:
int a[5]={10,20,30,40,50};
char b[8] = {‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
int a[5]={10,15};
char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
char b[] = “COMPUTER”;

LONG QUESTIONS AND ANSWERS (10M)


--------------------------------------------------------

1) what is function? Write types of function with example?


Function:
Function means, a large program can be divided into a series of individual related programs called
modules. These modules are called functions. A function is a program segment that carries out some
specific and well defined tasks.

The functions can be classified into two categories.

1 : Built in Functions (OR) Library Functions


2 : User-defined Functions.

1 : Built-in Functions (OR) Library Functions : The standard C library is a collection of various types of
functions which perform some standared and predefined tasks. These function which part of the C compiler that
have been written for general purpose are called library functions. They are alo called built-in functions.
11
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Examples :

1 : The function sqrt() is used for to find squre root of a given number.
2 : The function scanf() is used to read data from the keyboard.
3 : The function printf() is used to print the result on the display.

Advantages :

1 : The programmer’s job is made easier because the functions are already available.
2 : The library functions can be used whenever required.

Disadvantages : The standared library functions are limited, programmes can not completely on these library
functions. The programmer has to write his own programs.

This disadvantages of standred library functions can be overcome by using user-defined function.

Ex : #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n=36,s;
clrscr();
s=sqrt(n);
printf(“Squre root of %f:”,s);
getch();
}
2 : User Defined Functions : These functions which are written by the programmer to do some specific tasks
are called User Defined Functions.

Advantages of User Defined Functions :

1 : Reusability : The function write once and use many times is called Reusability.
2 : Modular Program Appraoch : A large program is divided into smaller sub programs . so that each sub
program performs a specific task. This approach makes the program development more manageable.
3 : Efficiency : means by avoiding redundant instructions the size of program can be reduced. Which increase
efficiency of program.
4 : Function sharing : A function can be shared by many programmers.

12
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2) Explain category of functions with examples?
CATEGORY OF FUNCTIONS : A function, depending on whether arguments are present or not and
whether a value is returned or not. The following are the function prototypes.
1 : Functions with no Parameters and no Return Values.
2 : Functions with no Parameters and Return Values.
3 : Functions with Parameters and no Return Values.
4 : Functions with Parameters and Return Values.

1 : Functions with no Parameters and no Return Values :

1 : In this category, there is no data transfer between the calling function and called function.
2 : But there is flow of control from calling function to the called function.
3 : When no parameters are there , the function cannot receive any value from the calling function.
4: When the function does not return a value, the calling function cannot receive any value from the called
function.
Ex #include<stdio.h>
#include<conio.h>
void sum();
void main()
{
sum();
getch();
}
void sum()
{
int a,b,c;
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d",c);
}
2 : Functions with no Parameters and Return Values.

1 : In this category, there is no data transfer between the calling function and called function.
2 : But there is data transfer from called function to the calling function.
3 : When no parameters are there , the function cannot receive any values from the calling function.
4: When the function returns a value, the calling function receives one value from the called function.

Ex : #include<stdio.h>
13
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<conio.h>
int sum();
void main()
{
int c;
clrscr();
c=sum();
printf("sum=%d",c);
getch();
}
int sum()
{
int a,b,c;
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
c=a+b;
return c;
}

3 : Functions with Parameters and no Return Values.

1 : In this category, there is data transfer from the calling function to the called function using parameters.
2 : But there is no data transfer from called function to the calling function.
3 : When parameters are there , the function can receive any values from the calling function.
4: When the function does not return a value, the calling function cannot receive any value from the called
function.

Ex : #include<stdio.h>
#include<conio.h>
void sum(int a,int b);
void main()
{
int m,n;
clrscr();
printf("Enter m and n values:");
scanf("%d%d",&m,&n);
sum(m,n);
getch();
}
void sum(int a,int b)
{
int c;
c=a+b;
printf("sum=%d",c);
14
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}

4 : Functions with Parameters and Return Values.

1 : In this category, there is data transfer from the calling function to the called function using parameters.
2 : But there is no data transfer from called function to the calling function.
3 : When parameters are there , the function can receive any values from the calling function.
4: When the function returns a value, the calling function receive a value from the called function.

Ex :
#include<stdio.h>
#include<conio.h>
int sum(int a,int b);
void main()
{
int m,n,c;
clrscr();
printf("Enter m and n values");
scanf("%d%d",&m,&n);
c=sum(m,n);
printf("sum=%d",c);
getch();
}
int sum(int a,int b)
{
int c;
c=a+b;
return c;
}

3) What is call by value and call by reference?


PASSING PARAMETERS TO FUNCTIONS :
The called function receives the information from the calling function through the parameters. The
variables used while invoking the calling function are called actual parameters and the variables used in
the function header of the called function are called formal parameters.
C provides two mechanisms to pass parameters to a function.

1 : Pass by value (OR) Call by value.


2 : Pass by reference (OR) Call by Reference.

1 : Pass by value (OR) Call by value : When a function is called with actual parameters, the values of actual
parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the
15
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
values of the actual parameters are not changed. This way of passing parameters is called pass by value or call
by value.
Ex :
#include<stdio.h>
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(i,j);
printf("After swapping:%d%d\n",i,j);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
2 : Pass by reference (OR) Call by Reference :
In pass by reference, a function is called with addresses of actual parameters. In the function header, the
formal parameters receive the addresses of actual parameters. Now the formal parameters do not contain values,
instead they contain addresses. Any variable if it contains an address, it is called a pointer variable. Using
pointer variables, the values of the actual parameters can be changed. This way of passing parameters is called
call by reference or pass by reference.

Ex : #include<stdio.h>

16
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(&i ,&j);
printf("After swapping:%d%d\n",i,j);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

Differnce between Call by value and Call by reference


Call by value Call by Reference
1 : When a function is called the values of 1 : When a function is called the address of
variables are passed variables are passed.
2 : Change of formal parameters in the 2 : The actual parameters are changed since
function will not affect the actual the formal parameters indirectly manipulate
parameters in the calling function. the actual parametes.
3 : Execution is slower since all the values 3 : Execution is faster since only address
have to be copied into formal parameters. are copied.

4) Explain inter function communication with example?


INTER FUNCTION COMMUNICATION :

17
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The calling and called function are two separate entities, they need to communicate to exchange
data. The data flow between the calling function and called functions can be divided into 3 strategies.

1 : Downward Flow.(from the calling function to called function).


2 : Upward Flow. (From the called function to the calling function).
3 : Bi-direction Flow.(flow in both direction).

Calling function Calling function Calling function

Called Function Called Function Called Function

a)Downward b) Upward c)Bi-directional

1 : Downward Function : In downward communication, the calling function sends data to the called function.
In this strategy, the values of the calling function are copied into the called function. The called function may
changes the values passed, but the original values in the calling function can not be changed.
In Downward communication in C is a pefect solution for the Call by value (OR) Pass by value.
Downward communication is one-way communication. The calling function can send data to the
called function, but the called function cannto send any data to the calling function.

Ex : #include<stdio.h>
void downFun(int x,int y);
void main()
{
int a=5;
downFun(a,15);
printf(“%d”,a);
}
void downFun(int x,int y)
{
x=x+y;
}

2 : Upward Function : Upward communication, when the called function sends data back to the calling
function without receiving any data from it.

18
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
In Upward communication in C is a pefect solution for the Call by reference (OR) Pass by
reference.
Ex : #include<stdio.h>
void upFun(int *m,int *n);
void main()
{
int a=10,b=20;
upFun(&a,&b);
printf(“%d%d”,a,b);
}
void upFun(int *m,int *n)
{
*m=40;
*n=60;
}

3 : Bi-Directional Flow : Bi-directional flow in both directions

Ex : #include<stdio.h>
void upFun(int *m,int *n);
void main()
{
int a=10,b=20;
upFun(&a,&b);
printf(“%d%d”,a,b);
}
void upFun(int *m,int *n)
{
*m=*m+40;
*n=*n / *m;
}

19
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5) Explain storage classes with examples?

STORAGE CLASSES

Storage : Any variable declared in a program can be stored either in memory or registers. Registers are small
amount of storage in CPU. The data stored in registers has fast acess compared to data stored in memory.
Storage class of a variable gives information about the location of the variable in which it is
stored; initial value of the variable, if storage class is not specified; scope of the variable; life of the variable.

Storage Classes in C Divided into Four Types


1 : Automatic Storage class.
2 : Register Storage class.
3 : Static Storage class.
4 : External Storage class.
1 : Automatic Storage class : To define a variable as automatic storage class, the keyword auto is used.By
definig a variable as automatic storage class, it is stored in the memory. The default value of the variable will be
garbage value. Scope of the variable is within the block where it is defined and the life of the variable is until
the control remains within the block.

Syntax : auto data_type variable_name;


auto int a,b;
The variables a and b are declared as integer type and auto. The keyword auto is not mandatory.
Because the default storage class in C is auto.

Ex : void function1();
void function2(); OUTPUT
void main() 10
{ 0
int x=100; 100
function2();
printf(“%d”,x);
}
void function1()
{
int x=10;
printf(“%d”,x);

20
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
void function2()
{
int x=0;
function1();
printf(“%d”,x);
}

2 :Register Srorage class : To define a variable as register storage class, the keyword register is used. If CPU
cannot store the variables in CPU registers, then the variables are assumed as auto and stored in the memory.
When a variable is declared as register, it is stored in the CPU registers. The default value of the variable will be
garbage value. Scope of the variable within the block where it is defined and the life of the variables is until the
control remains within the block.

Sytax : register data_type variable_name;


register int i;

Ex : void demo(); OUTPUT


void main() 20
{ 20
demo(); 20
demo();
demo();
}
void demo()
{
register int i=20;
printf(“%d”,i);
i++;
}

3 : Static Storage class : When a variable is declared as static, it is stored in the memory. The default value of
the variable will be zero. Scope of the variable is within the block where it is defined and the life of the variable
persists between different function calles.To define a variable as static storage class, the keyword static is used.
A static variable can be initialized only once, it cannot be reinitialized.
Syntax : static data_type variable_name;
static int i;

21
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ex : void demo(); OUTPUT
void main() 20
{ 21
demo(); 22
demo();
demo();
}
void demo()
{
static int i=20;
printf(“%d”,i);
i++;
}

Here A static variable is initialized only once. When the program is compiled. It is never initialized again.

4 : External Storage class : When a varaiable is declared as extern, it is stored in the memory. The default
value is initialized to zero. The scope of the variable is global and the life of the variable is until the program
execution comes to an end. To define a variable as external storage class, the keyword extern is used. An extern
variable is also called as a global variable.

Systax : extern data_type variable_name;


extern int i;
Ex : void fun1();
void fun2();
int e=20;
void main()
{
fun1();
fun2();
}
void fun1()
{
extern int e;
printf(“e number is :%d”,e);
}
void fun1()
{
printf(“e number is :%d”,e);
}

22
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SCOPE RULES :
Storage Keyword Storage Location LifeTime Initial Value
Class
Automatic Auto Memory Until the end of Garbage value
function or Block
Register register CPU Registers Until the end of Garbage value
function or Block
Static Static Memory Zero
External Extern Memory Zero

6) Write pre-processor commands?

PRE-PROCESSOR COMMANDS

Any program execution needs certain steps. They are 1) the C program is written in the editor 2)
compilation 3) linking 4) executable code is generated. But in between these stages there also involves one
more stage. i.e preprocessor. The preprocessor are used to programmer supplied to command to perform a
source program to the compilation.
One of the most important features of C language is preprocessor commands. The preprocessor
commands are always initialized at the beginning of the program. It begins with a symbol # (pound sign). It
can be placed anywhere but it is declared at the beginning before the main() function or any particular function.

There are three types of preprocessor commands.


1 : macro substitution.
2 : file inclusion.
3 : conditional compilation directives.

1: Macro Substitution : They are two types of macro substitution.


1 : Macro substitution without arguments.
2 : Macro substitution with arguments.

23
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 : Macro substitution without arguments : It is a process to substitute the constant or value in the place of an
identifier. It is possible to achieve this with the help of directive or macro definition statement #define.

Syntax : #define identifier constant or expression

Ex : #define PI 3.142
#define MAX_MARKS 100
#define MIN_MARKS 35

Ex : Example of Macro substitution

#include<stdio.h>
#define PI 3.142
void main()
{
int r;
float area;
printf(“Enter the radius of circle”);
scanf(“%d”,&r);
area=PI*r*r;
printf(“the area of a circle is%d”,area);
}

Example of Macro definition with expressions

#define A (20*10)
#define B (200-100)
void main()
{
int div;
div=A/B;
printf(“the division of two numbers%d”,div);
}

Example of Macro definition with conditional expression

#define IFCONDITION if(a>b)


#define PRINT printf(“the value of a is the greatest no”)
void main()
{
int a=100,b=50;
IFCONDITION
PRINT;

24
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}

Macro Substitution with Arguments :

Syntax : #define identifier(var1,var2,va3,….varn)string

Where identifier is the name of macro function with the list of macro formal parameters
var1,var2,var3,…varn like the formal parameters in a function definition.

Ex : #define PROD(x) (x*x)


void main()
{
int a,mul;
printf(“enter the value of a”);
scanf(“%d”,&a);
mul=PROD(a);
printf(“The multification of two numbers%d”,mul);
}

Nesting of Macros : it is also possible to use one macro in the definition of another macro. This is called as
nesting of macros.

Ex : #define SQR(x) (x*x)


#define CUBE(x) (SQR(x)*x)
void main()
{
int p,squre,volume;
printf(“Enter the value of p”);
scanf(“%d”,&p);
squre=SQR(x);
cube=CUBE(x);
printf(“the squre of p%d”,squre);
printf(“the cube of p%d”,cube);
}
2 : FILE INCLUSION : A copying of one file to another files into program.

Ex : File inclusion of an external file “add.c”.

#include<stdio.h>
#include add.c
void main()
{
void add(); //FUNCTION PROTOTYPE/ DECLARATION.
add(); //FUNCTION CALLING
}

25
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The file add1.c contains the function definition as follows.

void add()
{
int a,b,c;
printf(“enter two numbers”);
scanf(%d%d”,&a,&b);
c=a+b;
printf(“c value is:%d”,c);
}

3 : CONDITIONAL COMPILATION DIRECTIVES : C preprocessor also supports number of conditional


compilation directives as

1 : #undef : Undefined a macro


2 : #ifdef : Tests for a macro definition.
3 : #endif : Specifies the end of #if.
4 : #if : Tests compile-time condition.
5 : #else : Specifies alternative when #if test fails.

These are used to select a particular segment of code for compilation depending on the condition.

Ex : #define TEST 1
void main()
{
#ifdef TEST
{
printf(“This is compiled”);
}
#else
{
printf(“This is not compiled”);
}
#endif
}

Ex2 : : #define FLAG 1


char ch;
void main()
{
#if FLAG
{
ch=’t’;
printf(“This is compiled”);
}

26
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#else
{
ch=’f’;
printf(“This is not compiled”);
}
#endif
}

7) What is an array? How to decleration, initialization and accessing of an array?

Array: An array is a collection of similar data items. All the data items of an array are stored in
continuous memory locations in RAM. The elements of an array are of same data type and each item
can be accessed using the same name.

Ex : consider an array of marks of 5 students.

The representation of a array.

array name marks[0] marks[1] marks[1] marks[3] marks[4]

80 90 45 99 100
1000 1002 1004 1006 1008

To refer an item in the array, we specify the name of the array along with position of the item. The
position of the item must be written within squre brackets( [] ). The position of the item enclosed within squre
brackets is called “subscript” or “index”.

marks of 5 students are stored. The marks of each student can be accessed as

marks[0] i.e, 80 – represents marks of first student.


marks[1] i.e 90 – represents marks of second student.
marks[2] i.e 45 – represents marks of third student.
marks[3] i.e 99 – represents marks of fourth student.
marks[4] i.e 100 – represents marks of fifth student.

Using marks[0] through marks[4] we can acess the marks of 5 students.

27
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Advantages of an Array :

1. It is used to represent multiple data items of same type by using only single name.
2. It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs etc.
3. 2D arrays are used to represent matrices.

Declaration of Array :

Syntax : data_type array_name[size];

Where

- data_type : can be a basic data type such as int,float , char etc.


- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.

Ex : int a[5];

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

80 90 45 99 100
1000 1002 1004 1006 1008

Formula for finding size of array

Size of array = n*sizeof(data_type)

Initilization of Array : assigning the required information to a variable before processing is called
initialization.we can initialize the individual elements of an array. Array elements can be initialized at the time
of declaration.

Syntax : data_type array_name[size]={v1,v2,…vn};

Where

- data_type : can be a basic data type such as int,float , char etc.


- array_name : is the name of the array.
28
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.
- v1,v2,..vn are the values and should be enclosed within ‘{‘ and ‘}’ separated by commas. The value v1
is assigned to the 0th location, v2 is assigned to 1st location and so on.

Different ways of initializing arrays :

1 : Initilizing all specified memory locations


2 : Partial array initialization.
3 : Intilization without size.
4 : String initialization.

1 : Initilizing all specified memory locations : If the number of values to be initialized is equal to size of array.
Arrays can be initialized at the time of declaration. Array elements can be initialized with data items of type
int,float,char, etc.

Ex : consider integer initialization

int a[5]={10,20,30,40,50};

During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and
all these locations are initialized.

The array a is initialized as

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

10 20 30 40 50
1000 1002 1004 1006 1008

If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.

Ex : consider character initialization

char b[8] = {‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};

The array b is initialized as

b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

29
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C O M P U T E R
Other Examples : char b[5]={‘J’,’B’,’R’,’E’,’C’,’B’};
//error : number of initial values are more than the size of array.

Other Example : int a[5]={10,20,30,40,50,60};

//error : Number of initial values are more than the size of array.

2 : Partial Array Initilization : partial array initialization is possible in C language. If the number of values to
be initialized is less than the size of the array, then the elements are initialized in the order from 0th location.
The remaining locations will be initialized to zero automatically.

Ex : Consider the partial initilization

int a[5]={10,15};

Eventhough compiler allocates 5 memory locations, using this declaration statement, the
compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically
initialized to zero.

The array a is partial initialization as

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

10 15 0 0 0
1000 1002 1004 1006 1008

3 : Initilization without size : consider the declaration along with the initialization

char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};

In this declaration, eventhough we have not specified exact number of elements to be used in
array b, the array size will be set of the total number of initial values specified. So, the array size will be set to 8
automatically.
The array b is initialized as

b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

30
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C O M P U T E R

4) Array Initilization with a String : consider the declaration with string initialization.

char b[] = “COMPUTER”;


The array b is initialized as
b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

C O M P U T E R

The string “COMPUTER” contin 8 charactes, because it is a string. It always ends with null character. So,
thearray is 9 bytes (i.e string length+1 byte for null character).

accessing of an array:
accessing of array values on the display screen.

Syntax: for(i=0;i<n;i++)
printf(“control string”, array_var[i]);

8) write types of arrays with examples?

C language supports three types of arrays.

1 : Single Dimentional Array


2 : Double Dimentional Array.
3 : Multiple Dimentional Array.

1 : Single Dimentional Array : is also called one-dimentional array) is a linear list consisting of related and
similar data items. In memory all the data items are stored in continuous memory locations one ofter the other.

Declaration of Single-Dimentional Arrays :

Syntax : data_type array_name[size];

31
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Where

- data_type : can be a basic data type such as int,float , char etc.


- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.

Ex : int a[5];

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

80 90 45 99 100
1000 1002 1004 1006 1008

Formula for finding size of array

Size of array = n*sizeof(data_type)

Initilization of Single-Dimentional Arrays : assigning the required information to a variable before processing
is called initialization.we can initialize the individual elements of an array. Array elements can be initialized at
the time of declaration.

Syntax : data_type array_name[size]={v1,v2,…vn};

Where

- data_type : can be a basic data type such as int,float , char etc.


- array_name : is the name of the array.
- Size : size should be evaluated to a positive integer only. It can also be an integer constants.
- v1,v2,..vn are the values and should be enclosed within ‘{‘ and ‘}’ separated by commas. The value v1
is assigned to the 0th location, v2 is assigned to 1st location and so on.

Different ways of initializing arrays :

1 : Initilizing all specified memory locations


2 : Partial array initialization.

32
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 : Intilization without size.
4 : String initialization.

1 : Initilizing all specified memory locations : If the number of values to be initialized is equal to size of array.
Arrays can be initialized at the time of declaration. Array elements can be initialized with data items of type
int,float,char, etc.

Ex : consider integer initialization

int a[5]={10,20,30,40,50};

During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and
all these locations are initialized.

The array a is initialized as

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

10 20 30 40 50
1000 1002 1004 1006 1008

If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.

Ex : consider character initialization

char b[8] = {‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};

The array b is initialized as

b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

C O M P U T E R
Other Examples : char b[5]={‘J’,’B’,’R’,’E’,’C’,’B’};
//error : number of initial values are more than the size of array.

Other Example : int a[5]={10,20,30,40,50,60};

//error : Number of initial values are more than the size of array.

33
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 : Partial Array Initilization : partial array initialization is possible in C language. If the number of values to
be initialized is less than the size of the array, then the elements are initialized in the order from 0th location.
The remaining locations will be initialized to zero automatically.

Ex : Consider the partial initilization

int a[5]={10,15};

Eventhough compiler allocates 5 memory locations, using this declaration statement, the
compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically
initialized to zero.

The array a is partial initialization as

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

10 15 0 0 0
1000 1002 1004 1006 1008

3 : Initilization without size : consider the declaration along with the initialization

char b[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};

In this declaration, eventhough we have not specified exact number of elements to be used in
array b, the array size will be set of the total number of initial values specified. So, the array size will be set to 8
automatically.
The array b is initialized as

b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

C O M P U T E R

34
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4) Array Initilization with a String : consider the declaration with string initialization.

char b[] = “COMPUTER”;


The array b is initialized as
b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

C O M P U T E R

The string “COMPUTER” contin 8 charactes, because it is a string. It always ends with null character. So,
thearray is 9 bytes (i.e string length+1 byte for null character).

Two Dimensional Array


When the data must be stored in the form of a matrix we can use two-dimensional arrays.

Example : int a[5][3];

Represents a two-dimensional array consisting of 5 rows and 3 coloums. So the total number of elements
which can be stored in this array are 5*3 i.e, 15.
Declaration of Two-Dimentioal Array :
Syntax : type array_name[row-size][column_size];
Where,
1 : type : represents the data type of array elements.
2 : array_name : is name of the array.
3 : row_size : represents the number of rows in array.
4 : column_size : represents number of coloumns of the array.
Both row-size and coloumn_size must be integer constants.
Examples :
1 : int xyz[2][5];
Here, xyz is an array consisting of 2 rows with each row consisting of5 integer constants.
2 : float a[5][5];
Here, a is an array consisting of 5 rows with each row consisting of 5 floating point numbers.

35
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Initilization of Two-Dimensional Arrays :


Two-Dimensional arrays can also be initialized at the place of declaration itself.
Syntax : type array-name[row_size][coloumn_size]= { {list of elements in row1},
{ list of elements in row 2},
:
:
{list of elements in row n}};
Example : int xyz[2][3]={{2,3,4},{5,6,7}};
Here 2,3,4 are assigned to three coloumns of first row and 5,6,7 are assigned to three coloumns of second row in
order.
If the values are missing in initialization, they are set to garbage values
Ex : int xyz[2][3]={{0},{0}};

Here , first element of each row is initialized to zero while other elements have some garbase values.

9) Write a C program addition of two matrices?

#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n;
clrscr();
printf("enter the rows and colums of two matrics:\n");
scanf("%d%d",&m,&n);
printf("\nenter the elements of A matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\nenter the elements of B matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\nThe elements of A matrics");
36
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe elements of B matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe additon of two matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
}
getch();
}

10) Write a C program multiplication of two matrices?

// Write a C program Multiplication of Two Matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n,k,r,s;
clrscr();
printf("enter the rows and colums of A matrics:\n");
scanf("%d%d",&m,&n);
printf("enter the rows and colums of B matrics:\n");
scanf("%d%d",&r,&s);
printf("\nenter the elements of A matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);

37
JOGINPALLY B. R. ENGINEERING COLLEGE
B.Tech-I Year COMPUTER PROGRAMMING NOTES
D.Murali Mohan K. Praveen Kumar P.Sandya A. Raju
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
printf("\nenter the elements of B matrics");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\nThe elements of A matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe elements of B matrics");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}

for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}

printf("\nThe Multiplication of two matrics");


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

38

Вам также может понравиться