CMPT 130 Lab 3 - Loops

 

In this lab you are to print all the four digit integers (i.e. numbers between 1,000 and 9,999) where the sum of the digits is 27 and where the value of each digit is different. Examples of numbers that should appear in the output are 3,978 and 9,648.

 

Labs are assessed so make sure that the TA has seen, and marked, your finished work before you leave the lab.

 

Finding Digits

Unless you are familiar with this sort of thing the fiddliest part of this is may be finding the four digits that make up each number. Fortunately, the way in which integer division works makes this relatively simple. I would suggest creating an integer variable for each of the digit positions. I called mine thousands, hundreds, tens and ones as these are appropriately descriptive variable names.

 

Example Calculation

Let’s look at the number 3,776 as an example.

 

1.      Using integer division 3,776 / 1,000 = 3 so that is the thousands.

2.      We can follow a similar process for the hundreds except that we have to first subtract 3,000, fortunately we can multiply the number of thousands by 1,000 to compute this: (3,776 – (3,776 / 1,000) * 1,000) / 100 = 7

3.      The tens is very similar: (3,776 – (3,776 / 100) * 100) / 10 = 7

4.      And the ones is left as an exercise.

 

Writing the Loop

Write a for loop to test each of the four digit integers. The first number will be 1,000 and the last 9,999. That means the loop control variable, let’s call it i, starts off as 1,000 and we want to add one to i after each loop iteration. The loop should terminate when i becomes 10,000. Recall that a for loop contains three statements in brackets, separated by semi-colons and preceding the loop body.

 

for(<initialization>; <condition>; <increment>) {

<loop body>

}

 

where C++ statements should replace the <>s.

 

The initialization statement should declare i and initialize it to 1,000. The condition should check that i is less than 10,000 and the increment statement should add one to i.

 

Testing Each Value

In the loop body you need to determine whether or not each number should be printed. You are to only print numbers whose digits are different from each other and that sum to 27. We’ve already discussed how you can find the digits so this means that you need to write if statements to test these conditions.

 

You can write these test as a single (and ugly) condition or as two conditions in two nested if statements. I’d choose the latter as it is going to look a lot cleaner.

 

Testing that the digits sum to 27 is fairly straightforward once you’ve calculated the separate digits so I’m not going to discuss it any further.

 

Testing that all the digits are different is conceptually simple but involves multiple not equal to (!=) comparisons. There really isn’t a shortcut to this and I would suggest writing it as one condition with multiple and clauses. The condition needs to only contain six comparisons.

 

Your output should just consist of numbers that meet both conditions, one number per line.

 

If You Have Time

If you have time, rewrite the loop as a while loop.

 

Assessment

1 mark for completion of the lab.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)