Cucerzan Rares
Cucerzan Rares

Reputation: 73

Custom filter function does not produce expected output

I am trying to make a filter that prints on the screen only the data I want. Here's what I am talking about:

Cost* FilterSum(Controller* ctrl, int n)
{
    int i;
    DynamicVector* CostList=getAll(ctrl->repo);
    for(i=0;i<getLen(CostList);i++)
    {
        Cost* c=getElementAtPosition(CostList,i); //returns the element on one position
        if((c->sum)<n)
        {
            return c; //if the element(Cost in my case) has the sum<20 return it
        }
    }


return 0;
}

So, I have Dynamic Array with Costs as elements. If the sum of the cost is less than n(n is given from the keyboard) I want to print on the screen only those costs. :) This are the print functions in the console:

void PrintCost(Cost* c) //this function prints one cost
{
    printf("\nID: %d\n", getID(c));
    printf("Day: %s\n", getDay(c));
    printf("Type: %s\n", getType(c));
    printf("Sum: %d\n\n", getSum(c));
}

void PrintCosts(Console* console) //this one prints all the costs in the list
{
    DynamicVector* CostList=getAllCosts(console->ctrl);
    if (getLen(CostList))
    {
        int i;
        for(i=0;i<getLen(CostList);i++)
        {
            Cost *c=(Cost*)getElementAtPosition(CostList,i);
            PrintCost(c);
        }

    }
    else printf("No cost in the list!");
}

And here is the call function from controller in the console:

void FilterCostsBySum(Console* console)
{
    int n;
    printf("See the costs that have the sum less than: ");
    scanf("%d",&n);
    Cost* c = FilterSum(console->ctrl,n);
    PrintCost(c);
}

Now, here comes the problem. If I have Monday with sum=10, Friday with sum=20 and Saturday with sum=40 and I want to print only those days with sum<30, it just prints Monday and that's it, it doesn't print Friday too. Where am I doing wrong? In the FilterSum function in the controller where I return the c? I tried everything and it didn't work at all... maybe you can help me! :)

Upvotes: 0

Views: 95

Answers (3)

a3.14_Infinity
a3.14_Infinity

Reputation: 5861

if((c->sum)<n)
{
  return c; //if the element(Cost in my case) has the sum<20 return it
}

This will immediately return only 10. Instead of immediately returning, add 10 to a list. Then when you obtain 20, add it to the list. Once for loop in FilterSum() is done, then you can return this list.

Upvotes: 0

user123
user123

Reputation: 9071

It's only printing one because you're only executing the PrintCost function once after getting one of the valid costs via FilterSum. You need to make your FilterCostsBySum function loop and print costs pushed to a DynamicVector.

Write a function that returns a DynamicVector containing all the costs satisfying the condition you want. You would do this by changing the FilterSum function so that instead of returning one Cost, it would add any cost that satisfies a given condition to a DynamicVector and return it. Rename the function GetFilteredCosts afterwards.

Finally, inside the FilterCostsBySum function, you would loop over the elements in the returned DynamicVector and print the costs.

Upvotes: 2

SJuan76
SJuan76

Reputation: 24895

if((c->sum)<n)
    {
        return c; //if the element(Cost in my case) has the sum<20 return it
    }

The return statements breaks the loop and exits the FilterSum function, so the function just returns the first Cost which matches the condition. You should change it to return a Const**, an pointer to a list of Const *.

Upvotes: 1

Related Questions