Please help, returning a value

Hello,
I've made a program that should find value of A. A = ap & A <= ag. Then, the program should count Y, wich is a*a + b.
But the program returns the last value of a, it doesn't count it with all A values in the interval. Please help.
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 Yval(int a, int b);
int main()
{
    int ap;
    int ag;
    int a;
    int b;
    int s;
    cout <<"Type in ap:  "<<endl;
    cin >> ap;
    cout <<"Type in ag:  "<<endl;
    cin >> ag;
    cout <<"Type in b:  "<<endl;
    cin >> b;
    for (int a = ap; a <= ag; a++)
    s = Yval(a, b);
    cout << s <<endl;
    return 0;
}
//-----------------Function------------------------
int Yval(int a, int b)
{
    int y;
    y = a*a + b;
    return y;
}
It's always good to set your int values when you create them else the computer just uses the last known value for that memory address.

int ap=0;

Do that for all of these
1
2
3
4
5
    int ap;
    int ag;
    int a;
    int b;
    int s;
1
2
for (int a = ap; a <= ag; a++)
    s = Yval(a, b);
In every cycle of the loop you store the result of the calculation in the same variable. So every new result overwrites the old one, and you're left with only the last result.
EDIT: It looks like maeriden beat me to it.

The variable 's' is being overwritten every cycle made in the for loop. This is an easy fix that only requires the use of the '+=' operator. The use of this operator is the equivalent to adding the new value to the existing value and then assigning it to that variable. The "working" code has been posted below.

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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

int Yval(int a, int b);

int main() {
    int ap;
    int ag;
    int a;
    int b;
    int s = 0;
    cout <<"Type in ap:  "<<endl;
    cin >> ap;
    cout <<"Type in ag:  "<<endl;
    cin >> ag;
    cout <<"Type in b:  "<<endl;
    cin >> b;
    for (int a = ap; a <= ag; a++)
		s+=Yval(a, b);
    cout << s <<endl;
    return 0;
}
//-----------------Function------------------------
int Yval(int a, int b) {
    return a * a + b;
}
Last edited on
Still the same.
Let's say I type:
ap: 1
ag: 6
b: 0
A value should be 1, 2, 3, 4, 5, 6. It's okay.
Till I need to count Y value, that is a*a + b.
So the Y, according to my ap, ag and b values should be:
1, 4, 9, 16, 25, 36.
But it only returns 36. Help?
Maybe I found the problem, After your for, you have no {}

The output is different, see if that is what you wanted to do:

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
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
using namespace std;
int Yval(int a, int b);
int main()
{
    int ap=0;
    int ag=0;
    int a=0;
    int b=0;
    int s=0;
    cout <<"Type in ap:  "<<endl;
    cin >> ap;
    cout <<"Type in ag:  "<<endl;
    cin >> ag;
    cout <<"Type in b:  "<<endl;
    cin >> b;
    
    for (int a = ap; a <= ag; a++)
    {cout << "a= "<< a << " b= "<< b << " ap= " << ap << " ag= " << ag << endl;
    s = Yval(a, b);
    cout << "s= " << s <<endl;
    }
    return 0;
}
//-----------------Function------------------------
int Yval(int a, int b)
{
    int y=0;
    y = a*a + b;
    return y;
}
Last edited on

Still the same.
Let's say I type:
ap: 1
ag: 6
b: 0
A value should be 1, 2, 3, 4, 5, 6. It's okay.
Till I need to count Y value, that is a*a + b.
So the Y, according to my ap, ag and b values should be:
1, 4, 9, 16, 25, 36.
But it only returns 36. Help?

The preceding posts on this thread explain why that is happening. When I run the code I provided I receive an output of '91'.
Last edited on
It works, your just not displaying the value for y

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



My output using 1 6 0

C:\Temp>testxxx5
Type in ap:
1 6 0
Type in ag:
Type in b:
a= 1 b= 0 ap= 1 ag= 6
y= 1
s= 1
a= 2 b= 0 ap= 1 ag= 6
y= 4
s= 4
a= 3 b= 0 ap= 1 ag= 6
y= 9
s= 9
a= 4 b= 0 ap= 1 ag= 6
y= 16
s= 16
a= 5 b= 0 ap= 1 ag= 6
y= 25
s= 25
a= 6 b= 0 ap= 1 ag= 6
y= 36
s= 36
Last edited on
Thanks guys
Topic archived. No new replies allowed.