I am a bit of a noob in programming, that is why I am struggling with this rather simple code. Our task is to sum up all elements of an array using a function. The function should print out the array, then sum up its elements and give back its sum to the main. There it should be printed. It is working until it gets to the function, I think.
Read a lot of other posts regarding this question but still can't wrap my head around the solution. My compiler is also telling me "Segementation fault (core dumped)". I am really confused.
That's my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 9 // i think the math one is a bit unneccessary but well
int sum(int array[], int size)
{
int i;
int sum = 0;
for (i = 0; i <= MAX; i++)
{
printf("\n%d", array[i]);
}
for (i = 0; i <= MAX; i++)
{
sum = sum + array[i];
}
return sum;
}
int main()
{
int i;
int array[MAX];
int size;
for (i = 1; i <= 10; i++)
{
printf("\nGeben Sie die %d. Zahl ein: ", i); // Its in german, basically saying the user should fill the array, starting with the 1. element
scanf("%d", &array[i - 1]);
}
sum(array[MAX], size);
printf("%d", size);
return 0;
}
Helping me would be really nice. Thanks!