Public Group

C++ simpel counting thing

This topic is 4479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

My math teather asked what the number would be if u count from 1 to 1000 by saying 1+2+3+4+5+6 until 1000. So I tried to write a program for this case. The problem is that x is the same as y, but it should go ((x's value)+(x's value + 1) not just (x'svalue + 1) what have i done wrong?
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int count=0;
int x=0;
int y=0;

cout<< "Count until:";
cin>> y;

while (count<y){
x + x++;
count++;
}
cout<< x;

system("PAUSE");
return EXIT_SUCCESS;
}



Share on other sites
I think it should be x +=x++

I hope this helps :)

Share on other sites
You can just use the formula:

To get 500500

Share on other sites
Neither x + x++ nor x += x++ are correct. In fact x += x++ is not legal C++ (it has undefined behaviour).

What you want is: count +=x; ++x;

Share on other sites
Quote:
 Original post by FrunyNeither x + x++ nor x += x++ are correct. In fact x += x++ is not legal C++ (it has undefined behaviour).What you want is: count +=x; ++x;

I think count is what he is using to make sure he goes to 1000. I didn't realize x +=x++ was illegal, thank you for pointing that out :)

Share on other sites
Quote:
 Original post by adam23I think count is what he is using to make sure he goes to 1000. I didn't realize x +=x++ was illegal, thank you for pointing that out :)

Right. Then it should be x += count; ++count;

Share on other sites
Here is a solution that I think will work.

x += count after count++

This should loop through make x = 1 then 3 then 6 and so on.

Fruny beat me to it :)

Share on other sites
Quote:
Original post by Fruny
Quote:
 Original post by adam23I think count is what he is using to make sure he goes to 1000. I didn't realize x +=x++ was illegal, thank you for pointing that out :)

Right. Then it should be x += count; ++count;

It works. Thanks for the advices and the formula, that was fast! Now I am going to transfer it to Visual basic :)

Share on other sites
i assume the following :
you are trying to get the sum of an arithmatic sequence.
an arithmatic sequence has a common diffrence e.g( term2 - term1 = term3 - term2 = d)
an arithmatic sequence can be calculated without a loop:
a is the first term.
d is the common difrrence.
n is number of terms.

this is how the forumla is driven
sum of sequence = a + (a+d) + (a+2d) .... (a+d(n-3)) +(a+d(n-2)) + (a+d(n-1)) (reverse)
sum of sequence = (a+d(n-1)) (a+d(n-2)) +(a+d(n-3)) .... (a + 2d) + (a + d) + a ( add both)
sum of sequence*2 = (2a + d(n-1)) + (2a+d +d(n-2)) + (2a + 2d + d(n-3)) .... (2a + 2d + d(n-3))
+ (2a+d +d(n-2)) + (2a + d(n-1)) (simplify)
sum of sequence*2 = (2a + d(n-1)) + (2a + d(n-1)) + (2a + d(n-1)) ..... (2a + d(n-1)) +
(2a + d(n-1)) + (2a + d(n-1)) (factorise)
sum of sequence*2 = n (2a + d(n-1)) ( divide by 2)
sum of sequence = n/2(2a + d(n-1))
( Carl Fredriech Gauss ( 1777-1855))
that's the most effecient way of doing it (that i know of).
1. you should do while (x<y) , so you count till y.
2. you increment x by doing x++; , and add the new x to count.
int main(){    int count=0;    int x=0;    int y=0;        cout<< "Count until:";    cin>> y;        while (x<y){		             x++; // cause common diffrence = 1          count+=x; // add the new number to the series    }    cout<< count; // the sum of the sequence}

OR much preferable
int main(){    int count=0;    int x=0;    int y=0;        cout<< "Count until:";    cin>> y;    count = y/2(2 + y -1)        cout<< count; // the sum of the sequence}

hope this helps.

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 33
• 13
• 12
• 10
• 12
• Forum Statistics

• Total Topics
632573
• Total Posts
3007140

×