[go: up one dir, main page]

0% found this document useful (0 votes)
4 views50 pages

C Arrays Functions

Uploaded by

45yg2
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)
4 views50 pages

C Arrays Functions

Uploaded by

45yg2
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/ 50

Damanpreet Singh

Professor
Department of Computer Science & Engg.
Sant Longowal Institute of Engineering & Technology,
(Deemed-to-be-University)
Longowal - 148106

Damanpreet Singh 1
ARRAYS
An array is collection of same data type.
int x[n] Index
or subscript
Data Type
identifier
One dimensional arrays

0 to (n-1) elements of arrays

x[0], x[1], x[2], …., x[n-1]

Damanpreet Singh 2
3
4
5
6
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("\n\nRead and Print elements of an array:\n");
printf("-----------------------------------------\n");
printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("\nElements in array are: ");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
} 7
8
Find the sum of all elements of an array

9
#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
printf("\n\nFind sum of all elements of array:\n");
printf("--------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum += a[i];
}

printf("Sum of all elements stored in the array is : %d\n\n", sum);

}
Damanpreet Singh 10
ARRAYS
An array is collection of same data type.
int Y[n][m]
Data Type Index 2

Index 1
Two dimensional arrays
Y[0][0], Y[0][1], Y[0][2], …., Y[0][m-1]
Y[1][0], Y[1][1], Y[1][2], …., Y[1][m-1]
Y[2][0], Y[2][1], Y[2][2], …., Y[2][m-1]
: : : :
Y[n-1][0], Y[n-1][1], Y[n-1][2],…., Y[n-1][m-1]
Damanpreet Singh 11
12
13
14
15
16
#include <stdio.h>
void main()
{
int arr1[3][3],i,j;
printf("\n\nRead a 2D array of size 3x3 and print the matrix :\n");
printf("------------------------------------------------------\n");
/* Stored values into the array*/
printf("Input elements in the matrix :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("\nThe matrix is : \n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",arr1[i][j]);
}
printf("\n\n");
}
17
18
Function
• A self-contained program segment that
carries out some specific, well-defined
task.
Some properties:
– Every C program consists of one or more
functions.

• One of these functions must be called


“main”.
• Execution of the program always begins by
carrying out the instructions in “main”.

– A function will carry out its intended


action whenever it is called or invoked.
19
• In general, a function will process
information that is passed to it from the
calling portion of the program, and
returns a single value.
• Information is passed to the function via
special identifiers called arguments or
parameters.
• The value is returned by the “return”
statement.
• Some function may not return anything.
• Return data type specified as “void”.

20
Why Functions?
• Functions
– Allows one to develop a program in a
modular fashion.
• Divide-and-conquer approach.
– All variables declared inside functions
are local variables.
• Known only in function defined.
• There are exceptions (to be discussed
later).
– Parameters
• Communicate information between functions.
• They also become local variables.
21
• Benefits
– Divide and conquer
• Manageable program development.
• Construct a program from small
pieces or components.
– Software reusability
• Use existing functions as building
blocks for new programs.
• Abstraction: hide internal details
(library functions).

22
When a function is called from some other
function, the corresponding arguments in
the function call are called actual
arguments or actual parameters.
– The formal and actual arguments must
match in their data types.

• Point to note:
– The identifiers used as formal
arguments are “local”.
• Not recognized outside the function.
• Names of formal and actual arguments
may differ.
23
Some Points
• A function cannot be defined within
another function.
– All function definitions must be
disjoint.
• Nested function calls are allowed.
– A calls B, B calls C, C calls D,
etc.
– The function called last will be the
first to return.
• A function can also call itself,
either directly or in a cycle.
– A calls B, B calls C, C calls back
A.
– Called recursive call or recursion.
24
Function Steps
• Declare the function in main function
Return Name of Formal Arguments /
type Function Parameters
• Call the function
Name of Actual Arguments /
Function Parameters
• Define the function
Return Name of Formal Arguments /
type Function Parameters
Damanpreet Singh 25
Function: Passing of parameters

Return Name of Formal Arguments


type Function

Name of Actual parameters


Function

Return Name of Formal Arguments


type Function

Damanpreet Singh 26
Function: Passing of parameters

Return Name of Formal Arguments


type Function

Name of Actual parameters


Function

Return Name of Formal Arguments


type Function

Damanpreet Singh 27
Functions
Scope of Variable
• Local variables
• Available in the function where
these are declared
• Global variables
• Available in main function as well
as other defined function
• Declared before the main function

Damanpreet Singh 28
Functions
Passing of Parameters
• Valued Parameters
• Actual parameters are copied to
formal arguments
• Reference Parameters
• Actual parameters & formal
arguments share same memory
location

Damanpreet Singh 29
Function Return type

#include<stdio.h> Function name


void order(int, float, char a[]); Arguments
void main() Function Declaration
{
int m; float y; char z[5]; Actual Parameter
order(m, y, z); Function call
}
Function name
No return type

void order(int N, float X, char name[5])


{ Formal arguments

} Function name
Return type
Damanpreet Singh 30
Function Return type

#include<stdio.h> Function name


float order(int, float, char a[]); Arguments
void main() Function Declaration
{
int m; float y, P; char z[5]; Actual Parameter
P=order(m, y, z); Function call
}
No return type Function name
float order(int N, float X, char name[5])
{
float Q Formal arguments
Q=expression
return(Q) Function name
} Return type
Damanpreet Singh 31
Global Parameters
Function Return type

#include<stdio.h> Function name


int m; float y, P; char z[5]; Arguments
float order(void); Function Declaration
void main()
{ Actual Parameter
P=order(m, y, z); Function call
}
No return type Function name
float order(void)
{
Formal arguments
}
Function name
Return type
Damanpreet Singh 32
Function Return type

#include<stdio.h> Function name


float order(int&, float&, char a[]); Arguments
void main() Function Declaration
{
int m; float y, P; char z[5]; Actual Parameter
order(m, y, z); Function call
}
No return type Function name

float order(int& N, float& X, char& name[5])


{
Formal arguments

} Function name
Return type
Damanpreet Singh 33
Function
#include<stdio.h>
void order(int, int);
void main()
{
int n1=99, n2=11;
printf ("n1=“, "n2=“;n1,n2;
order(n1,n2);
printf ("n1=“, "n2=“;n1,n2;
}

Damanpreet Singh 34
Function
void order(int numb1,int numb2)
{
if(numb1>numb2)
{
int temp=numb1;
numb1=numb2;
numb2=temp;
};
printf ("n1=“, "n2=“;numb 1,numb 2;
}
n1=99 n2=11
n1=11 n2=99
n1=99 n2=11
Damanpreet Singh 35
Function
#include<stdio.h>
int n1,n2;
Global Variables
void order(void);
void main()
{
n1=99, n2=11;
printf ("n1=“, "n2=“;n1,n2;
Order();
printf ("n1=“, "n2=“;n1,n2;
}

Damanpreet Singh 36
Function
void order(void)
{
if(n1>n2)
{
int temp=n1;
n1=n2;
n2=temp;
};
printf ("n1=“, "n2=“;n1,n2;
}
n1=99 n2=11
n1=11 n2=99
n1=11 n2=99
Damanpreet Singh 37
Function
#include<stdio.h>
void order(int&, int&);
void main()
{
int n1=99, n2=11;
int n3=22, n4=88;
order(n1,n2);
printf ("n1=“, "n2=“;n1,n2;
order(n3,n4);
printf ("n3=“, "n4=“;n3,n4;
}

Damanpreet Singh 38
Function
void order(int& numb1,int&numb2)
{
if(numb1>numb2)
{
int temp=numb1;
numb1=numb2;
numb2=temp;
};
printf ("n1=“, "n2=“;numb 1,numb 2;
}
n1=11 n2=99 n3=22 n4=88
n1=11 n2=99 n3=22 n4=88

Damanpreet Singh 39
Function
#include<stdio.h>
float avg(int,float);
void main()
{

int i,m,n; float z,y[100];


printf("feed m:“);
scanf(&m);
for(i=1;i<=m;++i)
{
Enter elements in to array y
}
z=average(n,y);
printf ("average=“, z);
} Damanpreet Singh 40
Function
float average(int n,float x[])
{
float avg, sum;
int i;
sum=0.0;
for(i=1;i<=n;++i)
sum+=x[i];
avg=sum/float(n);
return(avg);
}

Damanpreet Singh 41
// Program to calculate the sum of array elements by passing to a
function

#include <stdio.h>
float calculateSum(float num[]);

void main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

// num array is passed to calculateSum()


result = calculateSum(num);
printf("Result = %.2f", result);
}

float calculateSum(float num[])


{
float sum = 0.0;

for (int i = 0; i < 6; ++i) {


sum += num[i];
}
return sum;
}
In C, when we pass an array to a
function say fun(), it is always treated
as a pointer by fun().
Way-1
Formal parameters as a pointer −
void myFunction(int *param)
{...}

Way-2
Formal parameters as a sized array −
void myFunction(int param [10])
{...}

Way-3
Formal parameters as an unsized array −
void myFunction(int param[ ])
{...}
Type casting

◼ Type casting is the process of converting one data type


to another data type by the programmer using the
casting operation during program design.
◼ It’s a mechanism by which we can change the data type
of the variable irrespective of how it was originally
defined.
◼ When a variable typecast into another variable , the
compiler basically treats the variable as of the new data
type.
◼ Syntax
Int a;
Float b;
b= (float) a;
Types of Type casting:
▪ Implicit

▪ Explicit
Implicit Type casting

◼ Implicit type casting is used to convert the data


type of any variable without using actual value
of the variable.
◼ It performs the conversions without altering any
of the values that are stored in the data
variable.
◼ Conversion of lower data type to higher data
type will occur automatically.
Conversion of data type is based on the given
hierarchy:
Explicit Typecasting
◼ Explicit typecasting is user defined
typecasting.
◼ It is explicitly defined within the program.
Example

#include<stdio.h>
int main()
{
int a= 15, b=2;
Char x=‘a’;
double div;
div=a/b;
x=x+4;
print f(“ The result of explicit typecasting is %f, div);
print f(“ The result of implicit typecasting is %c\n”, x);
return 0;
}
Output: The result of explicit typecasting is 7.50000
Output: The result of implicit typecasting is e

You might also like