Oct 30, 2011 at 6:35am UTC
hai everybody,
i'm a beginners. i wanna to know how to solve this question by using loop statement?
QUESTION :
get the value for a,d,n from user.
and solve the question:
a + (a+d) + (a+2d) + (a+3d) + ... + [a-(n-1)d]
i do it for a whole day but haven solve this!!
TQ everybody.
Oct 30, 2011 at 7:06am UTC
Post the code you have at the moment, use
around it and format it appropriately and people on the forum will take a look and try to help you.
Oct 30, 2011 at 7:14am UTC
i dun understand.
can u show me example?
Oct 30, 2011 at 7:23am UTC
What do you mean, have you attempted to write the program yourself yet or did you come here hoping somebody would just make it for you? People can if they want but you won't learn from it. You need to actually try to make things yourself in order to learn when programming.
What i mean by post your code is just copy and paste whatever code you have so far. So your includes, your main, functions or anything else you have wrote. For the program you have described i doubt there are any classes or a function due to its size and with you being relatively new to C++.
Oct 30, 2011 at 7:29am UTC
oh.... sorry, i just not understand what u mean.
this is my work.
#include<iostream>
using namespace std;
int main()
{
double grade, total=0;
int count=1;
int a, d, n;
cout<<"Enter a number for a : ";
cin>>a;
cout<<"Enter a number for d : ";
cin>>d;
cout<<"Enter a number for n : ";
cin>>n;
Total = a + (a+d) + (a+2(d)) + (a+3(d)) + ... + [a+(n-1)d];
count++;
cout<<"Total = "<<endl;
return 0;
}
can u help me, see what was wrong with my program.
TQ very much
Oct 30, 2011 at 7:42am UTC
This: "2(d)" will not multiply 2 and d. Use the asterisk for multiplication: 2*d.
Oct 30, 2011 at 7:45am UTC
oh... ok i try to change it.
TQ
Oct 30, 2011 at 7:46am UTC
why the system write Total is not declared?
but i have int already.
Oct 30, 2011 at 8:03am UTC
because you are using a capital 'T'... you declared the variable with a lower case 't'... so change this:
Total = a + (a+d)
to this...
total = a + (a+d)
Last edited on Oct 30, 2011 at 8:04am UTC
Oct 30, 2011 at 8:07am UTC
how to solve this problem?
error : expected primary-expression before '...' token
error : expected ';' before '...' token
Last edited on Oct 30, 2011 at 8:12am UTC
Oct 30, 2011 at 8:14am UTC
Are you sure this is the exact equation you are meant to be using:
Total = a + (a+d) + (a+2(d)) + (a+3(d)) + ... + [a+(n-1)d];
and not this?
total = a + (a+d) + ((a+2)*d) + ((a+3)*d) + ((a+n-1)*d);
Oct 30, 2011 at 8:16am UTC
yes, i'm sure about the exact equation.
because the question give like this:
a + (a+d) + (a+2d) + (a+3d) + … + [(a + (n-1)d)]
a si the first term
d is the common difference
n is the numberof the term to be added
Last edited on Oct 30, 2011 at 8:24am UTC
Oct 30, 2011 at 8:19am UTC
If after following my other suggestion and the original one of Jokerdan, copy and paste your actual compiler errors as they will tell us what's wrong with your program.
Oct 30, 2011 at 8:25am UTC
As far as i know there are no operators that use ...
, and having [
/]
are not needed for this? I don't see why you 'need' to use them either.... Is this for a class project or something?
Last edited on Oct 30, 2011 at 8:26am UTC
Oct 30, 2011 at 8:28am UTC
i just copy from the formula given.
so how to expected this?
Oct 30, 2011 at 9:17am UTC
Okay the 3 dots convey a certain pattern, now I know you know that you cannot use 3 dots
in a C++ statement, it's just an expression on paper.
You need to learn about using loops, as you have heard, in this case the for loop is easier to use.
http://www.cplusplus.com/doc/tutorial/control/
Check them out, and if you still have issues, ask.
Last edited on Oct 30, 2011 at 9:18am UTC
Oct 30, 2011 at 12:24pm UTC
There's two solutions to this problem.
There's a nice little rewrite of your equation (which will work a ton faster).
And there's the loop version.
Since you want the loop version, you should look into for-loops.