Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4338102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:02:11+00:00 2026-05-21T11:02:11+00:00

#include <iostream> using namespace std; int g(float A[] , int L , int H)

  • 0
#include <iostream>
using namespace std;

int g(float A[] , int L , int H)
{
   if (L==H)
      if (A[L] > 0.0)
         return 1;
      else 
         return 0;
   int M = (L+H)/2;

   return g(A,L,M)+ g(A,M+1,H);
}
int main (void)
{       
   float A[] = {-1.5 ,3.1,-5.2,0.0};
   g(A,0,3);

   system ("pause");
   return 0;
}

its asking me what is return by the function g and what the function does

here is what i got so far

first call is g(A , 0 ,3)
-total skip the IF statement and M = 1 since it its a int
-return g(A,1,3) + g(A,2 3)

second call
– g(A,1,3) skipp the if statement again
– M = 0;
– g(A,2 3) skip the if statement again
– M= 2;

third call
-g(A, 0,0,)
return 0
-g(A,3,3)
return 0;

so it just return 0?

and i am guessing it is dividing the middle value and some sort of binary search?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:02:11+00:00Added an answer on May 21, 2026 at 11:02 am

    It’s a convoluted way to count how many numbers in the array is greater than 0. And if you try to run this in a compiler, the return value is 1 because the only number that is greater than 0 in the array is 3.1.

    at first run:

    {-1.5, 3.1, -5.2, 0.0}
       0    1     2    3
       L               H
    

    then since L=0 and H=3, M = (0+3)/2 = 3/2 = 1 when you get to g(A, L, M) + g(A, M+1, H), you branch into two:

    {-1.5, 3.1, -5.2, 0.0}
       0    1     2    3
       L               H
       L1   H1    L2   H2
    

    let’s do the left part g(A, L1, H1) = g(A, 0, 1) first:

    {-1.5, 3.1, -5.2, 0.0}
       0    1     2    3
       L               H
       L1   H1    L2   H2
       ^^^^^^^
    

    again since L1=0, H1=1, and so M1 = (0+1)/2 = 1/2 = 0 and you branch into two again g(A, 0, 0) and g(A, 1, 1):

    {-1.5,    3.1,    -5.2, 0.0}
       0       1        2    3
       L               H
       L1      H1      L2    H2
    L11,H11 L12,H12
    

    on the left part, since -1.5 <= 0 therefore g(A, L11, H11) = g(A, 0, 0) = 0, on the right part, since 3.1 > 0 therefore g(A, L12, H12) = g(A, 1, 1) = 1.

    So therefore g(A, 0, 1) = g(A, 0, 0) + g(A, 1, 1) = 1.

    Do the same with g(A, L2, H2), and you get that g(A, L, H) = g(A, L1, H1) + g(A, L2, H2) = 1 + 0 = 1.

    @Nawaz had a good idea of visualizing this into a binary tree, basically you start with at the root of the tree:

    {-1.5, 3.1, -5.2, 0.0}
    

    At the second layer of iteration, you split the array into two:

         {-1.5, 3.1, -5.2, 0.0}
                  /   \
                 /     \
                /       \
               /         \
       {-1.5, 3.1}    {-5.2, 0.0}
    

    At the third layer, you split again:

         {-1.5, 3.1, -5.2, 0.0}
                  /   \
                 /     \
                /       \
               /         \
       {-1.5, 3.1}    {-5.2, 0.0}
          /   \          /   \
         /     \        /     \
     {-1.5}   {3.1}  {-5.2}   {0.0}
    

    At this point L==H so, we can evaluate the nodes:

         {-1.5, 3.1, -5.2, 0.0}
                  /   \
                 /     \
                /       \
               /         \
       {-1.5, 3.1}    {-5.2, 0.0}
          /   \          /   \
         /     \        /     \
     {-1.5}   {3.1}  {-5.2}   {0.0}
        |       |       |       |
        0       1       0       0
    

    and to find the return values, we sum up:

         {-1.5, 3.1, -5.2, 0.0}
                  /   \
                 /     \
                /       \
               /         \
       {-1.5, 3.1}    {-5.2, 0.0}
          0+1=1          0+0=0
    

    and lastly

         {-1.5, 3.1, -5.2, 0.0}
                 1+0=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { double u = 0; double w
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this program in c++: #include <iostream> using namespace std; int main() {
Just ran into this: #include <iostream> using namespace std; int main(int argc, char** argv)
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
I'm trying to compile such code: #include <iostream> using namespace std; class CPosition {
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
What's wrong with the following snippet ? #include <tr1/functional> #include <functional> #include <iostream> using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.