Handout # 6 pointers and recursion in c++

NUST Stuff

c++ http://nustweb.blogspot.com/

LAB # 15 HANDOUT
Objectives:
Pointers:
Just like we can reference the cabinets of a cupboard with their order i.e. first cabinet or the second cabinet,
the memory in a computer can be referenced by address. These addresses can be viewed in HEX if we want.
By using pointers, we can use the addresses to manipulate the values of the variables.
Pointer are declared by doing int *pointer_name. Just like there can be different data types of variables, there
can different data types of pointers i.e. float pointer, double pointer, char pointer etc. Creating a pointer
reserves a slot in the memory that can store the address of another memory location.
Once we have created a pointer to store the address of another memory location, we now need to store the
address in it. This can be done by something we already know: using the &, we can save the address of a
variable in the memory slot that we have reserved earlier. The line of code that will accomplish this is: int
*my_ptr = &my_variable where my variable has been declared and initialized earlier.
In order to manipulate (or display) the data that a pointer points to can be accomplished by using the asterisk
sign with the pointer name i.e. cout<<*my_pointer. Similarly, the same syntax can be used to add or subtract
something that the pointer points to i.e. *my_ptr++. On the other hand, displaying the value of the pointer
itself will only display the value of the address i.e. cout<<my_pointer will display the address of the variable
that my_pointer holds.
Pointers can be passed to the functions and they can also be the return data type of a function. For example, I
can create a function with the prototype as: int * my_function (int *, float *). Whenever any pointer is passed,
it is the same as passing a value via variable.
The name of an array is a pointer to its first location.
Remember the values of the variables that we passed via reference? We can recreate that functionality using
pointers.
Practice Questions:
1. Create pointers for datatypes int, float, double, char. Then declare four variables of the datatypes that we
have declared the pointers for. Display the pointers and then the values that they are pointing to.
2. Write a function that takes an int pointer as an input and cubes it. The function shouldn’t return anything.
3. Create a variable of datatype float. Then check whether you can assign multiple pointers to this variable
or not. Is there any way to check it?
4. Write a function that takes two integer pointers as an input and switches the values that those two pointers
point to.
Recursion:
Up until now, we have used loops to do a task that needs to be done repeatedly. We can accomplish the same
thing using recursion. Whenever we intend to use recursion to solve a problem, we need to break down the
problem itself. In other words, we need divide the problems into sub problems and then we solve the original
problem by solving the sub parts.
Put simply, we write a solution that is capable of solving the simplest case of a problem. For any other case
of that problem, we simply call the function itself again until the simplest case is reached.
Some of the famous problems that can be solved with recursion are finding the factorial of a number (the
function is called again and again until we reach the base case), finding the Fibonacci Series (calling the
function again and again until the base case is met) etc.
Recursion can be used in place of loops but it may or may not result in a more efficient solution.
Practice Questions:
1. Write a function that computes the factorial of a number recursively.
2. Write a function that generates the Fibonacci series up to a given number.
3. Write a function that takes two arguments as an input and recursively computes the power of first number
to the second number i.e. function1 (a,b) where a = 2 and b =3.
4. Write a function that takes two arguments as input and multiplies them recursively.
5. Take an array as an input. Then write a recursive function that prints the array in reverse order.

Recomendados

Double pointer (pointer to pointer) por
Double pointer (pointer to pointer)Double pointer (pointer to pointer)
Double pointer (pointer to pointer)sangrampatil81
186 visualizações6 slides
Pointers In C por
Pointers In CPointers In C
Pointers In CSriram Raj
1.5K visualizações53 slides
Pointer in c por
Pointer in c Pointer in c
Pointer in c meet darji
133 visualizações12 slides
Arrays in c++ por
Arrays in c++Arrays in c++
Arrays in c++Asaye Dilbo
227 visualizações10 slides
Pointers in c v5 12102017 1 por
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1tanmaymodi4
667 visualizações32 slides
Fundamentals of Pointers in C por
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in CShivanshuVerma11
641 visualizações16 slides

Mais conteúdo relacionado

Mais procurados

Pointers por
PointersPointers
PointersProf. Dr. K. Adisesha
388 visualizações23 slides
Pointers-Computer programming por
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
62 visualizações11 slides
Functions-Computer programming por
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
81 visualizações19 slides
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun... por
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
10K visualizações19 slides
Pointers por
PointersPointers
PointersSwarup Kumar Boro
971 visualizações24 slides
Introduction to Python Basics por
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python BasicsRaghunath A
218 visualizações11 slides

Mais procurados(14)

Pointers-Computer programming por nmahi96
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi9662 visualizações
Functions-Computer programming por nmahi96
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi9681 visualizações
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun... por Jayanshu Gundaniya
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya10K visualizações
Introduction to Python Basics por Raghunath A
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
Raghunath A218 visualizações
5. struct, class, enum por Vahid Heidari
5. struct, class, enum5. struct, class, enum
5. struct, class, enum
Vahid Heidari93 visualizações
Pointers in c - Mohammad Salman por MohammadSalman129
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129585 visualizações
Introduction To Programming with Python-4 por Syed Farjad Zia Zaidi
Introduction To Programming with Python-4Introduction To Programming with Python-4
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi655 visualizações
Types of pointer in C por rgnikate
Types of pointer in CTypes of pointer in C
Types of pointer in C
rgnikate2.5K visualizações
oop lecture 2 por Atif Khan
oop lecture 2oop lecture 2
oop lecture 2
Atif Khan67 visualizações
Pointer in c por Imamul Kadir
Pointer in cPointer in c
Pointer in c
Imamul Kadir1K visualizações
Pointer in c por sangrampatil81
Pointer in cPointer in c
Pointer in c
sangrampatil81123 visualizações

Destaque

Using Drawing Tools and applied geometry por
Using Drawing Tools and applied geometryUsing Drawing Tools and applied geometry
Using Drawing Tools and applied geometryNUST Stuff
3.5K visualizações50 slides
Engineering Drawing: Chapter 12 working drawing por
Engineering Drawing: Chapter 12 working drawingEngineering Drawing: Chapter 12 working drawing
Engineering Drawing: Chapter 12 working drawingmokhtar
19K visualizações63 slides
Drg 5ab multiviews por
Drg 5ab multiviewsDrg 5ab multiviews
Drg 5ab multiviewsNUST Stuff
164 visualizações2 slides
Lec 5 engineering ethics por
Lec 5 engineering ethicsLec 5 engineering ethics
Lec 5 engineering ethicsNUST Stuff
2.7K visualizações51 slides
Essay Writing Simplified Secret Effective Shortcuts por
Essay Writing Simplified  Secret Effective ShortcutsEssay Writing Simplified  Secret Effective Shortcuts
Essay Writing Simplified Secret Effective ShortcutsEssayWriter.Co.Uk
2K visualizações19 slides
Engineering Ethics por
Engineering EthicsEngineering Ethics
Engineering Ethicsharoldtaylor1113
30K visualizações15 slides

Destaque(20)

Using Drawing Tools and applied geometry por NUST Stuff
Using Drawing Tools and applied geometryUsing Drawing Tools and applied geometry
Using Drawing Tools and applied geometry
NUST Stuff3.5K visualizações
Engineering Drawing: Chapter 12 working drawing por mokhtar
Engineering Drawing: Chapter 12 working drawingEngineering Drawing: Chapter 12 working drawing
Engineering Drawing: Chapter 12 working drawing
mokhtar19K visualizações
Drg 5ab multiviews por NUST Stuff
Drg 5ab multiviewsDrg 5ab multiviews
Drg 5ab multiviews
NUST Stuff164 visualizações
Lec 5 engineering ethics por NUST Stuff
Lec 5 engineering ethicsLec 5 engineering ethics
Lec 5 engineering ethics
NUST Stuff2.7K visualizações
Essay Writing Simplified Secret Effective Shortcuts por EssayWriter.Co.Uk
Essay Writing Simplified  Secret Effective ShortcutsEssay Writing Simplified  Secret Effective Shortcuts
Essay Writing Simplified Secret Effective Shortcuts
EssayWriter.Co.Uk2K visualizações
Engineering Ethics por haroldtaylor1113
Engineering EthicsEngineering Ethics
Engineering Ethics
haroldtaylor111330K visualizações
Engineering Drawing: Chapter 08 orthographic convention por mokhtar
Engineering Drawing: Chapter 08 orthographic conventionEngineering Drawing: Chapter 08 orthographic convention
Engineering Drawing: Chapter 08 orthographic convention
mokhtar12.4K visualizações
Ethics in engineering presentation por Ornella Ramsarran
Ethics in engineering presentationEthics in engineering presentation
Ethics in engineering presentation
Ornella Ramsarran7K visualizações
Lecture2 research &_methodology_chap2 por Dere2014
Lecture2 research &_methodology_chap2Lecture2 research &_methodology_chap2
Lecture2 research &_methodology_chap2
Dere2014359 visualizações
Engineeringdrawingi 090303074237-phpapp01 por Christopher Todd
Engineeringdrawingi 090303074237-phpapp01Engineeringdrawingi 090303074237-phpapp01
Engineeringdrawingi 090303074237-phpapp01
Christopher Todd5.5K visualizações
Rm 9 por Bob Bin
Rm 9Rm 9
Rm 9
Bob Bin429 visualizações
Rm 6 por Bob Bin
Rm  6Rm  6
Rm 6
Bob Bin543 visualizações
Rm 3 por Bob Bin
Rm 3Rm 3
Rm 3
Bob Bin543 visualizações
02asbe040 por Musa Dadi
02asbe04002asbe040
02asbe040
Musa Dadi1.3K visualizações
Engineering Drawing: Chapter 02 using drawing tools por mokhtar
Engineering Drawing: Chapter 02 using drawing toolsEngineering Drawing: Chapter 02 using drawing tools
Engineering Drawing: Chapter 02 using drawing tools
mokhtar9.9K visualizações
All posters 2015-2016 Period 5 por ResearchMethodsUCM
All posters 2015-2016 Period 5All posters 2015-2016 Period 5
All posters 2015-2016 Period 5
ResearchMethodsUCM1.5K visualizações
Engineering Ethics and Values por haroldtaylor1113
Engineering Ethics and ValuesEngineering Ethics and Values
Engineering Ethics and Values
haroldtaylor111320.3K visualizações
Research methodology por xoogsade
Research methodology   Research methodology
Research methodology
xoogsade3.3K visualizações
Basic mechanisms por Christopher Todd
Basic mechanismsBasic mechanisms
Basic mechanisms
Christopher Todd12.4K visualizações

Similar a Handout # 6 pointers and recursion in c++

Handout # 4 functions + scopes por
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
72 visualizações2 slides
e computer notes - Reference variables por
e computer notes - Reference variablese computer notes - Reference variables
e computer notes - Reference variablesecomputernotes
190 visualizações14 slides
C pointer por
C pointerC pointer
C pointerUniversity of Potsdam
6.8K visualizações29 slides
computer notes - Reference variables por
computer notes -  Reference variablescomputer notes -  Reference variables
computer notes - Reference variablesecomputernotes
425 visualizações14 slides
Pointers por
PointersPointers
PointersSwarup Boro
906 visualizações24 slides
Pointers in c language por
Pointers in c languagePointers in c language
Pointers in c languageTanmay Modi
832 visualizações32 slides

Similar a Handout # 6 pointers and recursion in c++(20)

Handout # 4 functions + scopes por NUST Stuff
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
NUST Stuff72 visualizações
e computer notes - Reference variables por ecomputernotes
e computer notes - Reference variablese computer notes - Reference variables
e computer notes - Reference variables
ecomputernotes190 visualizações
computer notes - Reference variables por ecomputernotes
computer notes -  Reference variablescomputer notes -  Reference variables
computer notes - Reference variables
ecomputernotes425 visualizações
Pointers por Swarup Boro
PointersPointers
Pointers
Swarup Boro906 visualizações
Pointers in c language por Tanmay Modi
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi832 visualizações
Programming in C sesion 2 por Prerna Sharma
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma974 visualizações
CS-XII Python Fundamentals.pdf por Ida Lumintu
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
Ida Lumintu9 visualizações
C++ Pointers with Examples.docx por JoeyDelaCruz22
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
JoeyDelaCruz2242 visualizações
Pointers in c++ por Asaye Dilbo
Pointers in c++Pointers in c++
Pointers in c++
Asaye Dilbo785 visualizações
Write a function named no_duplicate that accepts 2 parameters- The fir.docx por noreendchesterton753
Write a function named no_duplicate that accepts 2 parameters- The fir.docxWrite a function named no_duplicate that accepts 2 parameters- The fir.docx
Write a function named no_duplicate that accepts 2 parameters- The fir.docx
noreendchesterton7532 visualizações
Write a function named no_duplicate that accepts 2 parameters. The fi.docx por ajoy21
 Write a function named no_duplicate that accepts 2 parameters. The fi.docx Write a function named no_duplicate that accepts 2 parameters. The fi.docx
Write a function named no_duplicate that accepts 2 parameters. The fi.docx
ajoy212 visualizações
C programming perso notes por Melanie Tsopze
C programming perso notesC programming perso notes
C programming perso notes
Melanie Tsopze295 visualizações
C++ data types por pratikborsadiya
C++ data typesC++ data types
C++ data types
pratikborsadiya3.2K visualizações
Chapter 5 (Part I) - Pointers.pdf por TamiratDejene1
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene14 visualizações
Structured Languages por Mufaddal Nullwala
Structured LanguagesStructured Languages
Structured Languages
Mufaddal Nullwala216 visualizações
Lecture 4: Functions por Vivek Bhargav
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
Vivek Bhargav161 visualizações
functions- best.pdf por MikialeTesfamariam
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam215 visualizações

Mais de NUST Stuff

Me211 1 por
Me211 1Me211 1
Me211 1NUST Stuff
1K visualizações14 slides
Lab LCA 1 7 por
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7NUST Stuff
1.8K visualizações48 slides
Me211 2 por
Me211 2Me211 2
Me211 2NUST Stuff
1.7K visualizações26 slides
Me211 4 por
Me211 4Me211 4
Me211 4NUST Stuff
154 visualizações1 slide
Me211 3 por
Me211 3Me211 3
Me211 3NUST Stuff
621 visualizações4 slides
Drag force on a sphere in yield stress fluid por
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidNUST Stuff
214 visualizações3 slides

Mais de NUST Stuff(20)

Me211 1 por NUST Stuff
Me211 1Me211 1
Me211 1
NUST Stuff1K visualizações
Lab LCA 1 7 por NUST Stuff
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7
NUST Stuff1.8K visualizações
Me211 2 por NUST Stuff
Me211 2Me211 2
Me211 2
NUST Stuff1.7K visualizações
Me211 4 por NUST Stuff
Me211 4Me211 4
Me211 4
NUST Stuff154 visualizações
Me211 3 por NUST Stuff
Me211 3Me211 3
Me211 3
NUST Stuff621 visualizações
Drag force on a sphere in yield stress fluid por NUST Stuff
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
NUST Stuff214 visualizações
Nums entry test 2017 paper por NUST Stuff
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
NUST Stuff13K visualizações
Nums entry-test-new-syllabus-mbbsc-bds por NUST Stuff
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
NUST Stuff515 visualizações
MCAT Full length paper 8-student_copy_ por NUST Stuff
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
NUST Stuff1.1K visualizações
MCAT Full length paper 7-student_copy_ por NUST Stuff
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
NUST Stuff514 visualizações
MCAT Full length paper 6-student_copy_ por NUST Stuff
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
NUST Stuff628 visualizações
MCAT Full length paper 5-student_copy_ por NUST Stuff
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
NUST Stuff784 visualizações
Mcat (original paper 2014) por NUST Stuff
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
NUST Stuff916 visualizações
MCAT Full length paper 4-student_copy por NUST Stuff
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
NUST Stuff578 visualizações
mcat (original paper 2013) por NUST Stuff
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
NUST Stuff1.1K visualizações
mcat (original paper 2012) por NUST Stuff
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
NUST Stuff473 visualizações
MCAT Full length paper 3 final por NUST Stuff
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
NUST Stuff168 visualizações
MCAT (original paper 2011) por NUST Stuff
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
NUST Stuff355 visualizações
MCAT Full length paper 2 (student copy) por NUST Stuff
MCAT Full length paper  2 (student copy)MCAT Full length paper  2 (student copy)
MCAT Full length paper 2 (student copy)
NUST Stuff374 visualizações
mcat (original paper 2010) por NUST Stuff
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
NUST Stuff446 visualizações

Último

MercerJesse2.1Doc.pdf por
MercerJesse2.1Doc.pdfMercerJesse2.1Doc.pdf
MercerJesse2.1Doc.pdfjessemercerail
273 visualizações5 slides
Women from Hackney’s History: Stoke Newington by Sue Doe por
Women from Hackney’s History: Stoke Newington by Sue DoeWomen from Hackney’s History: Stoke Newington by Sue Doe
Women from Hackney’s History: Stoke Newington by Sue DoeHistory of Stoke Newington
163 visualizações21 slides
Drama KS5 Breakdown por
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 BreakdownWestHatch
98 visualizações2 slides
Gross Anatomy of the Liver por
Gross Anatomy of the LiverGross Anatomy of the Liver
Gross Anatomy of the Liverobaje godwin sunday
61 visualizações12 slides
Dance KS5 Breakdown por
Dance KS5 BreakdownDance KS5 Breakdown
Dance KS5 BreakdownWestHatch
99 visualizações2 slides
UNIDAD 3 6º C.MEDIO.pptx por
UNIDAD 3 6º C.MEDIO.pptxUNIDAD 3 6º C.MEDIO.pptx
UNIDAD 3 6º C.MEDIO.pptxMarcosRodriguezUcedo
134 visualizações32 slides

Último(20)

MercerJesse2.1Doc.pdf por jessemercerail
MercerJesse2.1Doc.pdfMercerJesse2.1Doc.pdf
MercerJesse2.1Doc.pdf
jessemercerail273 visualizações
Women from Hackney’s History: Stoke Newington by Sue Doe por History of Stoke Newington
Women from Hackney’s History: Stoke Newington by Sue DoeWomen from Hackney’s History: Stoke Newington by Sue Doe
Women from Hackney’s History: Stoke Newington by Sue Doe
History of Stoke Newington163 visualizações
Drama KS5 Breakdown por WestHatch
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch98 visualizações
Gross Anatomy of the Liver por obaje godwin sunday
Gross Anatomy of the LiverGross Anatomy of the Liver
Gross Anatomy of the Liver
obaje godwin sunday61 visualizações
Dance KS5 Breakdown por WestHatch
Dance KS5 BreakdownDance KS5 Breakdown
Dance KS5 Breakdown
WestHatch99 visualizações
UNIDAD 3 6º C.MEDIO.pptx por MarcosRodriguezUcedo
UNIDAD 3 6º C.MEDIO.pptxUNIDAD 3 6º C.MEDIO.pptx
UNIDAD 3 6º C.MEDIO.pptx
MarcosRodriguezUcedo134 visualizações
Psychology KS4 por WestHatch
Psychology KS4Psychology KS4
Psychology KS4
WestHatch98 visualizações
Class 9 lesson plans por TARIQ KHAN
Class 9 lesson plansClass 9 lesson plans
Class 9 lesson plans
TARIQ KHAN51 visualizações
MIXING OF PHARMACEUTICALS.pptx por Anupkumar Sharma
MIXING OF PHARMACEUTICALS.pptxMIXING OF PHARMACEUTICALS.pptx
MIXING OF PHARMACEUTICALS.pptx
Anupkumar Sharma95 visualizações
Pharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptx por Ms. Pooja Bhandare
Pharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptxPharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptx
Pharmaceutical Inorganic chemistry UNIT-V Radiopharmaceutical.pptx
Ms. Pooja Bhandare113 visualizações
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx por ISSIP
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxEIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
ISSIP386 visualizações
ICS3211_lecture 08_2023.pdf por Vanessa Camilleri
ICS3211_lecture 08_2023.pdfICS3211_lecture 08_2023.pdf
ICS3211_lecture 08_2023.pdf
Vanessa Camilleri231 visualizações
Solar System and Galaxies.pptx por DrHafizKosar
Solar System and Galaxies.pptxSolar System and Galaxies.pptx
Solar System and Galaxies.pptx
DrHafizKosar106 visualizações
Education and Diversity.pptx por DrHafizKosar
Education and Diversity.pptxEducation and Diversity.pptx
Education and Diversity.pptx
DrHafizKosar193 visualizações
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx por Debapriya Chakraborty
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Debapriya Chakraborty695 visualizações
Structure and Functions of Cell.pdf por Nithya Murugan
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan719 visualizações

Handout # 6 pointers and recursion in c++

  • 1. LAB # 15 HANDOUT Objectives: Pointers: Just like we can reference the cabinets of a cupboard with their order i.e. first cabinet or the second cabinet, the memory in a computer can be referenced by address. These addresses can be viewed in HEX if we want. By using pointers, we can use the addresses to manipulate the values of the variables. Pointer are declared by doing int *pointer_name. Just like there can be different data types of variables, there can different data types of pointers i.e. float pointer, double pointer, char pointer etc. Creating a pointer reserves a slot in the memory that can store the address of another memory location. Once we have created a pointer to store the address of another memory location, we now need to store the address in it. This can be done by something we already know: using the &, we can save the address of a variable in the memory slot that we have reserved earlier. The line of code that will accomplish this is: int *my_ptr = &my_variable where my variable has been declared and initialized earlier. In order to manipulate (or display) the data that a pointer points to can be accomplished by using the asterisk sign with the pointer name i.e. cout<<*my_pointer. Similarly, the same syntax can be used to add or subtract something that the pointer points to i.e. *my_ptr++. On the other hand, displaying the value of the pointer itself will only display the value of the address i.e. cout<<my_pointer will display the address of the variable that my_pointer holds. Pointers can be passed to the functions and they can also be the return data type of a function. For example, I can create a function with the prototype as: int * my_function (int *, float *). Whenever any pointer is passed, it is the same as passing a value via variable. The name of an array is a pointer to its first location. Remember the values of the variables that we passed via reference? We can recreate that functionality using pointers. Practice Questions: 1. Create pointers for datatypes int, float, double, char. Then declare four variables of the datatypes that we have declared the pointers for. Display the pointers and then the values that they are pointing to. 2. Write a function that takes an int pointer as an input and cubes it. The function shouldn’t return anything. 3. Create a variable of datatype float. Then check whether you can assign multiple pointers to this variable or not. Is there any way to check it? 4. Write a function that takes two integer pointers as an input and switches the values that those two pointers point to. Recursion: Up until now, we have used loops to do a task that needs to be done repeatedly. We can accomplish the same thing using recursion. Whenever we intend to use recursion to solve a problem, we need to break down the problem itself. In other words, we need divide the problems into sub problems and then we solve the original problem by solving the sub parts.
  • 2. Put simply, we write a solution that is capable of solving the simplest case of a problem. For any other case of that problem, we simply call the function itself again until the simplest case is reached. Some of the famous problems that can be solved with recursion are finding the factorial of a number (the function is called again and again until we reach the base case), finding the Fibonacci Series (calling the function again and again until the base case is met) etc. Recursion can be used in place of loops but it may or may not result in a more efficient solution. Practice Questions: 1. Write a function that computes the factorial of a number recursively. 2. Write a function that generates the Fibonacci series up to a given number. 3. Write a function that takes two arguments as an input and recursively computes the power of first number to the second number i.e. function1 (a,b) where a = 2 and b =3. 4. Write a function that takes two arguments as input and multiplies them recursively. 5. Take an array as an input. Then write a recursive function that prints the array in reverse order.