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.
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
usingnamespace 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;
}
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.
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.
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
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?
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
usingnamespace 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;
}
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'.
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
usingnamespace 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;
}