Functions c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Functions c++

I just started learning functions and I am struggling to solve this. Lecturer gave a hint that you have to start calculating from the bottom aka 256 to the top. This is the code i have so far(its incorrect obviously): EDIT: THIS IS CORRECT CODE: https://code.sololearn.com/cjqthEVDtJPn/#cpp this is the formula(sorry i can only upload as a picture because it would be hard to understand if i would type it all): https://imgur.com/pmJzAFV

22nd Nov 2019, 7:56 PM
A S
A S - avatar
16 Answers
+ 1
A S I do not understand how you can divide a floating number by 1 and yet able to get a whole integer.
23rd Nov 2019, 3:15 PM
Avinesh
Avinesh - avatar
0
A S what is your code actually trying to do?
22nd Nov 2019, 8:08 PM
Avinesh
Avinesh - avatar
0
In the function funkc() replace cout<<a<<endl; with return a;
22nd Nov 2019, 8:11 PM
VOID
VOID - avatar
0
@avinesh user inputs x and using function its calculating y
22nd Nov 2019, 8:17 PM
A S
A S - avatar
0
A S what is your code about? What should the output be like?
22nd Nov 2019, 8:18 PM
Avinesh
Avinesh - avatar
0
@CodeCat_ okay, i did. Answer is still incorrect. But can you explain why would i need to use return a;?
22nd Nov 2019, 8:19 PM
A S
A S - avatar
0
A S if the method is of either int, float, char, double or String, it must return a variable. And if the method is of void type, it should directly print using cout.
22nd Nov 2019, 8:23 PM
VOID
VOID - avatar
0
@Avinesh i dont get your question. User just inputs x, then y is calculated. Y is outputed. Look at the picture i added
22nd Nov 2019, 8:26 PM
A S
A S - avatar
0
A S Your missing some thing here that in the last iteration 'a' storing x*x +0; all previous values are missing... So use float values for a,o so 1st statement of while loop write outside, and take another variable float y; y=x*x+a; And do you know if i input 2 what is output? its better to use functions. better to calculate from top to bottom
22nd Nov 2019, 8:56 PM
Jayakrishna 🇮🇳
0
Are you trying to do:- 1/((x*x) + 2) / ((x*x) + 4) / ((x*x) + 6) / ((x*x) + 8).....-->/((x*x) + 256) and then divide the total by (x*x)...... using a loop inside a function?
23rd Nov 2019, 12:29 AM
rodwynnejones
rodwynnejones - avatar
0
@rodwennejones yes,but you dont have to divide total by x*x because only 256 has to be divided by it
23rd Nov 2019, 7:44 AM
A S
A S - avatar
0
Can you give a sample input..... and an output using that input so I can check my prog does what you need.
23rd Nov 2019, 12:53 PM
rodwynnejones
rodwynnejones - avatar
0
i only know that if input 5, output 120
23rd Nov 2019, 1:03 PM
A S
A S - avatar
0
the example you gave (input 5, output 120) is simple enough to do in a loop inside a function........but I can't see how the equation in your uploaded image could do that...but i'm no mathematician.
23rd Nov 2019, 3:00 PM
rodwynnejones
rodwynnejones - avatar
0
A S What problem you are trying to solve? Give me your Problem in English and Input. I will make you a explained solution in C++
24th Nov 2019, 7:05 PM
Robin Singh
Robin Singh - avatar
0
Hello, output, when input 5 is not 120 , sorry for that. And I solved it already, thanks for everybody's help.
24th Nov 2019, 7:24 PM
A S
A S - avatar