Function and sqrt

Hello,
I've made a function and I want to find sqrt for a*y, but it keeps printing strange results.. Any help? Here's my function.

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
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
using namespace std;
int Yfunc(int a, int y, int b, int ap, int ag);
int main()
{
    int y;
    double z;
    int ap;
    int ag;
    int a;
    int b;
    cout <<"Type in ap value in the interval:  "<<endl;
    cin >> ap;
    cout <<"Type in ag value in the interval:  "<<endl;
    cin >> ag;
    cout <<"Type in b value:  "<<endl;
    cin >> b;
    cout << Yfunc(a, y, b, ap, ag) <<endl;
    return 0;
}
int Yfunc (int a, int y, int b, int ap, int ag)
{
    for (int a = ap; a <= ag; a++)
    {
        y = a*a + b;
        cout << y <<endl;
    }
}


So this code, counts the value of y, wich is (a*a) + b.
A must be ap <= a <= ag
I can't find the value of z, wich is sqrt(y * a)
Please notice, it's my 2nd program. Thanks.
oh, by the way, y value prints 0 at the end? any solutions? thanks.
If you want to find the square toot, then use the sqrt() function. I don't see it anywhere in your code.
Well, I've done sqrt(Yfunc * a), but the numbers were wrong, so I removed it... Should I make another function and use sqrt inside the for function?
you declare a return function for your Yfun
but i didn't see any return value here . that's why u cannot c out your value
in your function

after for loop.
return y . and don't display inside the function.
not a good practice
The real question is, in what way were the numbers wrong?

The sqrt() function expects a floating-point parameter and returns a floating-point result. Possibly this code which uses integers caused a loss of accuracy. But it's hard to say what went wrong without seeing the particular code which was used.
Topic archived. No new replies allowed.