Hello, My C program crashes during the sum function in the following program. I
ID: 3547622 • Letter: H
Question
Hello, My C program crashes during the sum function in the following program. I believe the problem is in lines 13 and 15, where I add the next term in the array to the pointer. I don't know why it keeps doing this to me!
#include <stdio.h>
#include <stdlib.h>
void sum(double a[],
int n, // n is the size of a
double *even_index_sum_ptr,
double *odd_index_sum_ptr)
{
int i;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
*even_index_sum_ptr += a[i];
else
*odd_index_sum_ptr += a[i];
}
}
int main(void)
{
int n, i;
double *oddSum=0;
double *evenSum=0;
printf("Enter N: ");
scanf("%d", &n);
double a[n];
printf(" Print %d doubles: ", n);
for (i=0; i < n; i++)
{
scanf("%lf", &a[i]);
}
sum(a, n, evenSum, oddSum);
printf(" Sum of even index = %lf", evenSum);
printf(" Sum of odd index = %lf", oddSum);
return 0;
}
Explanation / Answer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.