SlideShare a Scribd company logo
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Recursion
Dr. Yousaf, PIEAS
Recursion
Example: factorials
– 5! = 5 * 4 * 3 * 2 * 1
– Notice that
• 5! = 5 * 4!
• 4! = 4 * 3! ...
– Can compute factorials recursively
– Solve base case (1! = 0! = 1) then plug in
• 2! = 2 * 1! = 2 * 1 = 2;
• 3! = 3 * 2! = 3 * 2 = 6;
Write a recursive function int fact(int num) that should return
the factorial of given number num
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
//Calculate Factorial using Recursion
# include<stdio.h>
int factorial(int num);
int main ()
{
int f,x;
printf("Enter integer value in the range 1
to 10: ");
scanf("%d", &x);
if (x > 10 || x < 1)
printf ("Illegal input!n");
else
{
f = factorial(x);
printf ("%d factorial equals %dn", x,f);
}
getchar(); return 0; }
int factorial (int a)
{
if (a==1) // base case
return 1;
else
{
a *= factorial(a-1);
return a;
}
}
Recursion
• Recursive functions
• A function that calls itself inside its body is called a
recursive function
• Can only solve a base case
– Divide a problem into
• What it can do
• What it cannot do
– What it cannot do resembles original problem
– The function launches a new copy of itself (recursion
step) to solve what it cannot do
– Eventually base case gets solved
• Gets plugged in, works its way up and solves whole problem
Dr. Yousaf, PIEAS
Example Using Recursion
/*This function prints all the number between zero and a given number that is
greater than zero. */
#include <stdio.h>
void printSeries(int num);
int main()
{
printSeries(6);
getchar(); return 0;
}
void printSeries(int num)
{
if(num<=0)
return;
printf("%d", num);
printSeries(num-1);
}
Dr. Yousaf, PIEAS
Example Using Recursion
/*This function prints all the number between zero and a given number that is
greater than zero. */
#include <stdio.h>
void printSeries(int num);
int main()
{
printSeries(6);
getchar(); return 0;
}
void printSeries(int num)
{
if(num<=0)
return;
printf("%d", num);
printSeries(num-1);
}
Dr. Yousaf, PIEAS
Example Using Recursion:
The Fibonacci Series
• Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, ...
– Each number is the sum of the previous two
– fib( n ) = fib( n - 1 ) + fib( n – 2 )
– Write a recursive function that can print first N terms of
Fibonacci series ,
– Can be solved recursively:
Code for the fibaonacci function
int fibonacci(int n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci(n - 1)+ fibonacci(n – 2);
}
Dr. Yousaf, PIEAS
Example Using Recursion:
The Fibonacci Series
• Set of recursive calls to function fibonacci
f( 3 )
f( 1 )f( 2 )
f( 1 ) f( 0 ) return 1
return 1 return 0
return +
+return
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
// To generate Fibonacci series
using Recursion
#include<stdio.h>
int fibonacci(int n);
int main()
{
int i, terms, result;
printf("Please enter the number of
terms from 1 to 20: ");
scanf("%d",&terms);
for ( i = 0; i<= terms; i++)
{
result = fibonacci(i);
printf("%d, ", result);
}
getchar(); return 0; }
// Function Definition
int fibonacci(int n)
{
if ( n == 0 || n == 1 )
return n;
else
return fibonacci( n - 1 ) +
fibonacci( n - 2 );
}
Recursion vs. Iteration
• Repetition
– Iteration: explicit loop
– Recursion: repeated function calls
• Termination
– Iteration: loop condition fails
– Recursion: base case recognized
Dr. Yousaf, PIEAS
POINTERS
Dr. Yousaf, PIEAS
/* Demonstration on Pointers
A very simple program to print a value of a variable
without using pointer */
#include<stdio.h>
int main()
{
int x = 20;
printf("n x = %d", x);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
// A very simple program to print the address and value of
a variable using pointer
#include<stdio.h>
int main()
{
int x = 20;
int *ptrx; //Pointer is declared. Select any name instead of ptrx.
ptrx = &x; // Initialization of the pointer
printf("n address of x = %d", ptrx);
// It would print the address value
printf("n Value of x = %d",*ptrx);
/* It would print the value of the variable stored at the
address value of the pointer */
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
• Pointers are powerful and very useful tools
• With pointers we can effectively return more than one
value from a function
• Arrays are a form of pointer REALLY
• Pass by reference is also possible
Dr. Yousaf, PIEAS
The Importance of Pointers
The Importance of Pointers
• Pointers can be used for advanced data structures.
• Pointers can be "cheaper" to pass around a
program.
• You could program without using them but you
would be making life more difficult for yourself.
• Some things simply can't be done sensibly in C
without them.
Dr. Yousaf, PIEAS
What are pointers?
• Pointers are considered as one of the most
difficult topics to understand in C.
• Pointers "point at" areas of your computer's
memory (address of the memory)
• int *p; says p is a pointer to an int
• Imagine your computer's memory as a series of
boxes which all hold ints
56 71 12 3 21 7
p points at one of the ints
Dr. Yousaf, PIEAS
& means "address of" or "point at
me"
* means value or "what am I
pointing at?"
int *p; // p is a pointer to an int
int q= 5; // q is an int
p= &q; // p now points at q
printf (“The value stored at p location is
%dn",*p); // 5
/*We use *p to mean "the value in the memory where pointer p is pointing
at*/
q=5
p
Dr. Yousaf, PIEAS
& means "address of" or "point at
me"
* means value or "what am I
pointing at?"
int *p; // p is a pointer to an int
int q= 5; // q is an int
p= &q; // p now points at q
printf (“The value stored at p location is
%dn",*p); // 5
*p= 9;
printf (“%dn",q); // 9
printf (“The value stored at p location is
%dn",*p); // 9
Dr. Yousaf, PIEAS
p= &q; // p now points at q
This is what was going on when we use & in scanf
int x;
scanf(“%d”, &x);
Dr. Yousaf, PIEAS
& means "address of" or "point at
Pointer Variable Declarations and
Initialization
• Pointer variables
– Normal variables contain a specific value (direct reference)
– int count = 7;
– Pointer contains address of a variable that has a specific value
(indirect reference)
– Indirection – referencing a pointer value
– int *countPtr
– countPtr = &count
count
7
count
7
countPtr
Dr. Yousaf, PIEAS
Pointer Variable Declarations and
Initialization
• Pointer declarations
– * used with pointer variables
int *myPtr;
– Declares a pointer to an int (pointer of type int *)
– Multiple pointers require using a * before each variable
declaration
int *myPtr1, *myPtr2;
– Can declare pointers to any data type
Dr. Yousaf, PIEAS
Pointer Variables
• Pointer variables are variables that store memory
addresses.
• Pointer Declaration:
– int x, y = 5;
– int *ptr;
– /*ptr is a POINTER to an integer variable*/
• Reference operator &:
– ptr = &y;
– /*assign ptr to the MEMORY ADDRESS of y.*/
• Dereference operator *:
– x = *ptr;
– /*assign x to the int that is pointed to by ptr */
Dr. Yousaf, PIEAS
Pointer Variables
int x;
int y = 5;
int *ptry;
ptr = &y;
x = *ptry;
printf(“%d”, x); //5 ptry
Dr. Yousaf, PIEAS
Pointer Variables
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int x = 9;
int *ptrx = 0;
ptrx = &x;
printf("address of x = %dn",ptrx);
printf("Value in ptrx is %d", *ptrx);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
double x = 7.8;
double *ptrx = 0;
ptrx = &x;
printf("address of x = %dn",ptrx);
printf("Value in ptrx is %lf", *ptrx);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
Pointer Operators
• & (address operator)
– Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y; // yPtr gets address of y
yPtr “points to” y
yPtr
y
5
yptr
500000 600000
y
600000 5
Address of y
is value of
yptr
Dr. Yousaf, PIEAS
Pointer Operators
• * (indirection/dereferencing operator)
– Returns a synonym/alias of what its operand points to
– *yptr returns y (because yptr points to y)
– * can be used for assignment
• Returns alias to an object
int y = 5;
int *yPtr;
yPtr = &y;
*yptr = 7; // changes y to 7
Dr. Yousaf, PIEAS
Revision
• We declare a pointer with a *
• We use an & to get the "address of" and save this
address in a pointer variable
• We use a * to get the value "pointed at“
int *p;
int q = 5;
printf ("q is %dn",q); // 5
p= &q;
printf (“%dn",*p); //5
*p= 6;
printf ("q is now %dn",q); // 6
printf (“%dn",*p); //6
Dr. Yousaf, PIEAS

More Related Content

What's hot

Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Unit2 control statements
Unit2 control statementsUnit2 control statements
Unit2 control statements
deepak kumbhar
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
MomenMostafa
 
Buffer OverFlow
Buffer OverFlowBuffer OverFlow
Buffer OverFlow
Rambabu Duddukuri
 
Python iteration
Python iterationPython iteration
Python iteration
dietbuddha
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Kevlin Henney
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
Robert Lujo
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
Implementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & CImplementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & C
Eleanor McHugh
 
C programming slide c04
C programming slide c04C programming slide c04
C programming slide c04
pradeep dwivedi
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Mohammad Usman
 
Get Kata
Get KataGet Kata
Get Kata
Kevlin Henney
 
TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用
Mark Chang
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Pratt Parser in Python
Pratt Parser in PythonPratt Parser in Python
Pratt Parser in Python
Percolate
 

What's hot (20)

Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
Unit2 control statements
Unit2 control statementsUnit2 control statements
Unit2 control statements
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
C tech questions
C tech questionsC tech questions
C tech questions
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Buffer OverFlow
Buffer OverFlowBuffer OverFlow
Buffer OverFlow
 
Python iteration
Python iterationPython iteration
Python iteration
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
7 functions
7  functions7  functions
7 functions
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Implementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & CImplementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & C
 
C programming slide c04
C programming slide c04C programming slide c04
C programming slide c04
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Get Kata
Get KataGet Kata
Get Kata
 
TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Pratt Parser in Python
Pratt Parser in PythonPratt Parser in Python
Pratt Parser in Python
 

Similar to C Language Lecture 19

C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
Shahzaib Ajmal
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
Shahzaib Ajmal
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
Shahzaib Ajmal
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
Shahzaib Ajmal
 
Pointer
PointerPointer
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
Shahzaib Ajmal
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
Shahzaib Ajmal
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
Krishna Nanda
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
dhanajimirajkar1
 
C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
Shahzaib Ajmal
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
Shahzaib Ajmal
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Pointers [compatibility mode]
Pointers [compatibility mode]Pointers [compatibility mode]
Pointers [compatibility mode]
Kathmandu University
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
Shahzaib Ajmal
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
Shahzaib Ajmal
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
Shahzaib Ajmal
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
NewsMogul
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
Garrett Gutierrez
 

Similar to C Language Lecture 19 (20)

C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
Pointer
PointerPointer
Pointer
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers [compatibility mode]
Pointers [compatibility mode]Pointers [compatibility mode]
Pointers [compatibility mode]
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 

More from Shahzaib Ajmal

C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
Shahzaib Ajmal
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
Shahzaib Ajmal
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
Shahzaib Ajmal
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
Shahzaib Ajmal
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
Shahzaib Ajmal
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
Shahzaib Ajmal
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
Shahzaib Ajmal
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
Shahzaib Ajmal
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
Shahzaib Ajmal
 

More from Shahzaib Ajmal (9)

C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 

Recently uploaded (20)

MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 

C Language Lecture 19

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 3. Recursion Example: factorials – 5! = 5 * 4 * 3 * 2 * 1 – Notice that • 5! = 5 * 4! • 4! = 4 * 3! ... – Can compute factorials recursively – Solve base case (1! = 0! = 1) then plug in • 2! = 2 * 1! = 2 * 1 = 2; • 3! = 3 * 2! = 3 * 2 = 6; Write a recursive function int fact(int num) that should return the factorial of given number num Dr. Yousaf, PIEAS
  • 4. Dr. Yousaf, PIEAS //Calculate Factorial using Recursion # include<stdio.h> int factorial(int num); int main () { int f,x; printf("Enter integer value in the range 1 to 10: "); scanf("%d", &x); if (x > 10 || x < 1) printf ("Illegal input!n"); else { f = factorial(x); printf ("%d factorial equals %dn", x,f); } getchar(); return 0; } int factorial (int a) { if (a==1) // base case return 1; else { a *= factorial(a-1); return a; } }
  • 5. Recursion • Recursive functions • A function that calls itself inside its body is called a recursive function • Can only solve a base case – Divide a problem into • What it can do • What it cannot do – What it cannot do resembles original problem – The function launches a new copy of itself (recursion step) to solve what it cannot do – Eventually base case gets solved • Gets plugged in, works its way up and solves whole problem Dr. Yousaf, PIEAS
  • 6. Example Using Recursion /*This function prints all the number between zero and a given number that is greater than zero. */ #include <stdio.h> void printSeries(int num); int main() { printSeries(6); getchar(); return 0; } void printSeries(int num) { if(num<=0) return; printf("%d", num); printSeries(num-1); } Dr. Yousaf, PIEAS
  • 7. Example Using Recursion /*This function prints all the number between zero and a given number that is greater than zero. */ #include <stdio.h> void printSeries(int num); int main() { printSeries(6); getchar(); return 0; } void printSeries(int num) { if(num<=0) return; printf("%d", num); printSeries(num-1); } Dr. Yousaf, PIEAS
  • 8. Example Using Recursion: The Fibonacci Series • Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, ... – Each number is the sum of the previous two – fib( n ) = fib( n - 1 ) + fib( n – 2 ) – Write a recursive function that can print first N terms of Fibonacci series , – Can be solved recursively: Code for the fibaonacci function int fibonacci(int n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci(n - 1)+ fibonacci(n – 2); } Dr. Yousaf, PIEAS
  • 9. Example Using Recursion: The Fibonacci Series • Set of recursive calls to function fibonacci f( 3 ) f( 1 )f( 2 ) f( 1 ) f( 0 ) return 1 return 1 return 0 return + +return Dr. Yousaf, PIEAS
  • 10. Dr. Yousaf, PIEAS // To generate Fibonacci series using Recursion #include<stdio.h> int fibonacci(int n); int main() { int i, terms, result; printf("Please enter the number of terms from 1 to 20: "); scanf("%d",&terms); for ( i = 0; i<= terms; i++) { result = fibonacci(i); printf("%d, ", result); } getchar(); return 0; } // Function Definition int fibonacci(int n) { if ( n == 0 || n == 1 ) return n; else return fibonacci( n - 1 ) + fibonacci( n - 2 ); }
  • 11. Recursion vs. Iteration • Repetition – Iteration: explicit loop – Recursion: repeated function calls • Termination – Iteration: loop condition fails – Recursion: base case recognized Dr. Yousaf, PIEAS
  • 13. /* Demonstration on Pointers A very simple program to print a value of a variable without using pointer */ #include<stdio.h> int main() { int x = 20; printf("n x = %d", x); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 14. // A very simple program to print the address and value of a variable using pointer #include<stdio.h> int main() { int x = 20; int *ptrx; //Pointer is declared. Select any name instead of ptrx. ptrx = &x; // Initialization of the pointer printf("n address of x = %d", ptrx); // It would print the address value printf("n Value of x = %d",*ptrx); /* It would print the value of the variable stored at the address value of the pointer */ getchar(); return 0; } Dr. Yousaf, PIEAS
  • 16. • Pointers are powerful and very useful tools • With pointers we can effectively return more than one value from a function • Arrays are a form of pointer REALLY • Pass by reference is also possible Dr. Yousaf, PIEAS The Importance of Pointers
  • 17. The Importance of Pointers • Pointers can be used for advanced data structures. • Pointers can be "cheaper" to pass around a program. • You could program without using them but you would be making life more difficult for yourself. • Some things simply can't be done sensibly in C without them. Dr. Yousaf, PIEAS
  • 18. What are pointers? • Pointers are considered as one of the most difficult topics to understand in C. • Pointers "point at" areas of your computer's memory (address of the memory) • int *p; says p is a pointer to an int • Imagine your computer's memory as a series of boxes which all hold ints 56 71 12 3 21 7 p points at one of the ints Dr. Yousaf, PIEAS
  • 19. & means "address of" or "point at me" * means value or "what am I pointing at?" int *p; // p is a pointer to an int int q= 5; // q is an int p= &q; // p now points at q printf (“The value stored at p location is %dn",*p); // 5 /*We use *p to mean "the value in the memory where pointer p is pointing at*/ q=5 p Dr. Yousaf, PIEAS
  • 20. & means "address of" or "point at me" * means value or "what am I pointing at?" int *p; // p is a pointer to an int int q= 5; // q is an int p= &q; // p now points at q printf (“The value stored at p location is %dn",*p); // 5 *p= 9; printf (“%dn",q); // 9 printf (“The value stored at p location is %dn",*p); // 9 Dr. Yousaf, PIEAS
  • 21. p= &q; // p now points at q This is what was going on when we use & in scanf int x; scanf(“%d”, &x); Dr. Yousaf, PIEAS & means "address of" or "point at
  • 22. Pointer Variable Declarations and Initialization • Pointer variables – Normal variables contain a specific value (direct reference) – int count = 7; – Pointer contains address of a variable that has a specific value (indirect reference) – Indirection – referencing a pointer value – int *countPtr – countPtr = &count count 7 count 7 countPtr Dr. Yousaf, PIEAS
  • 23. Pointer Variable Declarations and Initialization • Pointer declarations – * used with pointer variables int *myPtr; – Declares a pointer to an int (pointer of type int *) – Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2; – Can declare pointers to any data type Dr. Yousaf, PIEAS
  • 24. Pointer Variables • Pointer variables are variables that store memory addresses. • Pointer Declaration: – int x, y = 5; – int *ptr; – /*ptr is a POINTER to an integer variable*/ • Reference operator &: – ptr = &y; – /*assign ptr to the MEMORY ADDRESS of y.*/ • Dereference operator *: – x = *ptr; – /*assign x to the int that is pointed to by ptr */ Dr. Yousaf, PIEAS
  • 25. Pointer Variables int x; int y = 5; int *ptry; ptr = &y; x = *ptry; printf(“%d”, x); //5 ptry Dr. Yousaf, PIEAS
  • 27. #include<stdio.h> int main() { int x = 9; int *ptrx = 0; ptrx = &x; printf("address of x = %dn",ptrx); printf("Value in ptrx is %d", *ptrx); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 29. #include<stdio.h> int main() { double x = 7.8; double *ptrx = 0; ptrx = &x; printf("address of x = %dn",ptrx); printf("Value in ptrx is %lf", *ptrx); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 31. Pointer Operators • & (address operator) – Returns address of operand int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y yPtr “points to” y yPtr y 5 yptr 500000 600000 y 600000 5 Address of y is value of yptr Dr. Yousaf, PIEAS
  • 32. Pointer Operators • * (indirection/dereferencing operator) – Returns a synonym/alias of what its operand points to – *yptr returns y (because yptr points to y) – * can be used for assignment • Returns alias to an object int y = 5; int *yPtr; yPtr = &y; *yptr = 7; // changes y to 7 Dr. Yousaf, PIEAS
  • 33. Revision • We declare a pointer with a * • We use an & to get the "address of" and save this address in a pointer variable • We use a * to get the value "pointed at“ int *p; int q = 5; printf ("q is %dn",q); // 5 p= &q; printf (“%dn",*p); //5 *p= 6; printf ("q is now %dn",q); // 6 printf (“%dn",*p); //6 Dr. Yousaf, PIEAS