-
Pointer arithmetic
I am writing a program for an array with 10 elements. I am trying to get the sum of all the elements in the array. I have thoroughly confused myself. Please look at my code and make suggestions as to what I am doing wrong. I know it is in the second 'for' loop, but I know that I am making it harder than what it should be.
Code:
#include <iostream>
using namespace std;
void explanation();
int sums;
int main()
{
explanation();
int nums[10] = {1,2,3,4,5,6,7,8,9,10};
int i;
int *ptr;
ptr=&nums[0];
printf("The numbers in the array are: \n");
for (i=0; i < 10; i++)
{
printf("%d ", nums[i]);
}
for (i=0; i < 10; i++)
{
sums = *ptr + nums[i];
}
printf( "%d","The sum of the elements are: ", sums );
return 0;
}
void explanation()
{
printf("Written by Kelle McCan\n");
printf("This program calculates the sum of a ten\n");
printf("element array and then displays the sum\n");
printf("\n");
}
-
int sums=0;
sums += nums[i];
-
I feel I'm getting closer! However, now it is saying the sum is 0. That's better than the 3294506849 I was getting!
-
printf( "The sum of the elements are: %d", sums );
-
It too is still giving the value of 0. I tried playing with that, giving the value of 55 (as what it should be) and it still came up with 0. Do you think it might be how I declared them?
-
Did you change
int sums;
to
int sums=0;
-
yes I did. Still getting '0' for the sums.
I haven't changed much.
Code:
#include <iostream>
using namespace std;
//void explanation();
int sums;
int main()
{
explanation();
int nums[10] = {1,2,3,4,5,6,7,8,9,10};
int i;
int *ptr;
ptr=0;
ptr=&nums[10];
printf("The numbers in the array are: \n");
for (i=0; i < 10; i++)
{
printf("%d ", nums[i]);
}
for (i=0; i <= 10; i++)
{
int sums=0;
sums += nums[i];
}
printf("\n");
printf("The sum of the elements are: ");
printf("\n");
printf("%d", sums);
printf("\n");
return 0;
}
-
You have 2 sums (delete both).
Delete: int sums;
Also delete: int sums=0;
Change: int i;
to: int i,sums=0;
-
I am sorry. I am now getting a negative number. It's very long. I did as you asked. Do I need a prototype? As you can see by my posts prior to this, I have problems understanding pointers.
-
You are currently not even using the pointers and can temporarily delete:
Code:
int *ptr;
ptr=0;
ptr=&nums[10];
We are first trying to find your problem.
Try adding more printf's everywhere to see the math.
-
Ok...I'm commenting on your code on post #7 ...
Do accordingly... and it'll be ok.
Code:
#include <iostream>
using namespace std;
//void explanation();
int sums;
int main()
{
explanation();
int nums[10] = {1,2,3,4,5,6,7,8,9,10};
int i;
int *ptr; //Why do you need these ?
ptr=0; //^
ptr=&nums[10];//^
printf("The numbers in the array are: \n"); //Try using C++ style I/O ..
for (i=0; i < 10; i++)
{
printf("%d ", nums[i]);
}
for (i=0; i <= 10; i++) //< instead of <= ...as indices are from 0 to n-1
{
int sums=0;//Put this statement above the loop.
sums += nums[i];
}
printf("\n");
printf("The sum of the elements are: ");
printf("\n");
printf("%d", sums);
printf("\n");
return 0;
}
Also.. just to tempt you towards using the modern C++ features..watch the following code..(Note that you'll need a C++11 compatible compiler)
Code:
#include<array>
#include<iostream>
using namespace std;
int main()
{
array<int,10> nums={1,2,3,4,5,6,7,8,9,10};
int sum(0);
for(auto x:nums)
{
cout<<x<<'\t';
sum+=x;
}
std::cout<<"\nSum= "<<sum;
cin.get();
return 0;
}
-
I guess I didn't clearly state what I was doing. In my assignment I am to write a program that has an array of 10 elements and use pointer arithmetic to sum up all of the elements in the array. I don't have a problem with adding the arrays. It's using pointers that I am having a hard time understanding. I know that I can use sums = *p + (*p+1) + (*p+2) +....(*p+9), but I know there is an easier way, so in the mean time I keep confusing myself.
-
motocross1, I don't know it you read my other reply to manasij7479, but I am trying to use pointer arithmetic to sum an array that has 10 elements. I appreciate the help you have given me, honestly.
-
Same thing..
Change the loop to:
Code:
for(int i=0;i<n;i++)
sum+=*(array_name + i);
-
Yay! Thank you so much!!! I knew I was making it harder than what it should be. Thank you so much!
-
That is quite dumb though. *(array_name + i) provides no advantage over array_name[i]. If you want to use pointer arithmetic, an appropriate use is with a generic algorithm, e.g.,
Code:
#include <numeric>
// ...
int sum = std::accumulate(array_name, array_name + 10, 0);
Another appropriate use, more likely what you are expected to do, would be to use a pointer to iterate, e.g.,
Code:
int sum = 0;
for (int* p = array_name; p != array_name + 10; ++p)
{
sum += *p;
}