<< Chapter < Page Chapter >> Page >

break; }

}

return 0;

}

2.3) Remove the break statements from the above program and then try it again and explain the result.

2.4) Use switch statement to rewrite the following code segment:

if (num == 1)

{cout<<“Alpha”; }

else if (num == 2)

{ cout<<“Beta”; }

else if (num == 3)

{ cout<<“Gamma”; }

else

{ cout<<“Other”; }

2.5) Write a program that inputs the integer variable n consisting of 3 digits and displays it in ascending order of digits.

Example: n = 291. It should be displayed as 129.

2.6) Write a program that inputs a date with correct month, year and day components, and then checks if the year is a leap year or not. Show the result on the screen.

2.7) Write a program that can calculate the fee for a taxi ride. The formula is as follows:

  • The first kilometer costs 5000.
  • Each next 200m costs 1000.
  • If the distance is more than 30km then each next kilometer adds 3000 to the fee.

The program has to input the total distance (in km) and calculate the charge.

2.8) Write a program that inputs a date consisting of day, month, and year components. Check if the date is valid or not and if it is, determine what its previous day is. Example: if the date is 01/01/2003 then its previous day is 31/12/2002.

Lab session 3: repetition structures

1. objective

The objectives of Lab 3 are to practice the C++’s repetition structures, such as:

  • for
  • while
  • do…while

2. experiment

2.1) Determine the result of the following code segment. Explain this result.

int a = 1;

while (a<4)

{

cout<<“This is the outer loop\n”;

a++;

while (a<= 25)

{

break;

cout<<“This prints 25 times\n”;

}

}

2.2) Test the following program.

#include<iostream.h>

#include<iomanip.h>

void main()

{

float total_grade=0.0;

float grade_avg = 0.0;

float grade;

int grade_ctr = 0;

do

{

cout<<“What is your grade? (-1 to end) “;

cin>>grade;

if (grade>= 0.0)

{

total_grade += grade; // Add to total.

grade_ctr ++;

} // Add to count.

} while (grade>= 0.0); // Quit when -1 entered.

grade_avg = (total_grade / grade_ctr);

cout<<“\nYou made a total of “<<setprecision(1)<<

total_grade<<“ points.\n”;

cout<<“Your average was “<<grade_avg<<“\n”;

if (total_grade>= 450.0)

cout<<“** You made an A!!”;

return 0;

}

2.3) Test the following program:

#include<iostream.h>

void main()

{

int outer, num, fact, total;

cout<<“What factorial do you want to see? “;

cin>>num;

for (outer=1; outer<= num; outer++)

{

total = 1;

for (fact=1; fact<= outer; fact++)

{ total *= fact; }

}

cout<<“The factorial for “<<num<<“ is “<<total;

return 0;

}

2.4) Determine the result of each following code segment:

a.

for (ctr=10; ctr>=1; ctr-=3)

{ cout<<ctr<<“\n”; }

b.

n =10;

i=1;

for (i = 0 ; i<n ; i++)

cout<<++i<<endl;

c.

for (i=1; i<=10; i++);

for (j=1; j<=5; j++)

{

if ( i == j )

continue;

else ( i>j)

break;

else

cout<<i<<j;

cout<<endl;

}

d.

i=1;

start=1;

end=5;

step=1;

for (; start>=end;)

{

cout<<i<<“\n”;

start+=step;

end--;

}

2.5) Write a C++ program to convert Celsius degrees to Fahrenheit. The Celsius degrees increase from 5 to 50 with the increment of 5 degrees. The resultant table should be in the following form with appropriate headings:

Fahrenheit = (9.0 / 5.0) * Celsius + 32.0;

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask