California State University, Northridge



[pic]

|College of Engineering and Computer Science

Computer Science Department

Computer Science 106

Computing in Engineering and Science | |

| |Spring 2006 Class number: 11672 Instructor: Larry Caretto |

Sixth Programming Exercise

Objective

This exercise should give you the ability to write code using the various repetition (looping) structures in C++: (1) the while statement, (2) the do-while statement, and (3) the for statement.

Background for exercise

During class, we have seen various formats for C++ if statements as shown below.

|The while statement |The for statement |

|while ( ) |for ( ; |

|{ |; |

|< loop body> | ) |

|} |{ |

| |< for block in which the value of the counter initialized and |

| |incremented in the for statement may be used in expressions> |

| |} |

| |< next statement executed after continuation condition in for |

| |statement becomes false> |

|The do-while statement | |

|do | |

|{ | |

|< loop body > | |

|} | |

|while ( ); | |

| | |

The in the structures above is the same Boolean condition that we saw in the if statement of C++. We can use the simple repetition or looping structures shown above within other control structures to produce expanded results with a minimum of code.

Overview of the exercise

This exercise has four tasks. The first task simply requires you to copy the code from the assignment and run it. The code is the completion of the example started in class of computing a table of kinetic energy as a function of mass and velocity. In task two you will modify this to create a two-way table for another equation. Tasks three and four show the use of looping to compute sums of infinite series, an important application in engineering and science calculations. These tasks are closely related. In task four you will use a for loop to accomplish the same calculations for which a while loop is used in task three. The instructions below discuss the items that you should place on the submission file for this assignment. Make sure that your name and your lab day are on the top of your submission file.

Specific tasks

Task one: Nested loops – During class we discussed the use of nested loops to compute a table of kinetic energy. During the lecture, we did not complete the code to include full output. The full code with the output section is shown below.

/*

Comp 106 Programming exercise six

Name: Date:

Task one: Kinetic Energy Table

*/

#include

#include

using namespace std;

int main()

{

// Print table description and V values for each column

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download