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.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include #include 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: "<> ap; cout <<"Type in ag: "<> ag; cout <<"Type in b: "<> b; for (int a = ap; a <= ag; a++) s = Yval(a, b); cout << s <
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
 ``12345`` `````` int ap; int ag; int a; int b; int s;``````
 ``12`` ``````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.

 ``123456789101112131415161718192021222324252627282930`` ``````#include #include #include #include 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: "<> ap; cout <<"Type in ag: "<> ag; cout <<"Type in b: "<> b; for (int a = ap; a <= ag; a++) s+=Yval(a, b); cout << s <
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:

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include #include #include 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: "<> ap; cout <<"Type in ag: "<> ag; cout <<"Type in b: "<> 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 <
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

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include #include 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: "<> ap; cout <<"Type in ag: "<> ag; cout <<"Type in b: "<> 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 <

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.