Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote