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

  • C/C++
  • Thread starter k_phanichandra
  • Start date
  • Tags
    C++ Program
In summary, pointers in a C++ program are used to indirectly access and manipulate data, making code more dynamic and efficient. They can be used to allocate memory for an array of N numbers and calculate their sum. However, they require careful management to avoid potential issues such as memory leaks and segmentation faults. Other methods, such as using arrays and loops, can also be used to get the sum of N numbers in a C++ program.
  • #1
k_phanichandra
3
0
What is the program code in c++ to get the sum of given n numbers using pointers?
 
Technology news on Phys.org
  • #2
"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:
  • #3
Thanks for giving an answer to my question.
 

1. What is the purpose of using pointers in a C++ program?

Pointers are variables that hold the memory address of another variable. They are used to indirectly access and manipulate data in a program, making it possible to create more dynamic and efficient code.

2. How do pointers help in getting the sum of N numbers in a C++ program?

Pointers can be used to dynamically allocate memory for an array of N numbers and then iterate through the array to calculate the sum. This saves memory and allows for more flexibility in the size of the array.

3. Can you provide an example of using pointers in a C++ program to get the sum of N numbers?

Sure, here is an example code snippet:int *numbers = new int[N]; // dynamically allocate memory for N numbersint sum = 0; // initialize sum variablefor (int i = 0; i < N; i++) { cin >> *(numbers + i); // store user input in array using pointer arithmetic sum += *(numbers + i); // add current number to sum using pointer arithmetic}cout << "Sum of " << N << " numbers: " << sum << endl; // print sumdelete[] numbers; // free up memory

4. Are there any potential issues or pitfalls to be aware of when using pointers in a C++ program?

Yes, pointers can be tricky as they require careful memory management. Improper use of pointers can lead to memory leaks, segmentation faults, and other errors. It is important to understand how pointers work and to use them correctly in order to avoid these issues.

5. Is it possible to get the sum of N numbers without using pointers in a C++ program?

Yes, it is possible. Pointers are just one approach to solving this problem. Other methods include using arrays, loops, and variables to store and manipulate the data. However, using pointers can often be a more efficient and flexible solution.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
881
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
28
Views
2K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
2
Views
931
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
811
Back
Top