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");
}