Fun with Functions part 3

Write a function titled say_hello() that outputs to the screen "Hello"

★ Modify the function so that it takes an integer argument and says hello a number of times equal to the value passed to it.

★★ Make another function that takes two integers arguments and then returns an integer that is the product of the two integers.
(i.e., integer1: 4, Integer2: 5 returns: 20)

★★★ 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.

I'm doing the third part to this excercise. I keep getting a failed response when I debug it. Could someone just take a gander at my function Half()? I'd greatly appreciate anyone's imput! thanks

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Fun with Functions.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void Say_hello(unsigned int x) //one integer argument
	{
		for(unsigned int i = 0; i < x; i ++)
		{
		cout << "hello\n"; //repeats output of "hello" 
		}
	}

void Say_Hello_Again(unsigned int x, unsigned int y) //two integer argument
{
	for(unsigned int i = 0; i <= x*y; i ++)
	{
		cout << "hello\n"; //repeats output of "hello" equal to the product of the two integers
	}
}

void half(signed int x)
{
	cin >> x;
	cout << x << "\n";
	x/2;
	if (x/2 > 0)
	{
		half(x);
	}
	if(x/2 <= 0)
	{
		exit;
	}
}

class KeepRunning
{
public:
	~KeepRunning()
	{
		system("pause");
		Sleep(5); 
	}
};
	

int _tmain(int argc, _TCHAR* argv[])
{
	KeepRunning kr;
	half(x);
	/*Say_hello(50);*/
	//Say_Hello_Again(2,50);
	system("PAUSE");
}
I don't think you want to use exit here. This keyword terminates the process, and all you want to do is return from the function.

Instead of exit, try return.

if(x/2 <= 0)
{
return;
}
It underlines the half(x); in red, and says it's undefined. I can't give it a value yet because the user is supposed to do that.
glancing quickly at this, I'd rather see something more like ...

int half(int x) // returns the integer value of x
{
cin >> x;
cout << x << "\n";
if (x/2 > 0)
return x/2;
else return x;
}


Well, just a thought.
You may wish to set x to 0 before returning if <0, however.
Last edited on
closed account (18hRX9L8)
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
#include <iostream>

void half(int n);

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

main()
{
	int n;
	
	std::cout<<"Number to keep cutting in half?  ";
	std::cin>>n;
	std::cout<<std::endl<<std::endl;
	
	half(n);
}
ok! thanks for all the imput so far!

@usandfriends

if(n!=0) The excercise wants to call the function half() if "n" is greater than 0 so I think it should be if(n > 0)?

Can I have the
1
2
std::cout<<"Number to keep cutting in half?  ";
	std::cin>>n;

within the function? The excercise says "the function must print the number it receives". Does that mean the user imputs a number and it reprints it?

One last question, when I call half() within the 'if' statement, shouldn't it ask me what number I want to imput again? Because it should start at the beginning of the function... I guess if you put the question inside the function like I did it would... but when I run the program it just repeats the number I imput
Nevermind about those questions! thanks to all who helped! You all were very helpful. Happy New years! I got it to work yay
Topic archived. No new replies allowed.