Tools needed:
1.
PC running Windows Operating
System (OS)
2.
DevC++ (installed)
Turn on the PC and log into it using your student
account information.
Note:
The information and concepts discussed in this lab
manual are in sufficient detail but are still not exhaustive. It is assumed
that all these concepts are already taught to you in your ITC theory class.
1. Nested loops
Using a loop within a loop results in nested loops.
There is no limit on the level of nesting. So a loop within a loop within a
loop within a loop etc. can be used to any depth.
Example 1:
Suppose we wish to create a multiplication table as
follows:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
There are 5 rows and 10 columns. Note that each column
entry is obtained by multiplying the first column entry in that row by the
column number.
The following code snippet does the job.
for(int i = 0; i <5; i++)
{
for(int j = 0;
j < 10; j++)
cout
<<(i+1)*(j+1)<< '\t';
cout <<
endl;
}
Note that the first loop iterates 5 times, whereas for
each iteration of the outer loop, the inner loop iterates 10 times, as
required. Thus the cout statement inside the inner loop gets executed
times. The cout
statement to print newline is part of the outer loop and hence it executes only
5 times.
To test the concepts discussed in this lab manual
write C++ code for as many of the following tasks as possible. Try to write
code for the same task using different types of loops.
Task # 1: Code
file name lab08_1.cpp
Write
a program to receive one integer value 'num' from user such that the number is
greater than 0 but less than 10. Then draw a triangle on screen such that there
is one number in first row, two in second, three in third and so on in an
increasing fashion until num rows are printed.
For
example if the user inputs 5, the output pattern is:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
#include <iostream>
using namespace std;
int
main()
{
int
i,j,num,n=1;
cout<<"input
:";
cin>>num;
for
(i=1;i<=num;i++)
{
for
(j=1;j<=i;j++)
{
cout<<n<<'\t';
n++;
}
cout<<endl;
}
return
0;
}
Task # 2: Code
file name lab08_2.cpp
Write
a program to receive one integer value 'num' from user such that the number is
greater than 0 but less than 20. Then draw a right justified triangle on screen
such that there are increasing rows of intersperced ‘*’ and ‘@’ characters in
first row, two in second, three in third and so on till num on line number num.
For
example if the user inputs 5, the output pattern is:
*
*@
*@*
*@*@
*@*@*
#include <iostream>
using namespace std;
int
main()
{
int
i,j,num,n,k,x;
char
v;
cout<<"input
: ";
cin>>num;
x=num;
for (i=1;i<=x;++i)
{
for (j=1;j<=num-1;++j)
{
cout <<" ";
}
num = num-1;
for
(k=1;k<=i;k=k+1)
{
n=k%2;
v=(n!=0) ? '*':'@';
cout<<v;
}
cout << endl;
}
return 0;
}
Task # 3: Code
file name lab08_3.cpp
Write
a program to display below pattern:
$*****
*$****
**$***
***$**
****$*
*****$
*$****
**$***
***$**
****$*
*****$
#include <iostream>
using namespace std;
int main()
{
int
i,j,num,n=1;
for
(i=1;i<=6;i++)
{
for
(j=1;j<=6;j++)
{
if
(i==j)
{
cout<<"$";
}
else
{
cout<<"*";
}
}
cout<<endl;
}
return
0;
}
Task # 4: Code
file name lab08_4.cpp
Write
a program to make the following tables-triangle on screen. Use loop nesting to
do the job.
1 2 3 4 5 6 7 8 9 10
4 6 8 10 12 14 16 18 20
9 12 15 18 21 24 27 30
16 20 24 28 32 36 40
25 30 35 40 45 50
36 42 48 54 60
49 56 63 70
64 72 80
81 90
100
#include
<iostream>
using namespace
std;
int main()
{
int x=1,k=10,y=0,n=1,m;
while (x<=k)
{
while (x<=y)
{
cout
<< "\t";
x++;
}
y=y+1;
m=y;
x=1;
while (x<=k)
{
cout
<<n*m<<"\t";
m=m+1;
x=x+1;
}
n=n+1;
k=k-1;
x=1;
cout<<endl;
}
return 0;
}
Task # 5: Code
file name lab08_5.cpp
Try
your hand at generating the below shape:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
#include
<iostream>
using namespace std;
int main()
{
int i,j;
for (i=1;i<=5;i++)
{
for (j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
for (i=4;i>=1;i--)
{
for (j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
No comments:
Post a Comment