Fun with Functions

My solution to 3rd problem in Fun with Functions, any critic welcome
Task Description:
★★★ Make a function called half() that takes an integer argument. The function must print the number it received to the screen, then the program should divide that number by two to make a new number. If the new number is greater than zero the function then calls the function half() passing it the new number as its argument. If the number is zero or less than the function exits

Call the function half() with an argument of 100, the screen output should be
100
50
25
...
...
1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

  //Function declared as requested in question
void half(int x=100){
    cout<<x<<endl;
   
//Loop created to pass new value 

    for (;x>0;){
        if (x/2>0){

//New value pasted to function argument to continue calculations     
    
     x = x/2;
     cout<<x<<endl;
        }else{
            
//Stopping program according to conditions specified in loop
   
     break;
            }
    }
  }


int main()
{
  
//Calling Function as requested by task

   half();
}
1
2
3
4
5
6
7
8
#include <iostream>

void half(int x) {
  for (int i = x; i > 0; i /= 2)
    std::cout << i << '\n';
}

int main() { half(100); }


Edit:
Oops, I didn't read the problem description. (Nor did OP!)

1
2
3
4
5
6
7
8
9
#include <iostream>

void half(int x) {
  std::cout << x << '\n'; 
  if (( x /= 2 ) > 0) half( x );
}

int main() { half(100); }
Last edited on
If the new number is greater than zero the function then calls the function half()


This is asking for a recursive function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

void half(int n)
{
    if(n == 0)
        return;
    std::cout << n << std::endl;
    half(n/2);	
}

int main()
{
    half(100);
}


Technically <=0 should be the end case, but I don't think that's what they meant.
We will never get zero as this is clearly division so any number being constantly divided by 2 will go in to decimals.
So I have taken approach that when the function calculates and the division is zero with reminder then it stops at 1 at least that was what the task was showing in expected result.

I do not know recursive yet so will dig into other code presented in comments later today and see how it works.
Thanks for commenting :)
We will never get zero as this is clearly division so any number being constantly divided by 2 will go in to decimals.

How many decimals are there in an int?

int divided by int produces int. No decimals. Test it if you don't believe.
We will never get zero as this is clearly division so any number being constantly divided by 2 will go in to decimals.


Just the problem description states

If the number is zero or less than the function exits


So technically it should be <= 0. But not sure if the question was just worded wrong :)
Last edited on
Topic archived. No new replies allowed.