CS 201 - Computer Science I - Spring 2009
Lab 9 - for loops


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Labs > Lab 9

Due

Monday, March 30th at 11:59pm. Labs submitted one day late will be assessed a 20% penalty. Labs will not be accepted more than one day late.

Objectives

Introduction

Recall the setup of a loop to compute the sum of the first n terms of a series:
initialize accumulator to 0
for (int i = 0; i < n; i++)
  compute the value of term number i
  add that term into the accumulator

This code works by making sure that after the loop has run k times, the accumulator holds the sum of the first k terms in the series. Before the loop starts, the accumulator should hold the sum of the first 0 terms of the series, which is zero; hence the initialization step. After the (k-1)st iteration (and at the beginning of the kth iteration), the accumulator holds the sum of the first k-1 terms in the series; adding the next term in the series to that value results in the sum of the first k terms. Since, in general, after the loop has run k times, the accumulator holds the sum of the first k terms, after the loop runs n times, the accumulator holds the sum of all desired terms.

Assignment

Complete the code in AccumulatorLoops to perform the following tasks. First, approximate ex by adding the first n terms in the series

e^x = 1 + x/1! + x^2/2! + ...,

Note that in this series it is wasteful to compute the numerator and denominator from scratch during each iteration of the loop. Instead, they can be easily obtained from their values for the previous term (for example, in this series, when the terms are numbered 0, 1, 2, ..., then the denominator for term i+1 is equal to the denominator for term i times i+1). For cases like this you can use code that mimics the following pseudocode.

initialize the accumulator
initialize the numerator to what it is for the first term
initialize the denominator to what it is for the first term
for each term
  compute the term and add it to the accumulator
  set the numerator to what it should be for the next term
  set the denominator to what it should be for the next term

Test your code by running the program with input 100 for n and 1.0 for x. Your program should output 2.7182818284590455, which is correct to 16 digits. Test your code with other inputs as well.

Next, approximate cos x by adding the first n terms in the series

cos x = 1 - x^2/2! + x^4/4! - ....

Test your code by entering 20 for n and 1.047197551 for x. Your program should output 0.5000000001702586, which is correct to 9 digits.

Next, write a loop that computes sigma(n), the sum of the divisors of n. For example, sigma(25) = 1 + 5 + 25 = 31. In this case it is difficult to compute how many terms there will be in a sum. Instead, you should write a loop as if you were to compute the sum 1 + 2 + 3 + ... + n, and in the body of that loop use an if statement to add the current term only if it appears in the divisor sum. Be sure to test your code on a few examples.

Finally, write a loop that will encrypt the string entered by the user by creating a new string that contains all of the characters in the even positions of the original string followed by all of the characters in the odd positions of the original string. For example, if the original string is CS201 then your program should output C21S0. Note that this is an accumulator loop just like the previous ones, except that instead of adding numbers to a running total you are adding characters to the end of a string (you can use the string append operator + to do this). You will have to think about what the correct initialization is for the string you are building.

Test the string encrypter with strings of both even length and odd length.

Files

Submissions

Submit the completed source code for the AccululatorLoops class as an attachment to e-mail sent to your instructor ([first-initial][last-name]@cs.loyola.edu).
Valid HTML 4.01 Transitional