Here is a function to sort an array in ascending order using selection sort algorithm:
#include <stdio.h>
void selectionSort(int array[], int size){
int i, j, min, temp;
for(i=0; i<size-1; i++){
min = i;
for(j=i+1; j<size; j++){
if(array[j] < array[min])
min = j;
}
temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main(){
int arr[] = {5, 3, 6, 2
P, NP, NP-Complete, and NP-Hard
Reductionism in Algorithms
NP-Completeness and Cooks Theorem
NP-Complete and NP-Hard Problems
Travelling Salesman Problem (TSP)
Travelling Salesman Problem (TSP) - Approximation Algorithms
PRIMES is in P - (A hope for NP problems in P)
Millennium Problems
Conclusions
Artificial neural networks (ANNs) or connectionist systems are a computational model used in machine learning, computer science and other research disciplines, which is based on a large collection of connected simple units called artificial neurons, loosely analogous to axons in a biological brain. Connections between neurons carry an activation signal of varying strength. If the combined incoming signals are strong enough, the neuron becomes activated and the signal travels to other neurons connected to it. Such systems can be trained from examples, rather than explicitly programmed, and excel in areas where the solution or feature detection is difficult to express in a traditional computer program. Like other machine learning methods, neural networks have been used to solve a wide variety of tasks, like computer vision and speech recognition, that are difficult to solve using ordinary rule-based programming.
Find me on:
AFCIT
http://www.afcit.xyz
YouTube
https://www.youtube.com/channel/UCuewOYbBXH5gwhfOrQOZOdw
Google Plus
https://plus.google.com/u/0/+AhmedGadIT
SlideShare
https://www.slideshare.net/AhmedGadFCIT
LinkedIn
https://www.linkedin.com/in/ahmedfgad/
ResearchGate
https://www.researchgate.net/profile/Ahmed_Gad13
Academia
https://www.academia.edu/
Google Scholar
https://scholar.google.com.eg/citations?user=r07tjocAAAAJ&hl=en
Mendelay
https://www.mendeley.com/profiles/ahmed-gad12/
ORCID
https://orcid.org/0000-0003-1978-8574
StackOverFlow
http://stackoverflow.com/users/5426539/ahmed-gad
Twitter
https://twitter.com/ahmedfgad
Facebook
https://www.facebook.com/ahmed.f.gadd
Pinterest
https://www.pinterest.com/ahmedfgad/
P, NP, NP-Complete, and NP-Hard
Reductionism in Algorithms
NP-Completeness and Cooks Theorem
NP-Complete and NP-Hard Problems
Travelling Salesman Problem (TSP)
Travelling Salesman Problem (TSP) - Approximation Algorithms
PRIMES is in P - (A hope for NP problems in P)
Millennium Problems
Conclusions
Artificial neural networks (ANNs) or connectionist systems are a computational model used in machine learning, computer science and other research disciplines, which is based on a large collection of connected simple units called artificial neurons, loosely analogous to axons in a biological brain. Connections between neurons carry an activation signal of varying strength. If the combined incoming signals are strong enough, the neuron becomes activated and the signal travels to other neurons connected to it. Such systems can be trained from examples, rather than explicitly programmed, and excel in areas where the solution or feature detection is difficult to express in a traditional computer program. Like other machine learning methods, neural networks have been used to solve a wide variety of tasks, like computer vision and speech recognition, that are difficult to solve using ordinary rule-based programming.
Find me on:
AFCIT
http://www.afcit.xyz
YouTube
https://www.youtube.com/channel/UCuewOYbBXH5gwhfOrQOZOdw
Google Plus
https://plus.google.com/u/0/+AhmedGadIT
SlideShare
https://www.slideshare.net/AhmedGadFCIT
LinkedIn
https://www.linkedin.com/in/ahmedfgad/
ResearchGate
https://www.researchgate.net/profile/Ahmed_Gad13
Academia
https://www.academia.edu/
Google Scholar
https://scholar.google.com.eg/citations?user=r07tjocAAAAJ&hl=en
Mendelay
https://www.mendeley.com/profiles/ahmed-gad12/
ORCID
https://orcid.org/0000-0003-1978-8574
StackOverFlow
http://stackoverflow.com/users/5426539/ahmed-gad
Twitter
https://twitter.com/ahmedfgad
Facebook
https://www.facebook.com/ahmed.f.gadd
Pinterest
https://www.pinterest.com/ahmedfgad/
It tells about functions in C++,Types,Use,prototype,declaration,Arguments etc
function with
A function with no parameter and no return value
A function with parameter and no return value
A function with parameter and return value
A function without parameter and return value
Call by value and address
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
Water scarcity is the lack of fresh water resources to meet the standard water demand. There are two type of water scarcity. One is physical. The other is economic water scarcity.
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...Amil Baba Dawood bangali
Contact with Dawood Bhai Just call on +92322-6382012 and we'll help you. We'll solve all your problems within 12 to 24 hours and with 101% guarantee and with astrology systematic. If you want to take any personal or professional advice then also you can call us on +92322-6382012 , ONLINE LOVE PROBLEM & Other all types of Daily Life Problem's.Then CALL or WHATSAPP us on +92322-6382012 and Get all these problems solutions here by Amil Baba DAWOOD BANGALI
#vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore#blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #blackmagicforlove #blackmagicformarriage #aamilbaba #kalajadu #kalailam #taweez #wazifaexpert #jadumantar #vashikaranspecialist #astrologer #palmistry #amliyaat #taweez #manpasandshadi #horoscope #spiritual #lovelife #lovespell #marriagespell#aamilbabainpakistan #amilbabainkarachi #powerfullblackmagicspell #kalajadumantarspecialist #realamilbaba #AmilbabainPakistan #astrologerincanada #astrologerindubai #lovespellsmaster #kalajaduspecialist #lovespellsthatwork #aamilbabainlahore #Amilbabainuk #amilbabainspain #amilbabaindubai #Amilbabainnorway #amilbabainkrachi #amilbabainlahore #amilbabaingujranwalan #amilbabainislamabad
Automobile Management System Project Report.pdfKamal Acharya
The proposed project is developed to manage the automobile in the automobile dealer company. The main module in this project is login, automobile management, customer management, sales, complaints and reports. The first module is the login. The automobile showroom owner should login to the project for usage. The username and password are verified and if it is correct, next form opens. If the username and password are not correct, it shows the error message.
When a customer search for a automobile, if the automobile is available, they will be taken to a page that shows the details of the automobile including automobile name, automobile ID, quantity, price etc. “Automobile Management System” is useful for maintaining automobiles, customers effectively and hence helps for establishing good relation between customer and automobile organization. It contains various customized modules for effectively maintaining automobiles and stock information accurately and safely.
When the automobile is sold to the customer, stock will be reduced automatically. When a new purchase is made, stock will be increased automatically. While selecting automobiles for sale, the proposed software will automatically check for total number of available stock of that particular item, if the total stock of that particular item is less than 5, software will notify the user to purchase the particular item.
Also when the user tries to sale items which are not in stock, the system will prompt the user that the stock is not enough. Customers of this system can search for a automobile; can purchase a automobile easily by selecting fast. On the other hand the stock of automobiles can be maintained perfectly by the automobile shop manager overcoming the drawbacks of existing system.
Vaccine management system project report documentation..pdfKamal Acharya
The Division of Vaccine and Immunization is facing increasing difficulty monitoring vaccines and other commodities distribution once they have been distributed from the national stores. With the introduction of new vaccines, more challenges have been anticipated with this additions posing serious threat to the already over strained vaccine supply chain system in Kenya.
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
This paper addresses the vulnerability of deep learning models, particularly convolutional neural networks
(CNN)s, to adversarial attacks and presents a proactive training technique designed to counter them. We
introduce a novel volumization algorithm, which transforms 2D images into 3D volumetric representations.
When combined with 3D convolution and deep curriculum learning optimization (CLO), itsignificantly improves
the immunity of models against localized universal attacks by up to 40%. We evaluate our proposed approach
using contemporary CNN architectures and the modified Canadian Institute for Advanced Research (CIFAR-10
and CIFAR-100) and ImageNet Large Scale Visual Recognition Challenge (ILSVRC12) datasets, showcasing
accuracy improvements over previous techniques. The results indicate that the combination of the volumetric
input and curriculum learning holds significant promise for mitigating adversarial attacks without necessitating
adversary training.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
Maintaining high-quality standards in the production of TMT bars is crucial for ensuring structural integrity in construction. Addressing common defects through careful monitoring, standardized processes, and advanced technology can significantly improve the quality of TMT bars. Continuous training and adherence to quality control measures will also play a pivotal role in minimizing these defects.
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)MdTanvirMahtab2
This presentation is about the working procedure of Shahjalal Fertilizer Company Limited (SFCL). A Govt. owned Company of Bangladesh Chemical Industries Corporation under Ministry of Industries.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
Planning Of Procurement o different goods and services
EST 102 Programming in C-MODULE 4
1. Introduction to modular programming, writing functions, formal
parameters, actual parameters .Pass by Value, Recursion, Arrays
as Function Parameters ,structure, union, Storage Classes, Scope
and life time of variables
MODULE 4
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 1
2. Functions
• A function is a group of statements
that together perform a task.
• Every C program has at least one
function, which is main().
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 2
3. Why to use Functions?
• Development can be divided
• Readable programs
• Programming errors are easy to detect
• Allows re-use of codes
• Improves manageability
• Collaboration
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 3
4. Types of Functions
There are two types of functions in C programming:
• Standard library functions
• User-defined functions
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 4
5. Standard Library Functions
• The standard library functions are built-in functions
in C programming.
• These functions are defined in header files.
For example,
• The printf() is defined in the stdio.h header file.
• The sqrt() function is defined in the math.h header
file.
• The strlen() function is defined in the string.h
header file.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 5
6. User-defined function
• User can also create functions as per their own
needs.
• Such functions created by users are known as
user-defined functions.
Advantages of user-defined function
• easier to understand, maintain and debug.
• Reusable codes
• A large program can be divided among many
programmers.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 6
7. • Three parts of a user defined functions are:
– Function Declaration or Prototype
– Function Definition
– Function Call
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 7
8. Function Declaration or function prototype
• A function prototype or function declaration is simply
the declaration of a function that specifies function's
name, parameters and return type.
• It doesn't contain function body.
• A function prototype gives information to the compiler
that the function may later be used in the program.
Syntax
• return_type function_name( parameter list );
• int add(int a, int b);6/30/2020 NIMMY RAJU,AP,VKCET,TVM 8
9. Function Definition
• A function definition in C programming consists
of a function header and a function body. Here
are all the parts of a function:
Return Type:
• A function may return a value.
• The return_type is the data type of the value the
function returns.
• Some functions perform the desired operations
without returning a value.
• In this case, the return_type is the keyword void.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 9
10. Function Definition
• Function Name: The actual name of the
function.
• Arguments: When a function is invoked, you
pass a value to the parameter. This value is
referred to as argument.
• Function Body: The function body contains a
collection of statements that define what the
function does.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 10
12. Function Call
• When a program calls a function from the
main function , the program control is
transferred to the called function.
• A called function performs a defined task and
when its return statement is executed or
when its function-ending closing brace is
reached, it returns the program control back
to the main program.
Syntax:
• functionName(parameter list);
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 12
14. • Return Statement
• The return statement terminates the
execution of a function and returns a value to
the calling function.
• The program control is transferred to the
calling function after the return statement.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 14
15. Types of user defined functions
Types INPUT OUTPUT
No arguments passed and
no return value
Sub function Sub function
Arguments passed but no
return value
Main function Sub function
No arguments passed but
return value
Sub function Main function
Arguments passed with
return value
Main function Main function
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 15
16. Without function Using function(No arguments passed and
no return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of
a & b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
void add();
void main()
{
add();
}
// function to read two number and
print its sum
void add()
{
int a,b,sum;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
Program to find the sum of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 16
17. Without function Using function(arguments passed and no
return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
void add(int ,int );
void main()
{
int a,b;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
add(a,b);
}
// function that takes two arguments
and print their sum.
void add(int a,int b)
{
int sum= a + b;
printf("Sum=%d",sum);
}
Program to find the sum of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 17
18. Without function Using function(No arguments passed and but
return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
int add( );
void main()
{
int S;
S=add();
printf("Sum=%d",S);
}
// function to read two number and
return its sum
int add()
{
int a,b,sum;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
sum = a+b;
return sum;
}6/30/2020 NIMMY RAJU,AP,VKCET,TVM 18
19. Without function Using function(arguments passed with return
value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
int add(int ,int );
void main()
{
int a,b,S;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
S=add(a,b);
printf("Sum=%d",S);
}
// function that takes two arguments
and print their sum.
int add(int a,int b)
{
int sum;
sum = a+b;
return sum;
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 19
20. No arguments
passed and no
return value
No arguments
passed and no return
value
No arguments passed
and no return value
No arguments passed
and no return value
#include<stdio.h>
void large();
void main()
{
large();
}
void large()
{
int a,b;
printf("Enter the
values of a & b");
scanf("%d%d",&a,&b)
;
If(a>b)
printf(“Largest=%d",a
);
else
printf(“Largest=%d“,a
);
}
#include<stdio.h>
void large(int,int);
void main()
{
int a,b;
printf("Enter the values
of a & b");
scanf("%d%d",&a,&b);
large(a,b);
}
void large(int x,int y)
{
If(x>y)
printf(“Largest=%d",x);
else
printf(“Largest=%d“,y);
}
#include<stdio.h>
int large();
void main()
{
int l;
l=large();
printf(“Large=%d”,l);
}
int large()
{
int a,b;
printf("Enter the values of
a & b");
scanf("%d%d",&a,&b);
If(a>b)
return a;
else
return b;
}
#include<stdio.h>
int large(int,int);
void main()
{
int a,b,l;
printf("Enter the values
of a & b");
scanf("%d%d",&a,&b);
l=large(a,b);
printf(“Large=%d”,l);
int large(int a,int b)
{
If(a>b)
return a;
else
return b;
}
Largest of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 20
21. • Formal and Actual Parameters
• Let us assume that a function B() is called
from another function A().
• In this case A is called the “caller functionor
calling function” and B is called the “called
function or callee function”.
• Also, the arguments which A sends to B are
called actual arguments and the parameters
of B are called formal arguments.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 21
22. • Formal Parameter: A variable and its type as
they appear in the prototype of the function
or method.
• Actual Parameter: The variable or expression
corresponding to a formal parameter that
appears in the function or method call in the
calling environment.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 22
23. Formal and Actual parameters
Let us assume that a function B() is called from another function
A(). In this case A is called the “caller function or calling
function” and B is called the “called function or callee
function”.
The arguments which A sends to B are called actual arguments
(parameters)and the arguments of B are called formal
arguments(parameters).
Formal Parameter: A variable that appears in the prototype of
the function.
Actual Parameter: The variable corresponding to a formal
parameter that appears in the function call in the calling function.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 23
24. Example:
#include<stdio.h>
int swap(int a,int b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(x,y); //function call
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int a,int b) //function definition
{
int temp=a;
a=b;
b=temp;
printf("a=%dt b=%d",a,b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=10 y=20
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 24
25. Pass by value(Call by value)
• In this parameter passing technique, a copy of
actual parameter is passed to formal parameter.
• As a result, any changes or modification
happened to formal parameter won’t reflect
back to actual parameter
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 25
26. Example:
#include<stdio.h>
int swap(int a,int b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(x,y); //function call –pass by value
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int a,int b) //function definition
{
int temp=a;
a=b;
b=temp;
printf("a=%dt b=%d",a,b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=10 y=20
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 26
27. Pass by reference(Call by reference)
• Both the actual and formal parameters refer to
same locations, so any changes made inside
the function are actually reflected in actual
parameters of caller.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 27
28. Example:
#include<stdio.h>
int swap(int *a,int *b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(&x,&y); //function call –pass by reference
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int *a,int *b) //function definition
{
int temp=*a;
*a=*b;
*b=temp;
printf("a=%dt b=%d",*a,*b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=20 y=10
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 28
29. Recursive Function
• A function that calls itself is known as a
recursive function. And, this technique is known as
recursion.
• While using recursion, programmers need to be
careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
• Recursion makes program elegant.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 29
30. Write a program to find factorial of number using recursive function
#include<stdio.h>
int fact(int n)
{
if( n == 0)
return 1;
return n*fact(n-1);
}
void main()
{
int n,f;
printf("Enter the number:");
scanf("%d",&n);
f-=fact(n);
printf("factorial=%d",f);
}
Output
Enter the number:5
factorial=120
fact(5) fact(4) fact(3) fact(2) fact(1) fact(0)
fact(5)
{
return
5*fact(4);
}
fact(4)
{
return
4*fact(3);
}
fact(3)
{
return
3*fact(2);
}
fact(2)
{
return
2*fact(1);
}
fact(1)
{
return
1*fact(0);
}
fact(0)
{
return 1;
}
5*24=120 4*6=24 3*2=6 2*1=2 1*1=1 1
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 30
31. Write a program to find sum of first n natural numbers using recursive function
#include<stdio.h>
int sum(int n)
{
if( n == 0)
return 0;
return n+sum(n-1);
}
void main()
{
int n,f;
printf("Enter the limit:");
scanf("%d",&n);
printf(“n Sum=%d",sum(n));
}
Output
Enter the limit:5
factorial=15
sum(5) sum(4) sum(3) sum(2) sum(1) sum(0)
sum(5)
{
return
5+sum(4);
}
sum(4)
{
return
4+sum(3);
}
sum(3)
{
return
3+sum(2);
}
sum(2)
{
return
2+sum(1);
}
sum(1)
{
return
1+sum(0);
}
sum(0)
{
return 0;
}
5+10=15 4+6=10 3+3=6 2+1=3 1+0=1 0
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 31
32. Passing an array as Parameter
Like the value of simple variables, it is also
possible to pass the values of an array to a
function.
To pass a single dimensional array to a
function, it is sufficient to list the name of the
array without any subscripts and the size of
the array as arguments
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 32
33. Rules to pass an Array to Function
The function must be called by passing only the
name of the array.
In function definition, formal parameter must be
an array type; the size of the array does not need
to be specified.
The function prototype must show that the
argument is an array.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 33
34. Write a function to sort an array.
#include<stdio.h>
int sort(int A[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1]= temp;
}
}
}
void main()
{
int A[30],i,n;
printf("Enter the limit:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the
element:");
scanf("%d",&A[i]);
}
sort(A,n);
printf("Sorted Arrayn");
for(i=0;i<n;i++)
printf("%dt",A[i]);
}
Output
Enter the limit:5
Enter the element:17
Enter the element:23
Enter the element:5
Enter the element:2
Enter the element:9
Sorted Array
2 5 9
17 23
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 34
35. • Passing 2 D array as parameter
• While passing 2D as parameter we need to
mention the max size of element of each row
that is column
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 35
36. Write a program to pass a 2 D matrix as parameter and find its sum of all the elements
#include<stdio.h>
// function that takes a 2 D
and its order
int sum(int A[][30],int m,int
n)
{
int i,j,sum =0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum = sum + A[i][j];
}
}
printf("Sum=%d",sum);
}
} void main()
{
int A[30][30];
int i,n,m,j;
printf("Enter the order of
matrix:");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the element:");
scanf("%d",&A[i][j]);
}
}
sum(A,m,n);
}
Output
Enter the order of
matrix:2 2
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:4
Sum=10
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 36
37. STRUCTURE
• A structure is a user defined data type in C.
• A structure creates a data type that can be
used to group items of possibly different types
into a single type.
• ‘struct’ keyword is used to create a structure.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 37
38. • Consider we want to create the structure of a
person with following variable name, age and
address.
• Then such a structure can be created as
struct Person
{
char name[30];
int age;
char addr[50];
}p;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 38
39. The general format of a structure definition is as
follows
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}S;
Here S is called structure variable.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 39
40. • A structure variable can either be declared
with structure declaration or as a separate
declaration like basic types.
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}:
void main()
{
struct structure_name S;
}
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}S;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 40
41. Declare a structure namely Student to store the details (roll number,
name, mark_for_C) of two students and display the data.
#include<stdio.h>
struct Student
{
char name[30];
int rollnum;
int mark;
};
void main()
{
struct Student s1,s2;
printf("Enter the Student name:");
scanf("%s",s1.name);
printf("Enter the Student rollnum:");
scanf("%d",&s1.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s1.mark);
printf("Enter the Student name:");
scanf("%s",s2.name);
printf("Enter the Student rollnum:");
scanf("%d",&s2.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s2.mark);
printf("NametRoll NumbertMark for Cn");
printf("%st%dt%dn",s1.name,s1.rollnum,s1.m
ark);
printf("%st%dt%dn",s2.name,s2.rollnum,s2.m
ark);
}
Output
Enter the Student name:Sangeeth
Enter the Student rollnum:
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 41
42. Array of Structures
DeclareastructurenamelyStudenttostorethedetails(roll number, name, mark_for_C)
of a student. Then, write a program in C to find the average mark obtained by the
students in a class for the subject Programming in C (using the field mark_for_C). Use
array of structures to store the required data.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 42
43. Declare a structure namely Student to store the details (roll number,
name, mark_for_C) of two students and display the data.
#include<stdio.h>
struct Student
{
char name[30];
int rollnum;
int mark;
};
void main()
{
struct Student s1,s2;
printf("Enter the Student name:");
scanf("%s",s1.name);
printf("Enter the Student rollnum:");
scanf("%d",&s1.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s1.mark);
printf("Enter the Student name:");
scanf("%s",s2.name);
printf("Enter the Student rollnum:");
scanf("%d",&s2.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s2.mark);
printf("NametRoll NumbertMark for Cn");
printf("%st%dt%dn",s1.name,s1.rollnum,s1.m
ark);
printf("%st%dt%dn",s2.name,s2.rollnum,s2.m
ark);
}
Output
Enter the Student name:Sangeeth
Enter the Student rollnum:
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 43
44. #include<stdio.h>
struct Student
{
char name[30];
int rollno;
int mark_for_C;
};
void main()
{
struct Student s[30];
int i,n,sum=0;
float avg;
printf("Enter the no
of Student:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Student
name:");
scanf("%s",s[i].name);
printf("Enter the Student
rollno:");
scanf("%d",&s[i].rollno);
printf("Enter the Mark for C:");
scanf("%d",&s[i].mark_for_C);
}
printf("NametRoll
NumbertMark for Cn");
for(i=0;i<n;i++)
{
printf("%st%dt%dn",s[i].name,
s[i].rollnum,s[i].mark_for_C);
sum = sum + s[i].mark_for_C;
}
avg = (float)sum /n;
printf("Average Mark=%fn",avg);
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 44
45. Output
Enter the no of Student:3
Enter the Student name:Sangeeth
Enter the Student rollnum:27
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Enter the Student name:Poornima
Enter the Student rollnum:26
Enter the Student Mark for C:45
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
Poornima 26 45
Average Mark=40.00
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 45
46. UNION
A union is a user-defined type similar to struct in C
programming.
We use the union keyword to define unions.
Example of Employee Union creation and declartion
union Employee
{
char name[30]; int age;
double salary;
};
union Employee e;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 46
47. Difference between structure and union
Structure Union
struct keyword is used to define a structure union keyword is used to define a union
Members do not share memory in a structure. Members share the memory space in a union
Any member can be retrieved at any time in a
structure
Only one member can be accessed at a time
in a union.
Several members of a structure can be
initialized at once.
Only the first member can be initialized.
Sizeofthestructureisequalto thesumofsize
of the each member.
Size of the union is equal to the size of the
largest member.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 47
48. #include<stdio.h>
struct Person
{
char pincode[6]; // Size = 6 bytes
int age; // Size = 4 bytes
double salary;// Size = 8 bytes
};
union Employee
{
char pincode[6];
int age;
double salary;
};
void main()
{
struct Person p;
union Employee e;
printf("Size of Structure Person=%dn",sizeof(p));
printf("Size of Union Employee=%d",sizeof(e));
}
Output
Size of Structure Person=18
Size of Union Employee=86/30/2020 NIMMY RAJU,AP,VKCET,TVM 48
50. • In C, not only do all the variables have a data
type, they also have a storage class.
• The following variable storage classes are
most relevant to functions
– Automatic Variables
– External or Global Variables
– Static Variables
– Register Variables
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 50
51. Automatic Variables
• Automatic variables are declared inside a
function in which they are to be utilized.
• They are created when the function is called
and destroyed automatically when the
function is exited, hence the name automatic.
• Automatic variables are therefore private or
local to the function in which they are
declared.
• Because of this property, automatic variables
are also referred to as local or internal
variables.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 51
52. Automatic Variables
• A variable declared inside a function without
storage class specification is by default an
automatic variable.
void main()
{
int num;
}
is same as
void main()
{
auto int num;
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 52
53. • Example
#include<stdio.h>
void func1()
{
int max=10;
printf("Max in func1()=%dn",max);
}
void func2()
{
int max=20;
printf("Max in func2()=%dn",max);
}
void main()
{
int max=30;
func1();
func2();
printf("Max in main()=%dn",max);
}
Output
Max in func1()=10
Max in func2()=20
Max in main()=30
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 53
54. External Variables
• Variables that are both alive and active
throughout the entire program are known as
external variables.
• They are called global variables.
• Unlike local variables, global variables can be
accessed by any function in the program.
• External global variables are global variables
visible to all file other than in which it is
declared6/30/2020 NIMMY RAJU,AP,VKCET,TVM 54
55. #include<stdio.h>
int max;
void func1()
{
printf("Max in func1()=%dn",max);
}
void func2()
{
printf("Max in func2()=%dn",max);
}
void main()
{
max=30;
func1();
func2();
printf("Max in main()=%dn",max);
}
Max in func1()=30
Max in func1()=30
Max in main()=30
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 55
56. Static Variables
• As the name suggests, the value of static
variables persists until the end of the
program.
• A variable can be declared static using the
keyword static like
static int x;
• Static global variables are global variables
visible only to the file in which it is declared.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 56
57. Register Variables
• We can tell the compiler that a variable should be
kept in one of the machine’s registers instead of
keeping in the memory.
• Since a register access is fast than a memory
access, keep in the frequently accessed variables
in the register will lead to faster execution of
programs.
• This is done as follows
register int count;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 57
58. • The scope of a variable is the part of the
program within which the variable can be
used. So, the scope describes the visibility of
an identifier within the program.
• The lifetime of a variable or function is the
time duration for which memory is allocated
to store it, and when that memory is released.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 58
59. StorageClass Wheredeclared Visibility Lifetime
extern
Beforeallfunctionsinafile
Entire file plus other
files where variable is
declared.
Global
Static
Beforeallthefunctioninafile Onlyinthatfile
Global
Noneorauto
Inside afunction
Only in that function
orblock
Until end of
function
Register
Inside afunctionorblock
Onlyinthatfunction
orblock
Until end of
function
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 59