Government Polytechnic Muzaffarpur.

Similar documents
Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

'C' Programming Language

Prepared by: Shraddha Modi

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Unit 3 Decision making, Looping and Arrays

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Module 6: Array in C

How to declare an array in C?

DECISION CONTROL AND LOOPING STATEMENTS

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Computer Science & Engineering 150A Problem Solving Using Computers

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Decision Making and Loops

ARRAYS(II Unit Part II)

Introduction. C provides two styles of flow control:

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Write a C program using arrays and structure

Arrays in C. By Mrs. Manisha Kuveskar.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

M1-R4: Programing and Problem Solving using C (JAN 2019)

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Class Test 2. Room Sections No of students

Fundamental of Programming (C)

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Fundamentals of Computer Programming Using C

UIC. C Programming Primer. Bharathidasan University

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Principles of Programming. Chapter 6: Arrays

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

C - Basics, Bitwise Operator. Zhaoguo Wang

Decision Making -Branching. Class Incharge: S. Sasirekha

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.


Questions Bank. 14) State any four advantages of using flow-chart

C library = Header files + Reserved words + main method

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

WAP 10. WAP 11. WAP 12. WAP 13. WAP 14. WAP 15. WAP 16. WAP 1. : 17. WAP 18. WAP 19. WAP 20. WAP 21. WAP 22. WAP 23. WAP & 24. WAP

IV Unit Second Part STRUCTURES

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Programming & Data Structure Laboratory. Day 2, July 24, 2014

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Subject: Fundamental of Computer Programming 2068

Module 4: Decision-making and forming loops

Q 1. Attempt any TEN of the following:

Fundamental of Programming (C)

Classification s of Data Structures

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Subject: PIC Chapter 2.

Computer Programming Unit 3

MODULE 3: Arrays, Functions and Strings

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Lesson 7. Reading and Writing a.k.a. Input and Output

Computer Programming: C++

Principles of C and Memory Management

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Dept. of CSE, IIT KGP

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Functions. (transfer of parameters, returned values, recursion, function pointers).

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Programming and Data Structures

Lecture 6. Statements

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Course Outline Introduction to C-Programming

Vidyalankar F.Y. Diploma : Sem. II [CD/CM/CO/CW/DE/ED/EE/EI/EJ/EN/EP/ET/EV/EX/IC/IE/IF/IS/IU/MU] Programming in C

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

UNIT III ARRAYS AND STRINGS

UNIT - V STRUCTURES AND UNIONS

COMP 208 Computers in Engineering

C: How to Program. Week /Mar/05

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..


from Appendix B: Some C Essentials

Chapter 8: Function. In this chapter, you will learn about

Computers Programming Course 7. Iulian Năstac

Computers Programming Course 12. Iulian Năstac

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Multi-Dimensional arrays

Transcription:

Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking for C programs, please click here C programs. This C programming basics section explains a simple Hello World C program. Also, it covers below basic topics as well, which are to be known by any C programmer before writing a C program. 1. C programming basic commands to write a C program 2. A simple C program with output and explanation 3. Steps to write C programs and get the output 4. Creation, Compilation and Execution of a C program * How to install C compiler and IDE tool to run C programming codes 5. Basic structure of a C program * Example C program to compare all the sections * Description for each section of the C program 1. C PROGRAMMING BASICS TO WRITE A C PROGRAM: Below are few commands and syntax used in C programming to write a simple C program. Let s see all the sections of a simple C program line by line.

C Basic commands #include <stdio.h> int main() /*_some_comments_*/ printf( Hello_World! ); getch(); return 0; Explanation This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program This is the main function from where execution of any C program begins. This indicates the beginning of the main function. whatever is given inside the command /* */ in any C program, won t be considered for compilation and execution. printf command prints the output onto the screen. This command waits for any character input from keyboard. This command terminates C program (main function) and returns 0. This indicates the end of the main function. 2. A SIMPLE C PROGRAM: Below C program is a very simple and basic program in C programming language. This C program displays Hello World! in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator. #include <stdio.h> int main()

/* Our first simple C basic program */ printf("hello World! "); getch(); return 0; OUTPUT: Hello World! 3. STEPS TO WRITE C PROGRAMS AND GET THE OUTPUT: Below are the steps to be followed for any C program to create and get the output. This is common to all C program and there is no exception whether its a very small C program or very large C program. 1. Create 2. Compile 3. Execute or Run 4. Get the Output 4. CREATION, COMPILATION AND EXECUTION OF A C PROGRAM:

Experiment: 2 Aim: Use of Sequential structure sequence of statements are written in order to accomplish a specific activity. So statements are executed in the order they are specified in the program. This way of executing statements sequentially is known as Sequential control statements. There is an advantage that is no separate control statements are needed in order to execute the statements one after the other. include<stdio.h> main() int arr[50],k,i,l,pos=0; printf( Enter limit For SEQUENTIAL SEARCH\n ); scanf( %d,&l); printf( Enter %d elements\n,l); for(i=0;i<l;i++) scanf( %d,&arr[i]); printf( Enter a number to be search\n ); scanf( %d,&k); for(i=0;i<l;i++) if(arr[i]==k) pos=i+1; break; if(pos!=0) printf( %d is found in the list, at position %d\n,k,pos);

else printf( %d is not in the list\n,k); OUTPUT: Enter limit For SEQUENTIAL SEARCH 10 Enter 10 elements 11 23 58 31 56 77 43 12 65 19 Enter a number to be search 31 31 is found in the list, at position 4

Experiment: 3 Aim: Use of if-else statements In decision control statements (if-else and nested if), group of statements are executed when condition is true. If condition is false, then else part statements are executed. There are 3 types of decision making control statements in C language. They are, 1. if statements 2. if else statements 3. nested if statements IF, ELSE AND NESTED IF DECISION CONTROL STATEMENTS IN C: Syntax for each C decision control statements are given in below table with description. Decision control statements if if else Syntax/Description Syntax: if (condition) Statements; Description: In these type of statements, if condition is true, then respective block of code is executed. Syntax: if (condition) Statement1; Statement2; else Statement3; Statement4; Description: In these type of statements, group of statements are executed when condition is true. If condition is false, then else part statements are executed.

nested if Syntax: if (condition1) Statement1; else_if(condition2) Statement2; else Statement 3; Description: If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed. EXAMPLE PROGRAM FOR IF STATEMENT IN C: In if control statement, respective block of code is executed when condition is true. int main() int m=40,n=40; if (m == n) printf("m and n are equal"); OUTPUT: m and n are equal EXAMPLE PROGRAM FOR IF ELSE STATEMENT IN C: In C if else control statement, group of statements are executed when condition is true. If condition is false, then else part statements are executed. #include <stdio.h> int main() int m=40,n=20;

if (m == n) printf("m and n are equal"); else printf("m and n are not equal"); OUTPUT: m and n are not equal EXAMPLE PROGRAM FOR NESTED IF STATEMENT IN C: In nested if control statement, if condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed. #include <stdio.h> int main() int m=40,n=20; if (m>n) printf("m is greater than n"); else if(m<n) printf("m is less than n"); else printf("m is equal to n"); OUTPUT: m is greater than n

Experiment: 4 Aim: Use of for statement. Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false. TYPES OF LOOP CONTROL STATEMENTS IN C: There are 3 types of loop control statements in C language. They are, 1. for 2. while 3. do-while Syntax for each C loop control statements are given in below table with description. Loop Name for Syntax for (exp1; exp2; expr3) statements; Where, exp1 variable initialization ( Example: i=0, j=2, k=3 ) exp2 condition checking ( Example: i>5, j<3, k=3 ) exp3 increment/decrement ( Example: ++i, j, ++k ) EXAMPLE PROGRAM (FOR LOOP) IN C: In for loop control statement, loop is executed until condition becomes false. #include <stdio.h> int main() int i; for(i=0;i<10;i++)

printf("%d ",i); OUTPUT: 0 1 2 3 4 5 6 7 8 9

Experiment:5 Aim: Use of Do-While Statement EXAMPLE PROGRAM (DO WHILE LOOP) IN C: In do..while loop control statement, while loop is executed irrespective of the condition for first time. Then 2 nd time onwards, loop is executed until condition becomes false. do while do statements; while (condition); where, condition might be a>5, i<10 #include <stdio.h> int main() int i=1; do printf("value of i is %d\n",i); i++; while(i<=4 && i>=2); OUTPUT: Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4

Experiment: 6 Aim: Use of While statement EXAMPLE PROGRAM (WHILE LOOP) IN C: In while loop control statement, loop is executed until condition becomes false. while Loop is executed only when condition is true. do while Loop is executed for first time irrespective of the condition. After executing while loop for first time, then c #include <stdio.h> int main() int i=3; while(i<10) printf("%d\n",i); i++; OUTPUT: 3 4 5 6 7 8 9

Experiment: 7 Aim: Use of brake and Continue statement. The statements which are used to execute only specific block of statements in a series of blocks are called case control statements. There are 4 types of case control statements in C language. They are, 1. switch 2. break 3. continue BREAK STATEMENT IN C: Break statement is used to terminate the while loops, switch case loops and for loops from the subsequent execution. Syntax: break; EXAMPLE PROGRAM FOR BREAK STATEMENT IN C: #include <stdio.h> int main() int i; for(i=0;i<10;i++) if(i==5) printf("\ncoming out of for loop when i = 5"); break; printf("%d ",i); OUTPUT:

0 1 2 3 4 Coming out of for loop when i = 5 CONTINUE STATEMENT IN C: Continue statement is used to continue the next iteration of for loop, while loop and dowhile loops. So, the remaining statements are skipped within the loop for that particular iteration. Syntax : continue; EXAMPLE PROGRAM FOR CONTINUE STATEMENT IN C: #include <stdio.h> int main() int i; for(i=0;i<10;i++) if(i==5 i==6) printf("\nskipping %d from display using " \ "continue statement \n",i); continue; printf("%d ",i); OUTPUT: 0 1 2 3 4 Skipping 5 from display using continue statement Skipping 6 from display using continue statement 7 8 9

Experiment: 8 Aim: Use of multiple branching Switch statement. EXAMPLE PROGRAM FOR SWITCH..CASE STATEMENT IN C: 1. switch case statement in c: switch case statements are used to execute only specific case statements based on the switch expression. below is the syntax for switch case statement. switch (expression) case label1: statements; break; case label2: statements; break; case label3: statements; break; default: statements; break; #include <stdio.h> int main () int value = 3; switch(value) case 1: printf( Value is 1 \n ); break;

case 2: printf( Value is 2 \n ); break; case 3: printf( Value is 3 \n ); break; case 4: printf( Value is 4 \n ); break; default : printf( Value is other than 1,2,3,4 \n ); return 0; Output: Value is 3

Experiment: 9 Aim: Use of different format specifiers using Scanf( ) and Printf( ) C PROGRAMMING BASICS TO WRITE A C PROGRAM: Below are few commands and syntax used in C programming to write a simple C program. Let s see all the sections of a simple C program line by line. C Basic commands #include <stdio.h> int main() /*_some_comments_*/ printf( Hello_World! ); getch(); return 0; Explanation This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program This is the main function from where execution of any C program begins. This indicates the beginning of the main function. whatever is given inside the command /* */ in any C program, won t be considered for compilation and execution. printf command prints the output onto the screen. This command waits for any character input from keyboard. This command terminates C program (main function) and returns 0. This indicates the end of the main function.

2. A SIMPLE C PROGRAM: Below C program is a very simple and basic program in C programming language. This C program displays Hello World! in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator. #include <stdio.h> int main() /* Our first simple C basic program */ printf("hello World! "); getch(); return 0; OUTPUT: Hello World! Scanf(): The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use. 5. BASIC STRUCTURE OF A C PROGRAM: Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below. 1. Documentation section 2. Link Section 3. Definition Section 4. Global declaration section 5. Function prototype declaration section 6. Main function 7. User defined function definition section

EXAMPLE C PROGRAM TO COMPARE ALL THE SECTIONS: You can compare all the sections of a C program with the below C program. /* Documentation section C programming basics & structure of C programs */ #include <stdio.h> /* Link section */ int total = 0; /* Global declaration, definition section */ int sum (int, int); /* Function declaration section */ int main () /* Main function */ printf ("This is a C basic program \n"); total = sum (1, 1); printf ("Sum of two numbers : %d \n", total); return 0; int sum (int a, int b) /* User defined function */ return a + b; /* definition section */ OUTPUT: This is a C basic program Sum of two numbers : 2 DESCRIPTION FOR EACH SECTION OF THE C PROGRAM: Let us see about each section of a C basic program in detail below. Please note that a C program mayn t have all below mentioned sections except main function and link sections.

Also, a C program structure mayn t be in below mentioned order. Sections Documentation section Link Section Definition Section Global declaration section Function prototype declaration section Main function User defined function section Description We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between /* and */, won t be considered by C compiler for compilation process.these will be ignored by C compiler during compilation. Example : /* comment line1 comment line2 comment 3 */ Header files that are required to execute a C program are included in this section In this section, variables are defined and values are set to these variables. Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section. Function prototype gives many information about a function like return type, parameter names used inside the function. Every C program is started from main function and this function contains two major sections called declaration section and executable section. User can define their own functions in this section which perform particular task as per the user requirement.

Experiment: 10 Aim: Use of one dimensional array e.g. String, finding standard deviation of a group data. An array is a collection of data that holds fixed number of values of same type. For example: if you want to store marks of 100 students, you can create an array for it. float marks[100]; The size and type of arrays cannot be changed after its declaration. Arrays are of two types: One-dimensional arrays Multidimensional arrays How to declare an array in C? data_type array_name[array_size]; For example, float mark[5]; Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold 5 floating-point values. Elements of an Array and How to access them? You can access elements of an array by indices. Suppose you declared an array mark as above. The first element is mark[0], second element is mark[1] and so on. C Array declaration

Few key notes: Arrays have 0 as the first index not 1. In this example, mark[0] If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4] Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes. How to initialize an array in C programming? It's possible to initialize an array during declaration. For example, int mark[5] = 19, 10, 8, 17, 9; Another method to initialize array during declaration: int mark[] = 19, 10, 8, 17, 9; Initialize an array in C programming Here, mark[0] is equal to 19 mark[1] is equal to 10 mark[2] is equal to 8 mark[3] is equal to 17 mark[4] is equal to 9 How to insert and print array elements? int mark[5] = 19, 10, 8, 17, 9

// insert different value to third element mark[3] = 9; // take input from the user and insert in third element scanf("%d", &mark[2]); // take input from the user and insert in (i+1)th element scanf("%d", &mark[i]); // print first element of an array printf("%d", mark[0]); // print ith element of an array printf("%d", mark[i-1]); Example: C Arrays // Program to find the average of n (n < 10) numbers using arrays #include <stdio.h> int main() int marks[10], i, n, sum = 0, average; printf("enter n: "); scanf("%d", &n); for(i=0; i<n; ++i) printf("enter number%d: ",i+1); scanf("%d", &marks[i]); sum += marks[i]; average = sum/n; printf("average = %d", average);

return 0; Output Enter n: 5 Enter number1: 45 Enter number2: 35 Enter number3: 38 Enter number4: 31 Enter number5: 49 Average = 39

Experiment: 11 Aim: Use of two dimensional array of integers/ reals. In C programming, you can create an array of arrays known as multidimensional array. For example, float x[3][4]; Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as table with 3 row and each row has 4 column. Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3]; Here,The array y can hold 24 elements. You can think this example as: Each 2 elements have 4 elements, which makes 8 elements and each 8 elements can have 3 elements. Hence, the total number of elements is 24. How to initialize a multidimensional array? There is more than one way to initialize a multidimensional array.

Initialization of a two dimensional array // Different ways to initialize two dimensional array int c[2][3] = 1, 3, 0, -1, 5, 9; int c[][3] = 1, 3, 0, -1, 5, 9; int c[2][3] = 1, 3, 0, -1, 5, 9; Above code are three different ways to initialize a two dimensional arrays. Initialization of a three dimensional array. You can initialize a three dimensional array in a similar way like a two dimensional array. Here's an example, int test[2][3][4] = 3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 3, 1, 4, 9 ; Example #1: Two Dimensional Array to store and display values // C program to store temperature of two cities for a week and display it. #include <stdio.h> const int CITY = 2; const int WEEK = 7; int main() int temperature[city][week]; for (int i = 0; i < CITY; ++i) for(int j = 0; j < WEEK; ++j) printf("city %d, Day %d: ", i+1, j+1);

scanf("%d", &temperature[i][j]); printf("\ndisplaying values: \n\n"); for (int i = 0; i < CITY; ++i) for(int j = 0; j < WEEK; ++j) printf("city %d, Day %d = %d\n", i+1, j+1, temperature[i][j]); return 0; Output City 1, Day 1: 33 City 1, Day 2: 34 City 1, Day 3: 35 City 1, Day 4: 33 City 1, Day 5: 32 City 1, Day 6: 31 City 1, Day 7: 30 City 2, Day 1: 23 City 2, Day 2: 22 City 2, Day 3: 21 City 2, Day 4: 24 City 2, Day 5: 22 City 2, Day 6: 25 City 2, Day 7: 26 Displaying values: City 1, Day 1 = 33

City 1, Day 2 = 34 City 1, Day 3 = 35 City 1, Day 4 = 33 City 1, Day 5 = 32 City 1, Day 6 = 31 City 1, Day 7 = 30 City 2, Day 1 = 23 City 2, Day 2 = 22 City 2, Day 3 = 21 City 2, Day 4 = 24 City 2, Day 5 = 22 City 2, Day 6 = 25 City 2, Day 7 = 26 Example #2: Sum of two matrices using Two dimensional arrays C program to find the sum of two matrices of order 2*2 using multidimensional arrays. #include <stdio.h> int main() float a[2][2], b[2][2], c[2][2]; int i, j; // Taking input using nested for loop printf("enter elements of 1st matrix\n"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) printf("enter a%d%d: ", i+1, j+1); scanf("%f", &a[i][j]); // Taking input using nested for loop printf("enter elements of 2nd matrix\n");

for(i=0; i<2; ++i) for(j=0; j<2; ++j) printf("enter b%d%d: ", i+1, j+1); scanf("%f", &b[i][j]); // adding corresponding elements of two arrays for(i=0; i<2; ++i) for(j=0; j<2; ++j) c[i][j] = a[i][j] + b[i][j]; // Displaying the sum printf("\nsum Of Matrix:"); for(i=0; i<2; ++i) for(j=0; j<2; ++j) printf("%.1f\t", c[i][j]); if(j==1) printf("\n"); return 0; Ouput Enter elements of 1st matrix Enter a11: 2; Enter a12: 0.5; Enter a21: -1.1;

Enter a22: 2; Enter elements of 2nd matrix Enter b11: 0.2; Enter b12: 0; Enter b21: 0.23; Enter b22: 23; Sum Of Matrix: 2.2 0.5-0.9 25.0 Experiment: 12

Aim: Defining a function and calling it in the main. function declare in main() and outside main() #include <stdio.h> //void fun(int x,int y, int z); int main() void fun(int x,int y, int z); //Function inside main int a,b,c; printf("..."); scanf("...",...); printf("..."); fun(x,y,z); getchar(); return 0; void fun(int x,int y, int z)... printf(""); 1. WHAT IS C FUNCTION? A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by which performs specific operation in a C program. Actually, Collection of these functions creates a C program.

2. USES OF C FUNCTIONS: C functions are used to avoid rewriting same logic/code again and again in a program. There is no limit in calling C functions to make use of same functionality wherever required. We can call functions any number of times in a program and from any place in a program. A large C program can easily be tracked when it is divided into functions. The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs. 3. C FUNCTION DECLARATION, FUNCTION CALL AND FUNCTION DEFINITION: There are 3 aspects in each C function. They are, Function declaration or prototype This informs compiler about the function name, function parameters and return value s data type. Function call This calls the actual function Function definition This contains all the statements to be executed. C functions aspects syntax function definition Return_type function_name (arguments list) Body of function; function call function_name (arguments list); function declaration return_type function_name (argument list); SIMPLE EXAMPLE PROGRAM FOR C FUNCTION: C As you know, functions should be declared and defined before calling in a C program. In the below program, function square is called from main function. The value of m is passed as argument to the function square. This value is multiplied by itself in this function and multiplied value p is returned to main function from function square.

1 2 3 4 #include<stdio.h> // function prototype, also called function declaration float square ( float x ); // main function, program starts from here 5 6 7 8 9 10 11 12 13 int main( ) float m, n ; printf ( "\nenter some number for finding square \n"); scanf ( "%f", &m ) ; // function call n = square ( m ) ; printf ( "\nsquare of the given number %f is %f",m,n ); 14 15 16 float square ( float x ) // function definition 17 18 19 20 float p ; p = x * x ; return ( p ) ; 21

OUTPUT: Enter some number for finding square 2 Square of the given number 2.000000 is 4.000000 4. HOW TO CALL C FUNCTIONS IN A PROGRAM? There are two ways that a C function can be called from a program. They are, 1. Call by value 2. Call by reference 1. CALL BY VALUE: Note: In call by value method, the value of the variable is passed to the function as parameter. The value of the actual parameter can not be modified by formal parameter. Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter. Actual parameter This is the argument which is used in function call. Formal parameter This is the argument which is used in function definition EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY VALUE): C In this program, the values of the variables m and n are passed to the function swap. These values are copied to formal parameters a and b in swap function and used. 1 2 3 #include<stdio.h> // function prototype, also called function declaration void swap(int a, int b); 4

5 6 7 8 9 10 int main() int m = 22, n = 44; // calling swap function by value printf(" values before swap m = %d \nand n = %d", m, n); swap(m, n); 11 12 13 void swap(int a, int b) 14 15 16 17 18 19 int tmp; tmp = a; a = b; b = tmp; printf(" \nvalues after swap m = %d\n and n = %d", a, b); 20 OUTPUT: values before swap m = 22 and n = 44 values after swap m = 44 and n = 22 2. CALL BY REFERENCE: In call by reference method, the address of the variable is passed to the function as parameter. The value of the actual parameter can be modified by formal parameter.

Same memory is used for both actual and formal parameters since only address is used by both parameters. EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY REFERENCE): C In this program, the address of the variables m and n are passed to the function swap. These values are not copied to formal parameters a and b in swap function. Because, they are just holding the address of those variables. This address is used to access and change the values of the variables. 1 2 3 #include<stdio.h> // function prototype, also called function declaration void swap(int *a, int *b); 4 5 6 7 8 9 10 int main() int m = 22, n = 44; // calling swap function by reference printf("values before swap m = %d \n and n = %d",m,n); swap(&m, &n); 11 12 13 void swap(int *a, int *b) 14

15 16 17 18 19 int tmp; tmp = *a; *a = *b; *b = tmp; printf("\n values after swap a = %d \nand b = %d", *a, *b); 20 OUTPUT: values before swap m = 22 and n = 44 values after swap a = 44 and b = 22