C++ Program Help: Get Sum of N Numbers w/ Pointers

  • Context: C/C++ 
  • Thread starter Thread starter k_phanichandra
  • Start date Start date
  • Tags Tags
    C++ Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
k_phanichandra
Messages
3
Reaction score
0
What is the program code in c++ to get the sum of given n numbers using pointers?
 
Physics news on Phys.org
"using pointers" is vague, also there is a separate programming forum...

if you have a list of length n and some pointer p to the start of that list, of the correct type then:

Code:
type sum(type* p, int n)
{
  type sum = 0;
  for(int i = 0; i < n; i++) sum += p[i];
  return sum;
}

is a function that sums the numbers
 
Last edited:
Thanks for giving an answer to my question.