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

  • Thread starter Thread starter k_phanichandra
  • Start date Start date
  • Tags Tags
    C++ Program
AI Thread Summary
The discussion centers on creating a C++ program to calculate the sum of a given number of elements using pointers. A provided code snippet demonstrates a function that takes a pointer to the start of a list and the number of elements, iterating through the list to accumulate the sum. The function is defined with a generic type, allowing it to handle different data types. The response emphasizes the importance of correctly defining the pointer type to ensure proper functionality. Overall, the conversation highlights the utility of pointers in C++ for accessing and manipulating array data efficiently.
k_phanichandra
Messages
3
Reaction score
0
What is the program code in c++ to get the sum of given n numbers using pointers?
 
Technology 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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top