0% found this document useful (0 votes)
13 views34 pages

Unit 3 Programming For Problem Solving

This document is a course outline for Unit 3: Arrays & Functions in a Programming for Problem Solving course, taught by Dr. Vishwa Pratap Singh. It covers the concepts of arrays, including single-dimensional and two-dimensional arrays, as well as string and character arrays, and details functions, their types, declarations, definitions, and advantages in C programming. The document provides examples and syntax for array and function usage, emphasizing the importance of modular programming and reusability.

Uploaded by

21deeno
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)
13 views34 pages

Unit 3 Programming For Problem Solving

This document is a course outline for Unit 3: Arrays & Functions in a Programming for Problem Solving course, taught by Dr. Vishwa Pratap Singh. It covers the concepts of arrays, including single-dimensional and two-dimensional arrays, as well as string and character arrays, and details functions, their types, declarations, definitions, and advantages in C programming. The document provides examples and syntax for array and function usage, emphasizing the importance of modular programming and reusability.

Uploaded by

21deeno
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/ 34

Arrays

Functions

Unit 3: Arrays & Function


Course: Programming for Problem Solving

Dr. Vishwa Pratap Singh

NextGen Academy

March 11, 2024

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 1 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Contents
1 Arrays
Single Dimensional Arrays
Two Dimensional Arrays
String and Character Arrays
2 Functions
Types of Functions
Function Declaration
Function Definition
Argument Passing
Advantages of Functions in C
Actual and Formal Arguments
Parameter Passing in Functions
Call by Value
Call by Reference
Passing Arrays to Functions
Nested Functions and Recursion

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 2 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Arrays in C

An array is a collection of similar data types or entities stored in contiguous


memory locations.
Arrays of characters form strings; each data item in an array is called an
element.
Each element is unique and resides in a separate memory location, identified by
a subscript.
Array elements share a variable, but each has a unique index number
(subscript).
Arrays can be single-dimensional (vectors) or multi-dimensional (matrices),
where subscripts determine the dimensionality.
Subscripts always start from zero.
One-dimensional arrays are vectors, while two-dimensional arrays are matrices.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 3 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Arrays in C

Advantages:
Array variables can store multiple values, unlike single variables that store only
one value.
Example: int arr[100]; int mark[100];
Declaration of Array:
Syntax: Data type array name [size];
Example: int arr[100]; int mark[100]; int a[5] = {10, 20,
30, 100, 5};
Initialization of Array:
Upon declaration, local array elements contain garbage values; global or static
arrays are automatically initialized with zero.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 4 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Single-Dimensional Arrays

Single-dimensional arrays are collections of elements of the same data type


arranged in a linear sequence.
They are also known as one-dimensional arrays and represent a simple form of
arrays.
Declaration syntax: Data type array name[size];
Example: int arr[100]; char characters[50];
Each element in a single-dimensional array is accessed using its index or
subscript.
Array indexing starts from zero, i.e., the first element has an index of 0, the
second has an index of 1, and so on.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 5 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Single-Dimensional Arrays

Elements can be of any valid data type (integers, characters, floats, etc.) based
on the array’s declared type.
Arrays can hold a fixed number of elements specified during declaration, and
the size cannot be changed during runtime.
Elements in the array can be initialized individually or collectively during
declaration.
Accessing array elements: Elements are accessed using their indices; for
example, arr[0], arr[1], etc.
Example of initializing array elements: int a[5] = {10, 20, 30, 40,
50};
A loop is commonly used to traverse through and process elements in a
single-dimensional array.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 6 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Two-Dimensional Arrays (Matrices)

A two-dimensional array, often referred to as a matrix, is an array of arrays.


The declaration of a 2D array involves specifying both rows and columns:
Data type array name[row][column];
In a 2D array, elements are organized in rows and columns, creating a grid-like
structure.
It can be visualized as an array of one-dimensional arrays where each row
represents a 1D array.
Total elements in a 2D array are calculated as the product of rows and columns:
Total elements = rows * columns

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 7 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

Two-Dimensional Arrays (Matrices)

Accessing elements in a 2D array requires two indices: one for the row and
another for the column.
Array indexing starts from zero for both rows and columns.
Example declaration: int matrix[3][3]; - a 3x3 matrix.
Elements are accessed using two indices: matrix[i][j] represents the
element at the i-th row and j-th column.
Common operations on matrices involve nested loops to traverse rows and
columns for processing elements.
Matrices are used extensively in mathematical operations, graphics, and other
data manipulation tasks.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 8 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

String and Character Arrays

Array of characters is called a string.


Strings are terminated by the NULL character (‘\0‘).
String is a one-dimensional array of characters.
We can initialize a string like:
char name[] = {’j’, ’o’, ’h’, ’n’, ’\0’};
Each character occupies 1 byte of memory.
The last character is always a NULL character.
ASCII values:
’\0’ has ASCII value 0.
0 (zero) has ASCII value 48.
Array elements of a character array are stored in contiguous memory.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 9 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

String Initialization

Representation: J o h N ‘\0‘
The terminating NULL (’\0’) is crucial for functions working with strings to
identify the string’s end.
String can also be initialized as: char name[] = "John";
The NULL character is not necessary in this case, as the compiler assumes it
automatically.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 10 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

String Constant (String Literal)

A string constant is a set of characters enclosed within double quotes.


Also known as a literal.
Whenever a string constant is written in a program, it’s stored in memory as an
array of characters terminated by a NULL character (‘\0‘).
Examples:
"m", "Tajmahal"
"My age is %d and height is %f\n"
The string constant itself becomes a pointer to the first character in the array.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 11 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

strlen()

strlen(): Returns the length of a string.


Number of characters in the string, excluding the terminating NULL character.
Accepts a pointer to the first character of the string.
Example: strlen("suresh") returns 6.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 12 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

strcmp()

strcmp(): Compares two strings.


Returns:
<0 when s1<s2
=0 when s1=s2
>0 when s1>s2
Example: strcmp(s1, s2) returns a value based on string comparison.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 13 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

strcpy()

strcpy(): Copies one string to another.


Copies str2 to str1 including the NULL character.
str2 is the source string, str1 is the destination string.
Example: strcpy(str1, str2) copies str2 to str1.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 14 / 34


Single Dimensional Arrays
Arrays
Two Dimensional Arrays
Functions
String and Character Arrays

strcat()

strcat(): Appends a copy of one string at the end of another.


Moves the NULL character from str1 and adds str2 at the end of str1.
str2 remains unaffected.
Returns a pointer to the modified str1.
Example: strcat(str1, str2) appends str2 to the end of str1.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 15 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Contents
1 Arrays
Single Dimensional Arrays
Two Dimensional Arrays
String and Character Arrays
2 Functions
Types of Functions
Function Declaration
Function Definition
Argument Passing
Advantages of Functions in C
Actual and Formal Arguments
Parameter Passing in Functions
Call by Value
Call by Reference
Passing Arrays to Functions
Nested Functions and Recursion

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 16 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Function Definition

A function is a self-contained block of code.


Contains a set of statements that perform a specific or coherent task.
Is called or invoked when needed.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 17 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Function Characteristics

Reusable: Functions can be called multiple times from different parts of a


program.
Modularity: Helps in breaking down complex tasks into smaller, manageable
units.
Encapsulation: Functions encapsulate functionality, making code easier to
understand and maintain.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 18 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Function Invocation

Functions are invoked or called by their name.


Parameters (if any) can be passed to the function.
Functions can return values or perform actions without returning a value.
Example: functionName(parameter1, parameter2)

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 19 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Library Function

Library functions are predefined functions provided by the system or external


libraries.
Cannot be modified, only read and used.
Examples: strlen(), strcmp(), printf(), scanf(), etc.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 20 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

User Defined Function

Defined by the user according to specific requirements.


Syntax:
Return type name of function (type1 arg1, type2 arg2, type3
arg3)
Allows modularity and reusability in code.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 21 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Function Declaration
Function Declaration:
It informs the compiler about three things:
The name of the function
Number and type of arguments received by the function
Type of value returned by the function
The name of the argument is optional
The function prototype is always terminated by a semicolon
Example:
int function(int, int, int); /* Function declaration */
Calling Function (in main()):
function(arg1, arg2, arg3);
Function Definition:
int function(type1 arg1, type2 arg2, type3 arg3) /* Function
definition */
{
Local variable declaration;
Statement;
return value;
}
Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 22 / 34
Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Function Definition

Function Definition:
Function definition consists of the complete description and code of the function.
It specifies what the function does, its inputs, and its output.
Syntax:
return type function(type1 arg1, type2 arg2, type3 arg3) /*
Function header */
{
Local variable declaration;
Statement 1;
Statement 2;
return value;
}

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 23 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Argument Passing in Functions

Function Calls:
Function(x);
Function(20, 30);
Function(a*b, c*d);
Function(2, 3, sum(a, b));

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 24 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Advantages of Functions in C

Reusability:
Functions enable calling the same code multiple times, saving developer time and
effort.
Code can be reused in different parts of the program, enhancing efficiency.
Modular Programming:
Functions facilitate breaking down complex problems into smaller, manageable
pieces.
Enables the division of a program into smaller parts, easing development and
debugging.
Efficiency:
Breaking a program into functions allows for easier optimization of code for speed
and memory usage.
Optimized functions can result in faster program execution and reduced memory
consumption.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 25 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Actual and Formal Arguments

Parameters (or Arguments):


Parameters pass information into and out of procedures in a program.
Also referred to as arguments.
When defining a parameter, its usage mode is specified.
Modes of Parameters/Arguments:
1 In Mode:
Passes information into the procedure.
2 Out Mode:
Sends information out of the procedure back to the calling program.
3 In-Out Mode:
Combines both passing information in and out.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 26 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Actual Parameters

Definition:
Actual arguments are the values passed during a function call.
Defined in the calling function.
These values are the variables or expressions referenced in the parameter list of the
function call.
Characteristics:
No need to specify datatype for actual parameters.
Represented by variables or expressions used in the function call.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 27 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Formal Parameters

Definition:
Formal parameters are variables or expressions listed in the parameter list of a
subprogram specification (function or procedure).
The datatype of the receiving value must be defined.
Scope of formal arguments is local to the function definition where they are used.
Characteristics:
Must define the datatype for the receiving value.
Scope is restricted to the function or subprogram where they are defined.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 28 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Parameter Passing in Functions

When a function is executed:


Control transfers from the calling function to the called function for execution.
Data values passed during this transfer are called parameters.
Types of Parameters in C:
Actual Parameters: Specified in the calling function.
Formal Parameters: Declared in the called function.
Passing Methods in C:
Call by Value: Copies actual parameter values to formal parameters.
Call by Reference: Passes references/address of actual parameters to formal
parameters.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 29 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Call by Value

Methodology:
Copies of actual parameter values are passed to formal parameters.
The called function uses these formal parameters for execution.
Effect on Parameters:
Changes made to formal parameters do not affect actual parameter values.
Upon returning to the calling function, actual parameter values remain unchanged.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 30 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Call by Reference

Methodology:
Memory location addresses of actual parameters are passed to formal parameters.
Formal parameters are pointers used to access actual parameter memory locations.
Effect on Parameters:
Changes made to formal parameters affect the values of actual parameters.
Using formal parameters directly accesses and modifies actual parameter values.
Requirement:
Formal parameters must be pointer variables in this method.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 31 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Passing Arrays to Functions in C

Array Passing:
In C, the entire array cannot be directly passed as an argument to a function.
However, a pointer to the array’s first element can be passed by specifying the
array’s name.
Passing Methods:
Syntax in function definition:
return type foo(array type array name[size], ...);
return type foo(array type array name[], ...);
return type foo(array type* array name, ...);
Array is passed as a pointer even when declared as an array in the function
argument.
Array Decay:
Passing an array to a function leads to array decay, losing size information.
Size or number of elements in the array can’t be determined within the function.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 32 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Nested Functions

Definition:
Nested functions refer to defining a function within another function.
The inner function has access to the variables of the outer function.
Often used for encapsulation and to organize code logically.
Characteristics:
Encapsulation: Inner functions have access to the outer function’s variables.
Local Scope: Inner functions are only accessible within the scope of the outer
function.
Clarity and Modularity: Helps in breaking down complex logic into smaller,
manageable parts.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 33 / 34


Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion

Recursion

Definition:
Recursion refers to the process where a function calls itself directly or indirectly.
Used to solve problems by dividing them into smaller, similar subproblems.
Involves a base case to terminate the recursive calls.
Characteristics:
Divides Problems: Breaks down complex problems into simpler ones.
Base Case: Essential to prevent infinite recursion and end the recursive calls.
Requires Extra Memory: Each recursive call adds to the function call stack.

Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 34 / 34

You might also like