简体   繁体   中英

How do I do the following recursive function?

Ok, so I have a regular Node list, with members info and next.

I need to use a function, recursively, to calculate the average, and then compare if each node is bigger than the average or not.

int Acount(NodeType* Node, int sum, int& avg){

    if (Node == NULL){//last call
        avg = sum / avg;
        return 0;
    }
    else {
        return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0));
        }
}

Which is quite simple. Problem is the value returned is always 0. The problem appears to be with

(Node->info > avg ? 1 : 0));

I've done the tests and when I do the following:

return (Acount(Node->next, sum + Node->info, ++avg) + Node->info;

or

return (Acount(Node->next, sum + Node->info, ++avg) + avg;

Results meet expectations. As in, I'm getting the sum of the Node->info in the first case, and I'm getting average*number of nodes in the second case.

Point of this, I've proved that the function is working perfectly.

Yet when it comes to

(Node->info > avg ? 1 : 0));

Appears to be problematic, which is quite peculiar. if I place for example:

(Node->info == 5 ? 1 : 0));

And there is only one 5 in the nodes, then the function returns 1. So everything is working as intended, yet I keep getting a 0.

The following are the main functions and additional functions for the Node.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct NodeType{
    int info;
    NodeType *next;
};
//pre: first node passed is not NULL
int Acount(NodeType* Node, int sum, int& avg){

    if (Node == NULL){//last call
        avg = sum / avg;
        return 0;
    }
    else {
        return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0));
        }
}
void fill(NodeType*& Node){

    NodeType *temp;
    Node = new NodeType;
    Node->info = 0;
    Node->next = NULL;
    temp = Node;

    for (int i = 1; i < 10; i++){
        temp->next = new NodeType;
        temp = temp->next;
        temp->info = i;
        temp->next = NULL;
    }
}
void print(NodeType* Node){
    NodeType *temp = Node;
    while (temp != NULL){
        cout << temp->info << " ";
        temp = temp->next;
    }
    cout << endl;
}
void Delete(NodeType* Node){
    NodeType *temp;
    while (Node != NULL){
        temp = Node;
        Node = Node->next;
        delete temp;
    }
}
void main(){

    int sum = 0, avg = 0;
    NodeType *Node;
    fill(Node);
    print(Node);

    cout << Acount(Node, sum, avg) << endl;

    Delete(Node);


}

I am not sure if your code has defined behaviour. But, this line

return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0));

depends on if the left summand or the right summand is calculated first.

If it is the left one, then Acount goes down the recursion an incrementing avg until avg equals the number of elements in the list (here 10 when starting from zero called by the main routine). Note, that avg is passed by reference. Thus, when the recursion goes back up, this term in the right summand

Node->info > avg

will never be true because Node->info is set in the fill routine to values smaller then the number of elements.

In C++ there is no concept of left-to-right (or right-to-left) evaluation order of expressions. Operator priorities will control associativity, but in the case of f1() + f2() there is no guarantee that f1() is invoked before f2() (and viceversa). It may depend on the compiler or other.

My suggestion is to split the expression into 2 distinct statements as follows:

int tmp = Acount(Node->next, sum + Node->info, ++avg);
return tmp + (Node->info > avg ? 1 : 0);

I don't think your method will work.

In this statement:

return (Acount(Node->next, sum + Node->info, ++avg) + (Node->info > avg ? 1 : 0))

You don't know when the second term has be evaluated. It's not defined in C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM