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

We want to compute the sum of an arithmetic series a_1 + a_2 + ... + a_N, where

ID: 3834483 • Letter: W

Question

We want to compute the sum of an arithmetic series a_1 + a_2 + ... + a_N, where a_1 = 1 and a_k = a_k-1 / d, for all values of k (up to N). Example, if d = 2 and N = 5, then sum of series is 1 + 0.5 + 0.25 + 0.125 + 0.0625 = 1.9375 d = -0.5 and N = 6, then sum of series is 1 + -2 + 4 + -8 + 16 + -32 = -21 Write a C program that Reads the values d and N from a file called data.txt Finds sum of the series using the values of d and N just read from the text file. Print values of d, N, and sum of series to a file output.txt Also finds the average value of all sum of the series and prints the average to the output.txt file. See sample code execution below. Your program must use the following user-defined function: void myFunc(double d, int n, double *sum_pt){ //it receives two numbers d and N read from the file and sets the memory referenced by sum_pt to the sum of the series. }

Explanation / Answer

// a1+a2+....aN
// a1 = 1, ak = ak-1/d,
// d=2, N=5
// a1 = 1 , a2 = 1/2 a3= 1/2*2 a4= 1/4*2 a5=1/8*2

// Below code will work file if you give input from stdin and resultant output will be shown on stdout//

// to use file input and output uncomment the below commented lines //

#include <stdio.h>
void myFunc(double d, int n , double *sum_pt)
{
if(n==0)
return;
if(n==1)
{
*sum_pt = 1;
return;
}
  
int i=1;
double adder = 1;
*sum_pt = 1;
while(i<n)
{
*sum_pt = *sum_pt + adder/d;
adder = adder/d;
i++;
}
  
}

int main() {
// FILE * pFile;
// FILE *rFile;
int N;
double d;
double sum = 0;
double average = 0;
int set_of_values = 6;
int i;
printf("dvalue Nvalue sum of series ");
//   rFile = fopen ("data.txt","r");
//   pFile = fopen ("output.txt","w");
for(i =1; i<=set_of_values; i++)
{
   //fscanf(rfile,"%lf", &d);
       //fscanf(rfile,"%d", &N);
scanf("%lf", &d);
scanf("%d", &N);
sum = 0;
myFunc(d, N, &sum);
average = average+sum;
printf("%0.2lf %d %0.4lf ", d, N, sum);
       // fprintf (pFile, "%0.2lf %d %0.4lf ", d, N, sum);
}
printf("average of all sum of series is: %0.4lf ", average/set_of_values);
   // fprintf("average of all sum of series is: %0.4lf ", average/set_of_values);
   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