C++ simpel counting thing

Started by
14 comments, last by Kaldemeo 17 years, 10 months ago
#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
Advertisement
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).
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
another thing though is that formula assumes your starting your count at 1 but what if you wanted to count from 100 to 1000?
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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;}
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
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' !

This topic is closed to new replies.

Advertisement