Sign in to follow this  

C++ simpel counting thing

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Fruny
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;


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

Share this post


Link to post
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).
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.

Share this post


Link to post
Share on other sites

#include<iostream>
int main(){
using std::cout;
using std::cin;
using std::endl;

cout << "Enter a small integer: ";
int nFirst(0);
cin >> nFirst;
cout << "Enter a large integer: ";
int nSecond(0);
cin >> nSecond;
int temp(nFirst);
int sum(nFirst);

while (temp < nSecond){
sum = sum + (temp + 1);
++temp;
}
cout << "the sum of all integers from " << nFirst << " to " << nSecond;
cout << " is " << sum;
cout << endl;
return 0;
}

OUTPUT:

Enter a small integer: 1
Enter a large integer: 1000
the sum of all integers from 1 to 1000 is 500500
Press any key to continue . . .

also code fusion your code and algorithms confused the hell out of me man :/ that was wayy to ego driven and not really for beginners

Share this post


Link to post
Share on other sites
My algorithm !. This is the standard method for calculating the sum of an arithmatic sequence.
this is at it's best ,high school level mathematics , and it's really irrelavent to a person's coding
experience.if you have problems understanding how it was driven , you should just apply the
formula as it is , or you can look for a better explantion (try the link Wiggin provided).

Share this post


Link to post
Share on other sites
Quote:
Original post by bish0p
another thing though is that formula assumes your starting your count at 1 but what if you wanted to count from 100 to 1000?


Count from 1 to 1000 and subtract the count from 1 to 99.

Share this post


Link to post
Share on other sites
just edit the formula
remember
d = common diffrence
a = first term
n = nummber of terms
sum = n/2 ( 2a + d(n-1))


void main()
{
LONG count=0;
LONG start=0;
LONG end=0;
int CDiffrence=0;
int n = 0;
cout<< "What do you want your starting number to be?\n";
cin>>start;
cout<< "What do you want your ending number to be?\n";
cin>> end;
cout<< "What do you want the constant incremenation to be ?\n e.g 1+2+3 OR 1+3+5\n ";
cin >> CDiffrence;
n = ((end - start)/CDiffrence);
count = (n/2) * (2*start + CDiffrence* (n - 1));

cout<< count; // the sum of the sequence
int yy;
cin >> yy;
return;

}


Share this post


Link to post
Share on other sites
Quote:
Original post by Wiggin
Link.

What a nice explanation (and story) ;)

Quote:
Original post by Code Fusion
just edit the formula
remember
d = common diffrence
a = first term
n = nummber of terms
sum = n/2 ( 2a + d(n-1))
*** Source Snippet Removed ***

That is great, even--
while (x<y){

x++; // cause common diffrence = 1
count+=x; // add the new number to the series

}
cout<< count;

--is more straight (for my point of view)

Thx to everybody for the replays' !

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this