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

I am woking to make average number on recursion. struct node { int data; node *

ID: 3568364 • Letter: I

Question

I am woking to make average number on recursion.

         

struct node
{
      int data;
      node * next;
};

         

Prototype

int avg_num(node * head)

        

in main()

index = 0;

index = avg_num(head);

cout << index;

      

int avg_num(node * head)

{

   if(!head)

        return 0;

   else

   {

          int index;

          int num = 1;

          num += avg_num(head->next);

          index = head -> data + avg_num(head_next);

          index /= num;

          return index:

     }

}

When I run program, I cannot get correct avrage number. Do I need make two function(count node, sum node data) or can i do it same function??

}

Explanation / Answer

There is no need of making different functions for finding the average. Average can be calculated using single with small corrections made in code. Check with the code given below for the average function.

/*Program*/

struct node

{

      int data;

      node * next;

};     

int avg_num(node * head)  

int main()

{

    index = 0;

    index = avg_num(head);

    cout << index;

}

  

    int avg_num(node * head)

    {

       if(!head)

            return 0;

       else

       {        

          int num = 1;

          num += avg_num(head->next);

          index = head -> data + avg_num(head->next);

          index /= num;       

          return index;

         }

    }

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