NUMERICAL ANALYSIS LAB-05
Assignment No. 03
LESSON 5 A
LAB 5.1: Working with the ‘While Loop’
Lab 5.1(a):
#include <iostream>
using namespace std;
int main()
{
char letter = 'a';
while (letter != 'x')
{
cout << "Please enter a letter" << endl;
cin >> letter;
cout << "The letter you entered is " << letter << endl;
}
return 0;
}
Lab 5.1(b):
#include <iostream>
using namespace std;
int main()
{
int month = 1;
float total = 0, rain;
cout << "Enter the total rainfall for month "
<< month << endl;
cout << "Enter -1 when you are finished" << endl;
cin >> rain;
while (rain != -1)
{
total += rain;
month++;
cout << "Enter the total rainfall in inches for month "
<< month << endl;
cout << "Enter -1 when you are finished" << endl;
cin >> rain;
}
if (month == 1)
cout << "No data has been entered" << endl;
else
cout << "The total rainfall for the "
<< month - 1
<< " months is " << total << " inches." << endl;
return 0;
}
Lab 5.2: Working with the ‘do-While Loop’
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number;
float cost;
char beverage;
bool validBeverage;
cout << fixed << showpoint << setprecision(2);
do
{
cout << "Hot Beverage menu " << endl;
cout << "A: Coffee $1.00" << endl;
cout << "B: Tea $0.75" << endl;
cout << "C: Hot Chocolate $1.25" << endl;
cout << "D: Cappuccino $2.50" << endl;
cout << "Enter the beverage A,B,C or D you desire" << endl;
cout << "Enter E to exit the program " << endl;
cin >> beverage;
switch (beverage)
{
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
validBeverage = true;
break;
default:
validBeverage = false;
break;
}
if (validBeverage == true)
{
cout << "How many cups would you like? " << endl;
cin >> number;
}
switch (beverage)
{
case 'a':
case 'A':
cost = number * 1.0;
cout << "The total cost is $ " << cost << endl;
break;
case 'b':
case 'B':
cost = number * 0.75;
cout << "The total cost is $ " << cost << endl;
break;
case 'c':
case 'C':
cost = number * 1.25;
cout << "The total cost is $ " << cost << endl;
break;
case 'd':
case 'D':
cost = number * 2.50;
cout << "The total cost is $ " << cost << endl;
break;
case 'e':
case 'E':
cout << "Please come again" << endl;
break;
default:
cout << "This option is not available" << endl;
cout << "Try again please" << endl;
cout << endl;
break;
}
} while (beverage != 'E' && beverage != 'e');
return 0;
}
Lab 5.3: Working with the ‘For Loop’
#include <iostream>
using namespace std;
int main()
{
int value;
int total = 0;
int number;
float mean;
cout << "Please enter a positive integer" << endl;
cin >> value;
if (value > 0)
{
for (number = 1; number <= value; number++)
{
total = total + number;
}
mean = static_cast<float>(total) / value;
cout << "The mean average of the first " << value
<< " positive integers is " << mean << endl;
}
else
cout << "Invalid input - integer must be positive" << endl;
return 0;
}
Lab 5.4: Working with the ‘Nested Loop’
#include <iostream>
using namespace std;
int main()
{
int numStudents;
float numHours, total, average;
int student,day = 0;
cout << "This program will find the average number of hours a day"
<< "That a student spent programming over a long weekend\n\n";
cout << "How many students are there ?" << endl << endl;
cin >> numStudents;
for(student = 1; student <= numStudents; student++)
{
total = 0;
for(day = 1; day <= 3; day++)
{
cout << "Please enter the number of hours worked by student "
<< student <<" on day " << day << "." << endl;
cin >> numHours;
total = total + numHours;
}
average = total / 3;
cout << endl;
cout << "The average number of hours per day spent programming by "
<< "student " << student << " is " << average
<< endl << endl << endl;
}
return 0;
}