<< 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;

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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