- 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
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.
0
A S what is your code actually trying to do?
0
In the function funkc() replace
cout<<a<<endl;
with
return a;
0
@avinesh
user inputs x and using function its calculating y
0
A S what is your code about?
What should the output be like?
0
@CodeCat_ okay, i did. Answer is still incorrect. But can you explain why would i need to use return a;?
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.
0
@Avinesh i dont get your question. User just inputs x, then y is calculated. Y is outputed. Look at the picture i added
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
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?
0
@rodwennejones yes,but you dont have to divide total by x*x because only 256 has to be divided by it
0
Can you give a sample input..... and an output using that input so I can check my prog does what you need.
0
i only know that if input 5, output 120
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.
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++
0
Hello, output, when input 5 is not 120 , sorry for that.
And I solved it already, thanks for everybody's help.