#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;
}
C++ simpel counting thing
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?
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;
What you want is: count +=x; ++x;
Quote:Original post by Fruny
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;
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 :)
Quote:Original post by adam23
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 :)
Right. Then it should be x += count; ++count;
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 :)
x += count after count++
This should loop through make x = 1 then 3 then 6 and so on.
Fruny beat me to it :)
Quote:Original post by FrunyQuote:Original post by adam23
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 :)
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 :)
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).
regarding your code.
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.
OR much preferable
hope this helps.
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).
regarding your code.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement