Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming assignment, please answer the following questions 1.What recursi

ID: 3845304 • Letter: C

Question

C++ Programming assignment, please answer the following questions

1.What recursion is? How is it implemented in computer languages?

2. What are the basic elements of a recursive function, how to write a simple recursive function, identify some wrong behavior/problem with a given function, and be able to tell what a given function computes?.

3.What are the basics of friend functions and operator overloading. You will NOT be asked to implement one from scratch but you should be comfortable answering basic questions on what we have done?

Explanation / Answer

1.

Ans:-A function that calls itself is known as recursive function. And, this technique is known as recursion.

A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task.

implemention code

2)

Ans;-

A recursive method is a method that contains a statement (or statements) that makes a call to itself. First we implement a mathematical function using recursion.

Any recursive method will include the following three basic elements:

1. A test to stop or continue the recursion.

2. An end case that terminates the recursion.

3. A recursive call(s) that continues the recursion.

Using recursive algorithm, certain problems can be solved quite easily. Towers of Hanoi (TOH) is one such programming exercise. Try to write an iterative algorithm for TOH. Moreover, every recursive program can be written using iterative methods (Refer Data Structures by Lipschutz).

Mathematically recursion helps to solve few puzzles easily.

For example, a routine interview question,

In a party of N people, each person will shake her/his hand with each other person only once. On total how many hand-shakes would happen?

There are N persons. Each person shake-hand with each other only once. Considering N-th person, (s)he has to shake-hand with (N-1) persons. Now the problem reduced to small instance of (N-1) persons. Assuming TN as total shake-hands, it can be formulated recursively.

TN = (N-1) + TN-1 [T1 = 0, i.e. the last person have already shook-hand with every one]

Solving it recursively yields an arithmetic series, which can be evaluated to N(N-1)/2.

Most of us aware atleast two different ways of writing recursive programs. Given below is towers of Hanoi code. It is an example of direct calling.

// Assuming n-th disk is bottom disk (count down)

void tower(int n, char sourcePole, char destinationPole, char auxiliaryPole)

{

   // Base case (termination condition)

   if(0 == n)

     return;

   // Move first n-1 disks from source pole

   // to auxiliary pole using destination as

   // temporary pole

   tower(n-1, sourcePole, auxiliaryPole,

      destinationPole);

// Move the remaining disk from source

   // pole to destination pole

   printf("Move the disk %d from %c to %c ", n,

     sourcePole, destinationPole);

   // Move the n-1 disks from auxiliary (now source)

   // pole to destination pole using source pole as

   // temporary (auxiliary) pole

   tower(n-1, auxiliaryPole, destinationPole,

     sourcePole);

}

void main()

{

   tower(3, 'S', 'D', 'A');

}

3)

Ans;-

Friend Functions

A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class. A friend class has full access of private data members of another class without being member of that class.

In C++ a function or an entire class may be declared to be a friend of another class or function. Friend function can also be used for overloading functions.

A C++ friend functions are special functions which can access the private members of a class. They are considered to be a loophole in the Object Oriented Programming concepts, but logical use of them can make them useful in certain cases. For instance: when it is not possible to implement some function, without making private members accessible in them. This situation arises mostly in case of operator overloading.

Friend functions have the following properties:

operator overloading

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type.

Following are some restrictions to be kept in mind while implementing operator overloading.

// Assuming n-th disk is bottom disk (count down)

void tower(int n, char sourcePole, char destinationPole, char auxiliaryPole)

{

   // Base case (termination condition)

   if(0 == n)

     return;

   // Move first n-1 disks from source pole

   // to auxiliary pole using destination as

   // temporary pole

   tower(n-1, sourcePole, auxiliaryPole,

      destinationPole);

// Move the remaining disk from source

   // pole to destination pole

   printf("Move the disk %d from %c to %c ", n,

     sourcePole, destinationPole);

   // Move the n-1 disks from auxiliary (now source)

   // pole to destination pole using source pole as

   // temporary (auxiliary) pole

   tower(n-1, auxiliaryPole, destinationPole,

     sourcePole);

}

void main()

{

   tower(3, 'S', 'D', 'A');

}

3)

Ans;-

Friend Functions

A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class. A friend class has full access of private data members of another class without being member of that class.

In C++ a function or an entire class may be declared to be a friend of another class or function. Friend function can also be used for overloading functions.

A C++ friend functions are special functions which can access the private members of a class. They are considered to be a loophole in the Object Oriented Programming concepts, but logical use of them can make them useful in certain cases. For instance: when it is not possible to implement some function, without making private members accessible in them. This situation arises mostly in case of operator overloading.

Friend functions have the following properties:

  • 1) Friend of the class can be member of some other class.
  • 2) Friend of one class can be friend of another class or all the classes in one program, such a friend is known as GLOBAL FRIEND.
  • 3) Friend can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object.
  • 4) Friends are non-members hence do not get “this” pointer.
  • 5) Friends, can be friend of more than one class, hence they can be used for message passing between the classes.
  • 6) Friend can be declared anywhere (in public, protected or private section) in the class.

operator overloading

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type.

Following are some restrictions to be kept in mind while implementing operator overloading.

  • Precedence and Associativity of an operator cannot be changed.
  • Arity (numbers of Operands) cannot be changed. Unary operator remains unary, binary remains binary etc.
  • No new operators can be created, only existing operators can be overloaded.
  • Cannot redefine the meaning of a procedure. You cannot change how integers are added.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote