|
|
Sum Fucntion? - I've been searching the web for a sum fucntion but... | ||
Discussion by Faint545 with 5 Replies.
Last Update: January 12, 2007, 12:40 pm | |||
I've been searching the web for a sum fucntion but I just can't seem to find one...
I'm programming in C++ and I have an array of numbers ( name[#] ) and I would like to calculate the sum of them without typing out the positions individually. Is there any way that is possible?
I'm programming in C++ and I have an array of numbers ( name[#] ) and I would like to calculate the sum of them without typing out the positions individually. Is there any way that is possible?
Sun Feb 26, 2006 Reply New Discussion
If you are running C++, you should have no problem writing down a loop
Sum=1
while blah...
++sum
No ?
Sum=1
while blah...
++sum
No ?
Sun Feb 26, 2006 Reply New Discussion
Here's something I came up with
#include <iostream>
int sum(int arr[], int length){
int sum = 0;
for(int i = 0, n = length; i < n; i++){
sum += arr[i];
}
return sum;
}
int main(){
int test[] = { 1, 2, 3, 4, 5 };
int result = sum(test, sizeof(test)/sizeof(int));
std::cout << result << std::endl;
return 0;
}
The problem with sending an array to a function is that the size information is lost, so you must send the size yourself. That is why I used sizeof(array)/sizeof(int); to work out the size of it.
Cheers,
MC
CODE
#include <iostream>
int sum(int arr[], int length){
int sum = 0;
for(int i = 0, n = length; i < n; i++){
sum += arr[i];
}
return sum;
}
int main(){
int test[] = { 1, 2, 3, 4, 5 };
int result = sum(test, sizeof(test)/sizeof(int));
std::cout << result << std::endl;
return 0;
}
The problem with sending an array to a function is that the size information is lost, so you must send the size yourself. That is why I used sizeof(array)/sizeof(int); to work out the size of it.
Cheers,
MC
Sun Feb 26, 2006 Reply New Discussion
You could also use the STL algorithm 'accumulate':
#include <iostream>
//...
int main(){
int test[] = { 1, 2, 3, 4, 5 };
int result = std::accumulate(test, test + 5);
std::cout << result << std::endl;
return 0;
}
It takes a pointer or iterator to the start of a sequence and a pointer of iterator one past the end of the sequence. Simply put, the first argument is the name of the array and the second is the name of the array plus the size of the array. You can also use it for STL containers, as in:
#include <iostream>
#include <vector>
//...
int main(){
std::vector<int> test(4,9);
int result = std::accumulate(test.begin(), test.end());
std::cout << result << std::endl;
return 0;
}
CODE
#include <algorithm>#include <iostream>
//...
int main(){
int test[] = { 1, 2, 3, 4, 5 };
int result = std::accumulate(test, test + 5);
std::cout << result << std::endl;
return 0;
}
It takes a pointer or iterator to the start of a sequence and a pointer of iterator one past the end of the sequence. Simply put, the first argument is the name of the array and the second is the name of the array plus the size of the array. You can also use it for STL containers, as in:
CODE
#include <algorithm>#include <iostream>
#include <vector>
//...
int main(){
std::vector<int> test(4,9);
int result = std::accumulate(test.begin(), test.end());
std::cout << result << std::endl;
return 0;
}
Sat Dec 16, 2006 Reply New Discussion
QUOTE (yordan)
If you are running C++, you should have no problem writing down a loop
Sum=1
while blah...
++sum
No ?
Link: view Post: 71248
You did some mistake. Actually you wrote in pseudocode count function. Sum differ from what you wrote in first line: Sum = 0, then in third Sum +=
Here is simple one line example code: for( float Sum=0, int i=0; i<sizeof(array); i++) Sum+=array;
Fri Jan 12, 2007 Reply New Discussion
QUOTE (ignite)
You did some mistake. Actually you wrote in pseudocode count function. Sum differ from what you wrote in first line: Sum = 0, then in third Sum +=
Here is simple one line example code: for( float Sum=0, int i=0; i<sizeof(array); i++) Sum+=array;
Link: view Post: 95720
Ouch ! yes, you are right. I could imagine that, before starting, the sum is 0, and I started with Sum=1.
You know, a lot of people did this mistake some years ago.
They really thought that January 1st 2000 was the beginning of the new century. And that's false, it was the beginning of the last year of the old century.
Still the same problem : how do you count the things in your pocket : 1 - 2 - 3 - 4, or 0 - 1 - 2 -3 ?
Fri Jan 12, 2007 Reply New Discussion
Win32 Api: Modal Dialog Box Problem C++ Win32 API Programming (2)
|
(5) C++ Template Coding Conundrum...
|
Index




