Docsity

Lab 7: Observations on Harmonic Series and Payroll Program, Study notes of Computer Science

Toggle sidebar
bg1
CPS 196 Lab 7 Fall 2007
For the first problem, you will be making some observations of the running of a program, both the
output and using debug for the values of variables. Please write down your observations on this lab
sheet as indicated.
1. Practicing loops.
A. The following is a different version of computing the harmonic series.
/* HarmonicError.c */
/* Computes the harmonic series until a difference is reached */
#include <stdio.h>
int main()
{
/* declarations of input and output variables */
double diffgoal; /* difference between sums of terms requested */
double sumterms; /* result of summing the squares */
/* declarations of additional variables */
double inverseterm; /* compute each term, the inverse */
double lastsum; /* the previous sum of terms in the series */
double diffachieved; /* difference between sums of terms so far */
int index; /* index variable to count loop repetitions */
/* ask the user how much precision is required to compute the series */
printf("How much difference between sums of terms should be achieved? \n");
printf("Please enter a number less than 1> ");
scanf("%lf", &diffgoal);
/* initialize the sum to zero */
sumterms = 0;
/* initialize the difference so far to 1 (the largest difference)
and the last sum */
diffachieved = 1.0;
lastsum = 0;
/* initialize the index variable */
index = 1;
/* compute the harmonic series of terms until the difference
between the sums of terms meets the goal */
while (diffachieved >= diffgoal)
{
/* compute the inverse of the index and add to the sum */
inverseterm = 1.0 / index;
sumterms = sumterms + inverseterm;
/* compute the difference between this sum and the previous one,
reset the last sum variable */
diffachieved = sumterms - lastsum;
lastsum = sumterms;
/* increment the index variable */
pf3

Partial preview of the text

Download Lab 7: Observations on Harmonic Series and Payroll Program and more Study notes Computer Science in PDF only on Docsity!

CPS 196 Lab 7 Fall 2007

For the first problem, you will be making some observations of the running of a program, both the

output and using debug for the values of variables. Please write down your observations on this lab

sheet as indicated.

1. Practicing loops.

A. The following is a different version of computing the harmonic series.

/* HarmonicError.c / / Computes the harmonic series until a difference is reached */ #include <stdio.h>

int main() { /* declarations of input and output variables / double diffgoal; / difference between sums of terms requested / double sumterms; / result of summing the squares */

/* declarations of additional variables / double inverseterm; / compute each term, the inverse / double lastsum; / the previous sum of terms in the series / double diffachieved; / difference between sums of terms so far / int index; / index variable to count loop repetitions */

/* ask the user how much precision is required to compute the series */ printf("How much difference between sums of terms should be achieved? \n"); printf("Please enter a number less than 1> "); scanf("%lf", &diffgoal);

/* initialize the sum to zero */ sumterms = 0;

/* initialize the difference so far to 1 (the largest difference) and the last sum */ diffachieved = 1.0; lastsum = 0;

/* initialize the index variable */ index = 1;

/* compute the harmonic series of terms until the difference between the sums of terms meets the goal / while (diffachieved >= diffgoal) { / compute the inverse of the index and add to the sum */ inverseterm = 1.0 / index; sumterms = sumterms + inverseterm;

/* compute the difference between this sum and the previous one, reset the last sum variable */ diffachieved = sumterms - lastsum; lastsum = sumterms;

/* increment the index variable */

index++; } // end of for loop

/* print the result */ index = index - 1; // to get the number of the last term printf("The harmonic series computed from %i terms is %lf.\n", index, sumterms);

return(0); }

i. Cut and paste this program from the Lecture/Lab page. Run the program with an input value

of 0.001. What is the output?

ii. Now step through the program using Debug | Step Into and Step Over and enter an input

value of .2. Record the values of the variables as they are modified in the program.

Variable Value1 Value2 Value3 …