You are on page 1of 12

TDB2073 Structured Programming & Database Systems

Lab/Tutorial Exercise 5 – Repetition Structure II


Semester SEP 2018

1. Find the error(s) in each of the following:


a. For ( x = 100, x >= 1, x++ )
cout << x << endl;

Ans: for (int x = 100; x >= 1; x-- )


cout << x << endl;

b. The following code should print whether integer value is odd or even:

switch ( value % 2 )
{
case 0:
cout << "Even integer" << endl;
case 1:
cout << "Odd integer" << endl;
}

Ans:

int value;

cout<<”Enter an integer:”;
cin>>value;

value = value % 2;

switch ( value )
{
case 0:
cout << "Even integer" << endl;
break;
case 1:
cout << "Odd integer" << endl;
}

c. The following code should output the odd integers from 19 to 1:

for ( x = 19; x >= 1; x += 2 )


cout << x << endl;

Ans: for (int x = 19; x >= 1; x -= 2)


cout << x << endl;

d. The following code should output the even integers from 2 to 100:
counter = 2;

do
{
cout << counter << endl;
counter += 2;
} While ( counter < 100 );

Ans:
int counter = 2;

do
{
cout << counter << endl;
counter += 2;
} while ( counter <= 100 );

2. Determine the output of the following codes.


a. for (int i=1; i!=5; i++)
cout << i + 2 * i;
cout << endl;

Ans: 36912

b. int x = 1;
while (x < 10) {
x = x + 2;
cout << x << endl;
while (x % 2 != 0)
x = x + 1;
}
Ans:

c. int x = 1;
while (true) {
cout << x++ << endl;
if (x > 3)
break;
}
cout << x + 1 << endl;

3. Rewrite the following code using only while loops as the looping structure.
int a = 5;
do {
cout << "a" << endl;
for (int i=0; i<a; i++)
cout << i << endl;
for (int j=a; j>=3; j--)
cout << a << endl;
a = a + 1;
} while (a <= 7);

Ans:
int a = 5;
while (a <= 7)
{
cout << "a" << endl;
int i=0;
while (i<a)
{
cout << i << endl;
i++;
}
int j=a;
while (j>=3)
{
cout << a << endl;
j--;
}
a = a + 1;
}

4. Correct the following C++ program so that it will display the multiplication table
based on the number entered by user. (Hint: copy the code into your C++ editor and
compile the program. Debug the program (i.e., correct errors) until it is error-free and
gives the desired output).

#include <iostream>
using namespace std;
int main(){
cout << "Enter a number : ";
cin >> no;
cout << "The multiplication table for : " << no << endl;
for (int i=1; I<12; i++) {
cout << i << " x " << no << " = " << i x no << endl;
return 0;
}

Ans:

#include <iostream>
using namespace std;
int main(){
int no;
cout << "Enter a number : ";
cin >> no;
cout << "The multi1plication table for : " << no << endl;
for (int i=1; i<=12; i++)
cout << i << " x " << no << " = " << i * no << endl;
return 0;
}

5. Write a program that prints the following diamond shape. You may use output
statements that print either a single asterisk (*) or a single blank. Maximize your
use of repetition (with nested loop statements) and minimize the number of
output statements.

*
***
*****
*******
*********
*******
*****
***
*

Ans:
#include <iostream>

using namespace std;

int main()
{
int space,row;

for (int i=1; i<=5; i++)


{
for (int j=5; j>=i; j-- )
{
cout<<" ";
}

for (int k=0;k != 2*i-1;++k )


{
cout<<"*";
}

cout<<endl;
}

for (int i=4; i>=1; i--)


{
for (int j=5; j>=i; j-- )
{
cout<<" ";
}

for (int k=0;k != 2*i-1;++k )


{
cout<<"*";
}

cout<<endl;
}

return 0;
}
6. The process of finding the largest number (i.e., the maximum of a group of numbers)
is used frequently in computer applications. For example, a program that determines
the winner of a sales contest inputs the number of units sold by each salesperson. The
salesperson who sells the most units wins the contest. Write a pseudocode program,
then a C++ program that uses a while statement to determine and print the largest
number of 10 numbers input by the user. Your program should use three variables, as
follows:

Counter:
A counter to count to 10 (i.e., to keep track of how many numbers have been input
and to determine when all 10 numbers have been processed).

Number:
The current number input to the program.

Largest:
The largest number found so far.

Ans:
#include <iostream>

using namespace std;

int main()
{
int counter,number,largest;
cout<< "Enter 10 numbers: ";
cin>>largest;

counter=2;
while (counter<=10 )
{
cin>>number;
if (number > largest)
largest= number;
counter++;
}

cout<< "Largest number: "<<largest;

return 0;
}

7. Develop a C++ program that uses a while statement to determine the gross pay for
each of several employees. The company pays "straight time" for the first 40 hours
worked by each employee and pays "time-and-a-half" for all hours worked in excess
of 40 hours. You are given a list of the employees of the company, the number of
hours each employee worked last week and the hourly rate of each employee. Your
program should input this information for each employee and should determine and
display the employee's gross pay.
Enter hours worked (-1 to end): 39
Enter hourly rate of the worker ($00.00): 10.00
Salary is $390.00

Enter hours worked (-1 to end): 40


Enter hourly rate of the worker ($00.00): 10.00
Salary is $400.00

Enter hours worked (-1 to end): 41


Enter hourly rate of the worker ($00.00): 10.00
Salary is $415.00

Enter hours worked (-1 to end): -1

Ans:
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int h;
double m,s;

while (h!=-1)
{

cout<< "Enter hours worked (-1 to end): ";


cin>>h;
cout<<fixed<<setprecision(2);

if (h==-1) return 1;
cout<< "Enter hourly rate of the worker ($00.00): ";
cin>>m;
if (h<40)
s=h*m;

else
{
h=h-40;
s=40*m + h*m*1.5;
}
cout<< "Salary is $"<<s<<endl;
cout<<"\n";
}

cout<<endl;

return 0;
}
8. One large chemical company pays its salespeople on a commission basis. The
salespeople each receive $200 per week plus 9 percent of their gross sales for that
week. For example, a salesperson who sells $5000 worth of chemicals in a week
receives $200 plus 9 percent of $5000, or a total of $650. Develop a C++ program
that uses a while statement to input each salesperson's gross sales for last week and
calculates and displays that salesperson's earnings. Process one salesperson's figures
at a time.
Enter sales in dollars (-1 to end): 5000.00
Salary is: $650.00

Enter sales in dollars (-1 to end): 6000.00


Salary is: $740.00

Enter sales in dollars (-1 to end): 7000.00


Salary is: $830.00

Enter sales in dollars (-1 to end): -1

Ans:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double h,s;
while (h!=-1)
{

cout<< "Enter sales in dollars (-1 to end): ";


cin>>h;
cout<<fixed<<setprecision(2);

if (h==-1) return 1;

s=200 + 0.09*h;

cout<< "Salary is $"<<s<<endl;


cout<<"\n";

cout<<endl;

return 0;
}

9. The sequence is a series of numbers where a number is found by adding up the two
numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, and so forth. Written as a rule, the expression is xn = xn-1 + xn-2. Write a C++
Program that generates Fibonacci Series up to n terms where n is entered by the user.
N++

Ans:
#include <iostream>

using namespace std;


int main()
{
int n,t1=0,t2=1,next_term;
cout<<"Enter a number: ";
cin>>n;

cout<<"Fibonacci sequence: ";


for (int i=0; i<n; i++)
{
if (i==0 || i==1)
{
cout<<i<<",";
}
else{

next_term=t1 + t2;
t1=t2;
t2=next_term;
cout<<next_term<<",";
}
}

return 0;
}