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

Write a program that inputs an integer N followed by N number of doubles that sh

ID: 3547362 • Letter: W

Question

Write a program that inputs an integer N followed by N number of doubles that  should be stored in an array. Then call the function sum to calculate the sum of odd index and even index numbers in the array, and print the results in the main program. Use two decimal places to show the results.    Each element of the array contributes to one of the two sums, depending on whether the index of the element is even or odd. Your function definition should look  something like this:

void sum(double a[],
                     int n,                       // n is the size of a[]
                     double *even_index_sum_ptr,
                     double *odd_index_sum_ptr)
            {
                ...
            }

Sample Input:
            Enter N: 7

            Enter 7 doubles: 20   30.1   10.5   90   10   60.7   1.2

Sample Output:
            Sum of even index =  41.70

            Sum of odd  index = 180.80

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

void sum(double a[], int n, double even_index_sum_ptr, double odd_index_sum_ptr) // n is the size of a[]

{

int i = 0;

for(i = 0; i<n ; i++) if(i%2 == 0) even_index_sum_ptr += a[i]; else odd_index_sum_ptr += a[i];


printf("Sum of even index = %f ",even_index_sum_ptr);

printf("Sum of odd index = %f",odd_index_sum_ptr);

}

int main(){

int n, i;

double even_index_sum_ptr = 0, odd_index_sum_ptr = 0;

printf("Enter N: ");

scanf("%d",&n);


double a[n];


printf("Enter %d doubles: ",n);

for(i = 0;i<n;i++) scanf("%f",&a[i]);

sum(a, n, even_index_sum_ptr, odd_index_sum_ptr);

return 0;

}

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