| Kovs95 (52) | |||
|
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
| |||
|
|
|||
| pogrady (525) | |
|
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; } | |
|
|
|
| Kovs95 (52) | |
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.
| |
|
|
|
| Incis B (92) | |
|
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
|
|
| usandfriends (186) | |||
| |||
|
|
|||
| Kovs95 (52) | |||
|
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
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 | |||
|
|
|||
| Kovs95 (52) | |
| Nevermind about those questions! thanks to all who helped! You all were very helpful. Happy New years! I got it to work yay | |
|
|
|