Lab 5 PDF
Lab 5 PDF
Computational Problem
Solving (CS100)
Shafay Shamail, Arif Zaman and
Safee Ullah Chaudhary
CS 100 Lab 5
Lab 5 Objective:
Lab Guidelines
1. Make sure you get your work graded before the lab time ends.
2. You put all your work onto the LMS folder designated for the lab (i.e. “Lab05”) before the time the
lab ends.
3. Talking to each other is NOT permitted. If you have a question, ask the lab assistants.
4. If you are a hot-shot C++ expert, you are still not allowed to use any feature of C++ that has not
5. The objective is not simply to get the job done, but to get it done in the way that is asked for in the
lab.
CS 100 Lab 5
Fun with Numbers
Given two integers, each in the range 10..99, if there is a digit that appears in both numbers, such as the
2 in 12 and 23, the program should print the number. (Note: division, e.g. n/10, gives the left digit while
the % "mod" n%10 gives the right digit.)
Enter digit 1:
24
Enter digit 2:
45
The common digit is '4'
1=T1
2 3=T2
4 5 6=T3
7 8 9 10=T4
11 12 13 14 15=T5
Here is a program that we could use to list the first few triangular numbers.
int n=0, t=0;
// now update n and t, so that they are ready for the next number
n = 1;
t = 1;
cout << "T(" << n << ") = " << t << endl;
// now update n and t, so that they are ready for the next number
n = 2;
t = 3;
cout << "T(" << n << ") = " << t << endl;
// now update n and t, so that they are ready for the next number
n = 3;
t = 6;
cout << "T(" << n << ") = " << t << endl;
// now update n and t, so that they are ready for the next number
n = 4;
t = 10;
cout << "T(" << n << ") = " << t << endl;
As you can see, there is not much point to this program. We entered in all the values and then got the
computer to print them out. What would be nice is if we could replace the four blocks of four lines with
CS 100 Lab 5
some statements that are exactly the same. That way we could cut-and-paste the same block of four
lines four times, or a dozen times. Already the first and last line in every one of the blocks is the same.
But the two middle lines are different for each block.
Can you think of a way to rewrite the middle two lines in the block so that every block is identical?
Hint: Try to express the next value of n in terms of the previous values.
a) Once you have got the correct output, find the first twelve triangular numbers by just cutting and
pasting twelve times.
CS 100 Lab 5
In our previous labs, we have been doing cut-and-paste programming for a while.
Let us introduce (without much explanation for the moment), a new programming concept.
First a review (You can skim the first three points quickly because it is a review):
1) Declaration statements.
int x=1, y=19*19;
They start with the name of a type (int or double or string or other types that we haven’t
yet covered).
2) Assignment statements
x=x*2+2;
They start with the name of a variable, followed by an equal sign, followed by an expression.
An expression can be
a simple number (or string enclosed in quotes " ")
or it can be the name of a variable
or it can be two expressions joined with an operator
or it might be an expression enclosed within parentheses ( )
3) if statements
if (x<5) { cout << “small”; } else { cout << “big”; }
They start with an if followed by a set of parenthesis containing an expression that evaluates
to true or false. This is followed by a single statement or a set of one or more statements
enclosed in braces { }. A single statement following the if should not be another if (to avoid
confusion).
Optionally this can be followed by the word else followed by a single statement or a set of
statements enclosed in braces { }.
CS 100 Lab 5
Now we want to introduce a fourth kind of statement. Read this part carefully!
4) while statements
They look just like if statements, and behave a bit like them.
They start with a while followed by a set of parenthesis containing an expression that
evaluates to true or false. This is followed by a single statement or a set of one or more
statements enclosed in braces { }.
(Compare this to the if statement and you will see that the structure of the two statements is
identical.)
If the expression in the parenthesis of the while statement is false, then the while and the if
statement both do exactly the same thing. They skip over the following statements, doing nothing at all,
and go on to the next statement.
If the expression in the parenthesis of the while statement is true, then just like the if statement, the
statement (or group of statements) in front of the while will be run. At the end of this, there is a big
difference:
Note that if the expression is something like (3<5), then this will never become false.
Even if the expression is something like (x<50) this may never become false. The expectation is that
inside the statements of the while, something might happen that changes the value of x, so that x
becomes 50 or larger, and so the looping stops. Otherwise the computer will loop forever.
CS 100 Lab 5
So let us return to our triangular numbers:
int n=0, t=0;
while (n < 4)
{
// now update n and t, so that they are ready for the next number
n = ...;
t = ...;
cout << "T(" << n << ") = " << t << endl;
}
You can see that this loop will print the first few triangular numbers without any copy-pasting!
If we now want to print the 12th or the 2000th triangular number all we have to do is to change one
number in the program.
int main()
{ int x,y=7;
string s;
cout << "Enter a number x: "; cin >> x;
cout << "Enter a word s: "; cin >> s;
while (s != "quit")
b) For what values of ‘x’ and ‘s’ does the program print “unicorn”?
c) Once you are done with this table, work on rewriting this code into easier and more efficient if (…)
{ … } else if ( … ) { … } else if (…) { … } else { … } statements.
Remember that the output of you code should match the output of this table.