0% found this document useful (0 votes)
12 views2 pages

KP IP Project

The document discusses arrays and functions in C programming. It provides details on one dimensional and two dimensional arrays, including syntax and examples. It also covers function definition, declaration, types based on arguments and return values, and advantages of using functions such as reusability and modularity. The document compares call by value and call by reference methods of passing arguments to functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

KP IP Project

The document discusses arrays and functions in C programming. It provides details on one dimensional and two dimensional arrays, including syntax and examples. It also covers function definition, declaration, types based on arguments and return values, and advantages of using functions such as reusability and modularity. The document compares call by value and call by reference methods of passing arguments to functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Array: printf("Enter the number of printf("%d\t", arr[i][j]);

elements in the array: ");


An array is a collection of elements of }
a same data type. scanf("%d", &n);
printf("\n");
Syntax: datatype arrayname[size]; int arr[n]; // Declare an array of size
}
n
Array are classified into two types
return 0; }
printf("Enter the elements of the
1.One dimensional array
array:\n"); Output:
2.Two dimensional array
for (i = 0; i < n; i++) { The array elements are:
One dimensional array:
scanf("%d", &arr[i]); 1 2 3
One-Dimensional Array is a group of
} 4 5 6
elements having the same data type
which are stored in a linear printf("The elements of the array Functions:
arrangement under a single variable are: ");
name. A function is a block of code that
for (i = 0; i < n; i++) { performs a specific task. It is a
Syntax: datatype arrayname[size]; reusable code that can be called
printf("%d ", arr[i]);
upon from anywhere in the program.
Two-dimensional array:
} Functions make the code more
A two-dimensional array, also known modular and easier to understand. It
printf("\n");
as a 2D array, A matrix with rows and runs only when the function is called.
columns is a two dimensional array. return 0;
Functions are classified into four
Syntax: datatype } types based on arguments and return
arrayname[size][size]; type .
#include <stdio.h>
Advantages of arrays: 1. Function with no arguments
int main() { and no return value .
1.Arrays make the code more
int rows, cols, i, j; 2. Function with no arguments
optimized and clean since we can
and a return value.
store multiple elements in a single printf("Enter the number of rows: "); 3. Function with arguments
array at once, so we do not have to
scanf("%d", &rows); and a no return value.
write or initialize them multiple
4. Function with arguments and
times. printf("Enter the number of a return value .
2.Every element can be traversed in columns: ");
Syntax for function declaration:
an array using a single loop. scanf("%d", &cols);
Returntype name(parameters);
3.Arrays make sorting much easier. int arr[rows][cols]; // Declare a 2D
Elements can be sorted by writing a array with the given dimensions Function definition:
few lines of code.
printf("Enter the elements of the Returntype name(parameters)
4.Any array element can be accessed array:\n"); Function calling:
in any order either from the front or
rear in O(1) time. for (i = 0; i < rows; i++) { name(parameters);
5.Insertion or deletion of the for (j = 0; j < cols; j++) { Advantages of functions :
elements can be done in linear
scanf("%d", &arr[i][j]); Reusability:
complexity in an array.
} Functions allow you to reuse code,
Example:
} which can save you time and effort.
#include <stdio.h>
printf("The elements of the array Modularity:
int main() {
are:\n"); Functions allow you to break down
int n, i; your code into smaller, more
for (i = 0; i < rows; i++) {
manageable pieces. This can make
for (j = 0; j < cols; j++) { your code easier to write, debug, and
maintain.
Abstraction: swapByReference(&x, &y);

Functions can be used to hide the printf("After swapping (call by


implementation details of your code. reference) - x:%d, y:%d\n", x, y);
This can make your code easier to
return 0; }
understand and use.

Readability:
#include<stdio.h>
Functions can make your code more
readable by grouping related code Void add();
together.
Void main ()
Maintainability:
{ add();
Functions can make your code more
maintainable by making it easier to Add();
change and update individual parts of }
your code without affecting the
rest of the code. void add()

we can pass values to the parameters { int a,b,c;


by two types : a=10;
1. Call by value b=5;
2. Call by reference
c=a+b;
1.call by value :
printf(“\n the sum =%d”,c);
#include <stdio.h>
}
void swapByValue(int a, int b) {
Output :
int temp = a;
The sum = 15
a = b;
The sum = 15
b = temp; }

void main() {

int x = 10, y = 20;

printf("Before swapping - x:%d,


y:%d\n", x, y);

swapByValue(x, y);

printf("After swapping (call by


value) - x:%d, y:%d\n", x, y); }

2.Call by reference :

#include <stdio.h>

void swapByReference(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

} int main() {

int x = 10, y = 20;

printf("Before swapping - x:%d,


y:%d\n", x, y);

You might also like