Question: PLEASE USE This lab will develop your skills in using repetition statements like while, for, and do-while. I. Learner Objectives: At the conclusion of this
PLEASE USE
This lab will develop your skills in using repetition statements like while, for, and do-while.
I. Learner Objectives:
At the conclusion of this programming assignment, participants should be able to:
Compose iterative statements (while, for, and do-while statements)
Compose decision statements ("if" conditional statements)
Create and utilize compound conditions
Apply top-down design for a given problem
II. Prerequisites:
Before starting this programming assignment, participants should be able to:
Analyze a basic set of requirements and apply top-down design principles for a problem
Document and comment a C program according to class standards
Summarize topics from Kochan Chapter 4 including:
o What is a for or while statement?
o What is a do statement?
o What is a break statement?
o What is a continue statement?
III. Overview & Requirements:
This page will be available at Blackboard as Lab 2: Repetition in Labs folder. Calling it up in a
browser will allow you to cut-and-paste the template below into your editor and save editing
time.
Plug your thumbdrive into your workstation. Give it a few seconds to be recognized by
Windows and then create a MinGW terminal emulator. In that emulator, create a new directory
for this lab and cd to it:
$ cd /E/cpts121
$ mkdir lab02
$ cd lab02
As always, keep all your work in this directory.
Mathematical Background (the 3n + 1 Problem)
In 1937, German mathematician Lothar Collatz made the following claim:
Take any number n. If it is even, divide it by two. If it is odd, multiply it by three and add one.
Repeat this procedure with the result. No matter what value of n you start with, you will
eventually end up with one.
1/22/2018 Lab 2 - Iterative Statements
This became known as the "Collatz conjecture" and, while no counterexample has ever been
found, it has yet to be proven despite years of mathematical research. It remains an unsolved
problem in mathematics.
In this assignment, you will implement code to allow the user to test this conjecture. Use this
page to test your initial values http://www.ericr.nl/wondrous/showsteps.html
IV. Part 1: collatz
In a file collatz.c fill in the following template:
/*
* Programmer:
* Class: CptS 121, Spring 2011
* Programming lab:
* Date:
* Description:
*/
#include
int main(void)
{
/*
* The main function should implement these steps:
*
* 1. Prompt the user to enter an integer n.
* 2. Check to make sure the integer is positive. If it isn't, print
* out an error message and exit.
* 3. Implement this pseudocode:
*
* as long as n is not equal to 1,
* print the value of n on a line
* if n is even,
* divide it by 2 and store the result back in n
* otherwise,
* mulitply it by 3, add 1, and store the back in n
* print the value of n on a line (it will be 1)
*/
return 0;
}
Compile the program with:
$ cc -Wall collatz.c -o collatz
Here's an example your compiled program should duplicate:
$ ./collatz
enter n: 3
3
10
5
16
8
4
2
1
1/22/2018 Lab 2 - Iterative Statements
V. Part 2: collatz_count
As it stands, collatz only tests a single value of n. In this part, you'll build a program that tests
it on a range of values from 1 to a user-provided maximum.
Create this program in a file collatz_count.c using this template:
/*
* Programmer:
* Class: CptS 121, Spring 2011
* Programming lab:
* Date:
* Description:
*/
#include
#include
int main(void)
{
/*
* The main function should implement these steps:
*
* 1. Prompt the user to enter a(n integer) maximum n value maxN.
*
* 2. Check to make sure maxN is positive. If it isn't, print out an
* error message and exit.
*
* 3. Print a column header with "initial n" and "count" column names.
*
* 4. Implement this pseudocode:
*
* for each value of initialN from 1 to maxN,
* set a counter to 0
* set n to initialN
* as long as n is not equal to 1,
* if n is even,
* divide it by 2 and store the result back in n
* otherwise,
* multiply it by 3, add 1, and store the result
back in n
* increment the counter
* print the values of initialN and the counter
*/
return 0;
}
(You may want to cut-and-paste from collatz.c to save typing.)
Note that you won't print out each n on each iteration of the loop, only the count of times the
loop is run.
1/22/2018 Lab 2 - Iterative Statements
Compile the program with:
$ cc -Wall collatz_count.c -o collatz_count
Here's a typical run:
enter maximum n: 10
initial n count
1 0
2 1
3 7
4 2
5 5
6 8
7 16
8 3
9 19
10 6
You're welcome to try other initial n values for yourself. If you come across one whose Collatz
sequence never reaches one, your program will loop indefinitely. If so, call the nearest
mathematician immediately!.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
