SlideShare a Scribd company logo
1 of 5
Download to read offline
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
Offline Assignment 2
1. The following recursive code finds out the maximum value from an array of integers. [4]
#include <iostream>
using namespace std;
int max_elm(int arr[], int sz){
///if the array contains only 1 element then that element is the maximum itself
if(sz==1){
return arr[0];
}
else{
///keeping the last array element for myself
int mypart=arr[sz-1];
///forwarding the rest of the array elements to my friend to search for maximum element
int friendmax=max_elm(arr,sz-1);
if(mypart>friendmax){
return mypart;
}
else{
return friendmax;
}
}
}
int main()
{
int arr[]={1,3,-100,1000, 999};
int sz=sizeof(arr)/sizeof(int);
cout<<"The maximum is: "<<max_elm(arr,sz)<<endl;
return 0;
}
Now first try to understand the above problem and its solution, and then
write a C/C++ code that will take input an integer and will return the maximum digit found within that integer.
Sample Input Sample Output
1956 9
2410 4
2. The following recursive code checks whether a string is palindrome or not. [4]
#include <iostream>
#include <string>
using namespace std;
bool is_palindrome(string s, int tracker){
int first_ind=tracker;
int last_ind=s.size()-1-tracker;
///if the first index crosses the last index then we have nothing to check i.e. default palindrome
if(first_ind>last_ind){
return true;
}
else{
///keeping the first and last characters for myself
char firstchar=s[first_ind];
char lastchar=s[last_ind];
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
///forwarding the remaining intermediate portion to my friend
bool friend_decision=is_palindrome(s,tracker+1);
if(friend_decision==true && firstchar==lastchar){
return true;
}
else{
return false;
}
}
}
int main()
{
string s1="madam", s2="abda";
cout<< is_palindrome(s1, 0) <<endl;
cout<< is_palindrome(s2, 0) <<endl;
return 0;
}
Now first try to understand the above problem and its solution, and then
write a C/C++ code that will take input an array of integers and will check whether the array is palindrome or not.
Sample Input Sample Output
{1,2,3,2,1} 1
{1,0,1,0} 0
3. The following recursive code finds out the GCD of two integers. [4]
#include <iostream>
using namespace std;
int two_gcd(int dividend, int divisor){
///if the divisor is zero then the dividend is the gcd solution
if(divisor==0){
return dividend;
}
else{
///calculating the new dividend and divisor value
int new_dividend=divisor;
int new_divisor=dividend%divisor;
///forwarding the new dividend and new divisor to a friend for gcd calculation
int friend_gcd=two_gcd(new_dividend,new_divisor);
return friend_gcd;
}
}
int main()
{
cout << two_gcd(17,13) << endl;
cout << two_gcd(25,15) << endl;
return 0;
}
Now first try to understand the above problem and its solution.
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
We can calculate the GCD of n integers as follow:
GCD(a1, a2, a3, a4, … , an) = GCD( GCD(a1, a2, a3, a4, … , an-1), an )
meaning if we know the GCD of the remaining (n-1) integers then the GCD of all the n integers is the GCD between
the last integer (an) and the GCD of remaining (n-1) integers.
So, each time we will keep the last integer (an) for ourself and ask our friend to find out the GCD of the remaining (n-
1) integers. Our final solution will be the GCD between our last integer (an) and our friends returned result. We can
directly calculate the GCD without any recursive call when the array contains only 2 elements.
Code template: implement the n_gcd() function according to the comments. Remember, the dividend value must be
greater than the divisor value in two_gcd() function.
#include <iostream>
using namespace std;
int two_gcd(int dividend, int divisor){
///if the divisor is zero then the dividend is the gcd solution
if(divisor==0){
return dividend;
}
else{
///calculating the new dividend and divisor value
int new_dividend=divisor;
int new_divisor=dividend%divisor;
///forwarding the new dividend and divisor to a friend for gcd calculation
int friend_gcd=two_gcd(new_dividend,new_divisor);
return friend_gcd;
}
}
///implement this function
int n_gcd(int arr[], int sz){
///if array size is 2 then directly calculate the gcd between arr[0] and arr[1]
///call two_gcd() to calculate the gcd of two integers
if(...){
}
else{
///reserve the last array integer for yourself
///call your friend to find out the remaining array integers gcd value
///your gcd result is the gcd between your reserved integer and your friend gcd result
///call two_gcd() for calculating the gcd value between your integer and friend result
///return the calculated gcd value
}
}
int main()
{
int arr[]={8,6,4,2}
int sz=sizeof(arr)/sizeof(int);
cout<< n_gcd(arr,sz) <<endl; ///output will be: 2
return 0;
}
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
4. The following recursive code calculates the number of moves necessary for Tower of Hanoi problem with 1
intermediate pole. [4]
How TOH works:
#include <iostream>
using namespace std;
int TOH(int n, char src, char dest, char tmp){
///if you have only 1 disk left then directly move the disk from src to dest i.e. 1 move required
if(n==1){
return 1;
}
else{
///first friend will move the top n-1 disks from src to tmp
int friend1moves=TOH(n-1, src, tmp, dest);
///then I will move the remaining last 1 disk from src to dest
int mymove=TOH(1,src,dest,tmp);
///then second friend will move back the n-1 disks from tmp to dest pole
int friend2moves=TOH(n-1,tmp,dest,src);
///so total moves required
int totalmoves=friend1moves+mymove+friend2moves;
return totalmoves;
}
}
int main()
{
cout << TOH(6, 'S', 'D', 'T') << endl;
return 0;
}
Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU.
Now first try to understand the above problem and its solution.
Write a C/C++ code that will implement the Tower of Hanoi problem but with 2 intermediate poles. Working
mechanism is described below:
The given poles are: src_pole, dest_pole, tmp_pole1, tmp_pole2
First, you will move the top (n-2) disks from src_pole to tmp_pole1
then, you will move the next 1 disk from src_pole to tmp_pole2
then, you will move the last 1 disk from src_pole to dest_pole
then, you will move back the 1 disk from tmp_pole2 to dest_pole
then, you will move back all the (n-2) disks from tmp_pole1 to dest_pole.
5. Suppose you are given a distance of n units and you can move either 1 unit/2 units/3 units at a time. You need to
find out the total number of ways you can cover that distance. [4]
Hints:
• If you choose 1 unit to cover in 1 way, then forward the rest (n-1) units to your friends.
Total no of ways = 1 * friend_ways
• If you choose 2 units to cover in 1 way, then forward the rest (n-2) units to your friends.
Total no of ways = 1 * friend_ways
• If you choose 3 units to cover in 1 way, then forward the rest (n-3) units to your friends.
Total no of ways = 1 * friend_ways
Finally, you can cover the whole distance by summing all the results you have achieved in every ways.
If the distance is 0, then you will know that you have finished covering that distance in 1 way.
If the distance is negative, then you will know that you can’t cover that distance so return 0 way.
Sample Input Sample Output
3 4
4 7
[Miss at your own risk]
[Any kind of plagiarism is strictly prohibited.]

More Related Content

Similar to DS & Algo 2 - Offline Assignment 2

i have written ths code as per your requirements with clear comments.pdf
i have written ths code as per your requirements with clear comments.pdfi have written ths code as per your requirements with clear comments.pdf
i have written ths code as per your requirements with clear comments.pdfanandf0099
 
The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf4babies2010
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docxCalculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docxssuserd02b23
 
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docxCalculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docxssuserd02b23
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptxKarthikVijay59
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersAnimesh Chaturvedi
 
Numerical Algorithm for a few Special Functions
Numerical Algorithm for a few Special FunctionsNumerical Algorithm for a few Special Functions
Numerical Algorithm for a few Special FunctionsAmos Tsai
 
Special trigonometric integrals
Special trigonometric integralsSpecial trigonometric integrals
Special trigonometric integralsMazharul Islam
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxvaishnavi339314
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfaptcomputerzone
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
C language sample test
C language sample testC language sample test
C language sample testSompal Duhan
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
 
Given below is the completed code along with comments. Output of the.pdf
Given below is the completed code along with comments. Output of the.pdfGiven below is the completed code along with comments. Output of the.pdf
Given below is the completed code along with comments. Output of the.pdfaparnacollection
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 

Similar to DS & Algo 2 - Offline Assignment 2 (20)

i have written ths code as per your requirements with clear comments.pdf
i have written ths code as per your requirements with clear comments.pdfi have written ths code as per your requirements with clear comments.pdf
i have written ths code as per your requirements with clear comments.pdf
 
The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docxCalculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docx
 
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docxCalculate_distance_and_bearing_between Latitude_Longitude_Points.docx
Calculate_distance_and_bearing_between Latitude_Longitude_Points.docx
 
Nov 02 P2
Nov 02 P2Nov 02 P2
Nov 02 P2
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptx
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answers
 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
 
Numerical Algorithm for a few Special Functions
Numerical Algorithm for a few Special FunctionsNumerical Algorithm for a few Special Functions
Numerical Algorithm for a few Special Functions
 
Special trigonometric integrals
Special trigonometric integralsSpecial trigonometric integrals
Special trigonometric integrals
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
 
Functions
FunctionsFunctions
Functions
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
C language sample test
C language sample testC language sample test
C language sample test
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
 
Given below is the completed code along with comments. Output of the.pdf
Given below is the completed code along with comments. Output of the.pdfGiven below is the completed code along with comments. Output of the.pdf
Given below is the completed code along with comments. Output of the.pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 

More from Mohammad Imam Hossain

DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchMohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaMohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaMohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckMohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 
TOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal FormTOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal Form
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

DS & Algo 2 - Offline Assignment 2

  • 1. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. Offline Assignment 2 1. The following recursive code finds out the maximum value from an array of integers. [4] #include <iostream> using namespace std; int max_elm(int arr[], int sz){ ///if the array contains only 1 element then that element is the maximum itself if(sz==1){ return arr[0]; } else{ ///keeping the last array element for myself int mypart=arr[sz-1]; ///forwarding the rest of the array elements to my friend to search for maximum element int friendmax=max_elm(arr,sz-1); if(mypart>friendmax){ return mypart; } else{ return friendmax; } } } int main() { int arr[]={1,3,-100,1000, 999}; int sz=sizeof(arr)/sizeof(int); cout<<"The maximum is: "<<max_elm(arr,sz)<<endl; return 0; } Now first try to understand the above problem and its solution, and then write a C/C++ code that will take input an integer and will return the maximum digit found within that integer. Sample Input Sample Output 1956 9 2410 4 2. The following recursive code checks whether a string is palindrome or not. [4] #include <iostream> #include <string> using namespace std; bool is_palindrome(string s, int tracker){ int first_ind=tracker; int last_ind=s.size()-1-tracker; ///if the first index crosses the last index then we have nothing to check i.e. default palindrome if(first_ind>last_ind){ return true; } else{ ///keeping the first and last characters for myself char firstchar=s[first_ind]; char lastchar=s[last_ind];
  • 2. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. ///forwarding the remaining intermediate portion to my friend bool friend_decision=is_palindrome(s,tracker+1); if(friend_decision==true && firstchar==lastchar){ return true; } else{ return false; } } } int main() { string s1="madam", s2="abda"; cout<< is_palindrome(s1, 0) <<endl; cout<< is_palindrome(s2, 0) <<endl; return 0; } Now first try to understand the above problem and its solution, and then write a C/C++ code that will take input an array of integers and will check whether the array is palindrome or not. Sample Input Sample Output {1,2,3,2,1} 1 {1,0,1,0} 0 3. The following recursive code finds out the GCD of two integers. [4] #include <iostream> using namespace std; int two_gcd(int dividend, int divisor){ ///if the divisor is zero then the dividend is the gcd solution if(divisor==0){ return dividend; } else{ ///calculating the new dividend and divisor value int new_dividend=divisor; int new_divisor=dividend%divisor; ///forwarding the new dividend and new divisor to a friend for gcd calculation int friend_gcd=two_gcd(new_dividend,new_divisor); return friend_gcd; } } int main() { cout << two_gcd(17,13) << endl; cout << two_gcd(25,15) << endl; return 0; } Now first try to understand the above problem and its solution.
  • 3. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. We can calculate the GCD of n integers as follow: GCD(a1, a2, a3, a4, … , an) = GCD( GCD(a1, a2, a3, a4, … , an-1), an ) meaning if we know the GCD of the remaining (n-1) integers then the GCD of all the n integers is the GCD between the last integer (an) and the GCD of remaining (n-1) integers. So, each time we will keep the last integer (an) for ourself and ask our friend to find out the GCD of the remaining (n- 1) integers. Our final solution will be the GCD between our last integer (an) and our friends returned result. We can directly calculate the GCD without any recursive call when the array contains only 2 elements. Code template: implement the n_gcd() function according to the comments. Remember, the dividend value must be greater than the divisor value in two_gcd() function. #include <iostream> using namespace std; int two_gcd(int dividend, int divisor){ ///if the divisor is zero then the dividend is the gcd solution if(divisor==0){ return dividend; } else{ ///calculating the new dividend and divisor value int new_dividend=divisor; int new_divisor=dividend%divisor; ///forwarding the new dividend and divisor to a friend for gcd calculation int friend_gcd=two_gcd(new_dividend,new_divisor); return friend_gcd; } } ///implement this function int n_gcd(int arr[], int sz){ ///if array size is 2 then directly calculate the gcd between arr[0] and arr[1] ///call two_gcd() to calculate the gcd of two integers if(...){ } else{ ///reserve the last array integer for yourself ///call your friend to find out the remaining array integers gcd value ///your gcd result is the gcd between your reserved integer and your friend gcd result ///call two_gcd() for calculating the gcd value between your integer and friend result ///return the calculated gcd value } } int main() { int arr[]={8,6,4,2} int sz=sizeof(arr)/sizeof(int); cout<< n_gcd(arr,sz) <<endl; ///output will be: 2 return 0; }
  • 4. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. 4. The following recursive code calculates the number of moves necessary for Tower of Hanoi problem with 1 intermediate pole. [4] How TOH works: #include <iostream> using namespace std; int TOH(int n, char src, char dest, char tmp){ ///if you have only 1 disk left then directly move the disk from src to dest i.e. 1 move required if(n==1){ return 1; } else{ ///first friend will move the top n-1 disks from src to tmp int friend1moves=TOH(n-1, src, tmp, dest); ///then I will move the remaining last 1 disk from src to dest int mymove=TOH(1,src,dest,tmp); ///then second friend will move back the n-1 disks from tmp to dest pole int friend2moves=TOH(n-1,tmp,dest,src); ///so total moves required int totalmoves=friend1moves+mymove+friend2moves; return totalmoves; } } int main() { cout << TOH(6, 'S', 'D', 'T') << endl; return 0; }
  • 5. Mohammad Imam Hossain, Lecturer, Dept. of CSE, UIU. Now first try to understand the above problem and its solution. Write a C/C++ code that will implement the Tower of Hanoi problem but with 2 intermediate poles. Working mechanism is described below: The given poles are: src_pole, dest_pole, tmp_pole1, tmp_pole2 First, you will move the top (n-2) disks from src_pole to tmp_pole1 then, you will move the next 1 disk from src_pole to tmp_pole2 then, you will move the last 1 disk from src_pole to dest_pole then, you will move back the 1 disk from tmp_pole2 to dest_pole then, you will move back all the (n-2) disks from tmp_pole1 to dest_pole. 5. Suppose you are given a distance of n units and you can move either 1 unit/2 units/3 units at a time. You need to find out the total number of ways you can cover that distance. [4] Hints: • If you choose 1 unit to cover in 1 way, then forward the rest (n-1) units to your friends. Total no of ways = 1 * friend_ways • If you choose 2 units to cover in 1 way, then forward the rest (n-2) units to your friends. Total no of ways = 1 * friend_ways • If you choose 3 units to cover in 1 way, then forward the rest (n-3) units to your friends. Total no of ways = 1 * friend_ways Finally, you can cover the whole distance by summing all the results you have achieved in every ways. If the distance is 0, then you will know that you have finished covering that distance in 1 way. If the distance is negative, then you will know that you can’t cover that distance so return 0 way. Sample Input Sample Output 3 4 4 7 [Miss at your own risk] [Any kind of plagiarism is strictly prohibited.]