C++

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.
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.
i dun understand.
can u show me example?
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++.
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
This: "2(d)" will not multiply 2 and d. Use the asterisk for multiplication: 2*d.
oh... ok i try to change it.
TQ
why the system write Total is not declared?
but i have int already.
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
how to solve this problem?

error : expected primary-expression before '...' token

error : expected ';' before '...' token
Last edited on
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);
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
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.
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
i just copy from the formula given.

so how to expected this?
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
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.
Topic archived. No new replies allowed.