Tuesday, March 6, 2012

STRUCTURED PROGRAMMING (Version 3.2)

Chapter 1 page 18
Write algorithms using Pseudocode
Design a program to calculate a person's age in months, and then in days. You are given the age in years.
  • What are the inputs needed by the program?
  • What does the program have to calculate?
  • what results must the program output?
2nd step: Define the input, Computation and Outputs

input :               Name
                        Age(in Years)

Computation:    Age in Months = 12 x Age
(Processing)     Age in Days = 365.25 x Age

Outputs:           Name
                        Age (in years)
                        Computed Values

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
void keep_window_open() { char ch; cin>> ch;}

int main()
{
 //----prompt the user to enter char from the keyboard----
 cout<<"\nPlease enter your name:";  //---read in name from keyboard---
 string name;
 cin >> name;
 
 //----prompt the user to enter values for age from the keyboard----
 int age, months;                    //--- Variable Declaration ---
 double days;                        //--- Variable Declaration ---
 cout<<"\nPlease enter your age:";   //---read in age from keyboard---
 cin >> age;
 
 //---compute age in months and age in days---
 months = 12 * age; //com            //calculate Age in months---
 days = 365.25* age;                 //calculate age in days---
 
 //----compute and display the name and values----
 cout <<"\nHi "<< name << "\nyour age is:" << age <<"\nyour age in months is:"
  << months << "\nand your age in days is: " << days;
 
 keep_window_open();
 return 0;
}

Exercise 2. Page 174.
10. In the following questions, apply the design & development process. Give both the first and second level pseudocode and flowcharts.
a. The volume of a cylinder is given by the following equation:

volume =  πr^2l
r is the radius

l is the length of the cylinder


Write a program that will prompt the user to enter the radius and the length of the cylinder. The program will calculate and display the volume.

#include <iostream>
#include <cmath>     //  pow, sqrt, sin ,log
#include <iomanip>  // setprecision (set decimal place) setw (set right or left justific) 
using namespace std;
#define PI 3.141592654   // declaration and assginment of a constant
void keep_window_open() { char ch; cin>> ch;}

int main()
{
 //----variable declaration----
 double radius, length, volume;

 cout <<"***Program that Calulates the volume of the cylinder***\n";
 //----prompt the user to enter the radius and length from keyboard----
 cout <<"\nPlease enter the radius of the cylinder: ";
 cin >> radius;
 cout <<"\nPlease enter the length of the cylinder: ";
 cin >> length;
 
 //--- compute the volume ---
 volume = PI* pow(radius,2)* length;
 
 //----display the volume of the cylinder----
 cout <<"\nThe volume of the cylinder is " << fixed << setprecision(2) << right << setw(2) << volume ;

 keep_window_open();
 return 0;
}

b. Enhance the above program such that it also calculates and displays the total surface area of the cylinder. The total surface area of the cylinder is given by the following equation:

 total surface area = 2πr^2 + 2πrl

#include <iostream>
#include <cmath>     //  pow, sqrt, sin ,log
#include <iomanip>  // setprecision (set decimal place) setw (set right or left justific) 
using namespace std;
#define PI 3.141592654   // declaration and assginment of a constant
void keep_window_open() { char ch; cin>> ch;}

int main()
{
 //----variable declaration----
 double radius, length, volume, area;

 cout <<"***Program that Calulates the volume of the cylinder***\n";
 //----prompt the user to enter the radius and length from keyboard----
 cout <<"\nPlease enter the radius of the cylinder: ";
 cin >> radius;
 cout <<"\nPlease enter the length of the cylinder: ";
 cin >> length;
 
 //--- compute the volume and area---
 volume = PI* pow(radius,2)* length;
 area = (2 * PI * pow(radius,2)) + (2 * PI * radius * length);
 
 //----display the volume and area of the cylinder----
 cout <<"\nThe volume of the cylinder is " << fixed << setprecision(2) << right << setw(2) << volume;
 cout <<"\nThe total surface area of the cylinder is " << fixed << setprecision(2) << right << setw(2) << area;
 keep_window_open();
 return 0;
}

Chapter 3. Page 49

Complete the following program which converts time in minutes to hours and minutes. E.g. 150mins is 2:30

#include <iostream>
using namespace std;

int main ()
{
      int total_mins, hours, mins;
      cout <<" Enter time in minutes: ";
      cin >> total_mins;
      hours = _______________________________;
      mins = ________________________________;
      cout<< ________________________________;
      return 0;
}


#include <iostream>
using namespace std;

int main ()
{
 int total_mins, hours, mins;
 cout <<" Enter time in minutes: ";
 cin >> total_mins;
 hours = total_mins / 60;  //--- compute total hours
 mins = total_mins % 60;  //--- " % " is remainder //--- compute total minutes
 cout<<"\nTotal time in hours and minutes is: " << hours << "h" << ":" << mins <<"m"; 
 return 0;
}



Exercise 3. Page 177
11a. The equivalent resistance of two resistors connected in parallel is given by the equation:
        R = R1*R2/(R1+R2)
Write a program that prompts the user to enter the value of the two resistors. The program then calculates and displays the equivalent resistance.


#include <iostream>
using namespace std;
void keep_window_open() {char ch; cin >> ch;}

int main ()
{
 //----variable declaration----
 double R, R1, R2;

 //----prompt the user to enter the value of the two resistors
 cout <<"\nEnter the value of the two resistors connected in parallel\n";
 cin >> R1 >> R2;

//--- compute the value of the two resistors connected in parallel---
 R = (R1 * R2) / (R1 + R2);

//----display the equivalent resistances in parallel----
 cout<<"\nThe equivalent resistance of two resistors connected in parallel is: " << R << " Ohms";
 keep_window_open();
 return 0;
}


11b. Write a program that calculates the equivalent resistances of two resistors connected in series and parallel. The user will be prompted to enter the values of the two resistors. A sample run of the code i shown below:

Program to calculte equivalent resistances in series and parallel.


Enter the value of the first resistor : 5
Enter the value of the second resistor: 20


The equivalent resistance in series is 25 Ohms.
The equivalent resistance in parallel is 4 Ohms.


#include <iostream>
using namespace std;
void keep_window_open() {char ch; cin >> ch;}

int main ()
{
 //----variable declaration----
 double PR,SR,R1, R2;

 //----prompt the user to enter the value of the two resistors
 cout <<"Enter the value of the first resistor: ";
 cin >> R1;
 cout <<"Enter the value of the second resistor: ";
 cin >> R2;

//--- compute the value of the two resistors connected in series and parallel---
 SR = R1 + R2;               //---series 
 PR = (R1 * R2) / (R1 + R2); //---parallel

//----display the equivalent resistances in series and parallel----
 cout<<"\nThe equivalent resistance in series is: " << SR << " Ohms";
 cout<<"\nThe equivalent resistance in parallel is: " << PR << " Ohms";
 keep_window_open();
 return 0;
}

Chapter 4. Page 73
Write a program to read in a temperature value from the keyboard in degrees Centigrade. If the temperature is less than or equal to zero, it output the word ICE. If it is greater than zero and less 100, it output the word WATER. if it is greater than or equal to 100, it output the word STEAM. Note that this program uses nested if-else statements.


# include <iostream>
using namespace std;
void keep_window_open () { char ch; cin >> ch;}

int main ()
{

 //---variable declaration---
 int temperature;

 //---prompt the user to enter a value for temperture from keyboard
 cout <<"Enter a temperature value: ";
 cin >> temperature;

 if ( temperature <= 0)   //<-- Condition 1
 {
  cout <<"\nICE";
 }
 else 
  {
   if (temperature > 0 && temperature < 100) //<-- Condition 2
   {
    cout <<"\nWATER";
   }
   else
   {
    cout <<"\nSTEAM";
   }
  }
   keep_window_open ();
   return 0;
}

Chapter 4. Page 82
Write a program to calculate the total weekly pay which is dependent on the number of hours worked.
The normal rate is $4.00/hr.
If the hours worked exceed 40 in the week, they will be considered as overtime and will be paid at 1.5 times the normal rate.

Test Data
Hours
Normal Pay
OT Pay
Total Pay
20
20 * 4 = 80
0
80
50
40 * 4 = 160
10 * 6 = 60
220


#include <iostream>
using namespace std;
void keep_window_open () { char ch; cin >> ch;}

//---variable declaration---
int hours, Total_Pay , Normal_Pay;

int main ()
{
//---Read in hours---
 cout <<"Enter the number of hours worked: ";
 cin >> hours;

//---Find the normal rate---
 if ( hours <= 40)
 {
  Total_Pay = hours * 4;
 }
 else //---Find the overtime and normal rate---
 {
  Normal_Pay = 40 * 4;
  Total_Pay = Normal_Pay + ((hours - 40) * 6);
 }
//---Output the total pay---
 cout <<"\nYour total pay is: " << Total_Pay;

 keep_window_open ();
 return 0;
}

Exercise 4. Page 180
6. Write a menu driven program that calculates the voltage, current or resistance using the Ohm's Law ( V = I*R).
The Program first display a menu prompting the user to enter the choice of calculation. If he chooses voltage calculation, he will then be asked to enter the value of current and resistance. If he chooses current, he will be prompted to enter voltage and resistance and so on. A sample run of the program is given below:

Ohms Law


1.     Voltage Calculation.
2.     Current Calculation.
3.     Resistance Calculation.


Enter your choice : 3

Resistance Calculation

=================
Enter voltage: 12
Enter current: 1.5


The resistance is 8 Ohms


(Use a switch statement for selection construct)


#include <iostream>
using namespace std;
void keep_window_open () { char ch; cin >> ch;}

int choice; 
double v, i, r;

int main ()
{
 cout <<" Ohms Law";

 cout <<"\n1.\tVoltage Calculation.";
 cout <<"\n2.\tCurrent Calculation.";
 cout <<"\n3.\tResistance Calculation.";

 cout <<"\n\nEnter your choice : ";
 cin >> choice;

 switch (choice)
 {
 case 1 : cout <<"\nVoltage Calculation\n";
  cout <<"=================\n";
  cout <<"Enter current : ";
  cin >> i;
  cout <<"Enter resistance: ";
  cin >> r;
  v = i * r;
  cout <<"\nThe voltage is "<< v << " V"; 
  break;

 case 2 : cout <<"\nCurrent Calculation";
  cout <<"\n=================\n";
  cout <<"Enter voltage : ";
  cin >> v;
  cout <<"Enter resistance: ";
  cin >> r;
  i = v / r;
  cout <<"\nThe current is "<< i << " A"; 
   break;

 case 3 : cout <<"\nResistance Calculation";
  cout <<"\n=================\n";
  cout <<"Enter voltage : ";
  cin >> v;
  cout <<"Enter current: ";
  cin >> i;
  r = v / i;
  cout <<"\nThe resistance is "<< r << " Ohms"; 
   break;
 }
 keep_window_open ();
 return 0;
}

Exercise 4 . Page 180
7. A retail company pays its salesmen with a basic salary and a commission. The basic salary is $800 per month. The commission has two different levels as shown below:
   Sales of 1 to 99; commission is $2.00 each
   Sales of 100 or more; commission is $2.20 each
Write a program that calculates the total salary based on the sales.

Total Salary Calculation


Enter your sales: 20


Basic salary is $800.00
Sales Commission is $40.00
Total Salary is $840.00



#include <iostream>
using namespace std;
void keep_window_open () { char ch; cin >> ch;}

int sales;
double Basic_salary, Sales_Commission, Total_Salary;
int main ()
{
 cout <<"Total Salary Calculation";

 cout <<"\n\nEnter your sales : ";
 cin >> sales;

 if (sales <=99)
 {
  Sales_Commission = sales * 2;
 }
 else 
 {
  Sales_Commission = sales * 2.2;
 }

 Basic_salary = 800.00;
 Total_Salary = Sales_Commission + Basic_salary;  
 
 cout <<"\nBasic salary is "<< "$"<<Basic_salary;
 cout<<"\nSales Commission is "<< "$"<<Sales_Commission;
 cout <<"\nTotal Salary is "<< "$"<<Total_Salary;

 keep_window_open ();
 return 0;
}



or



#include <iostream>

using namespace std;

int main()
{
    double sales,basic=800,commission,total;

    cout << "Total Salary Calculation" <<endl;
    cout << "Enter your sales : ";
    cin >> sales;

    if (sales < 100) commission = 2.0;
    else commission = 2.2;

    total = basic + sales*commission;

    cout << " Bacis salary is " << basic << endl;
    cout << " Sales commission is " << sales*commission << endl;
    cout << " Total Salary is " << total << endl;



    return 0;
}







Exercise 5. Page 182.
Write a program, which prompts the user to enter an integer. The program then displays the corresponding multiplication table.
A sample run is shown below:

Enter an integer: 8

8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

int main()
{
 int i=1, num;
 cout <<" Please enter a number:";
 cin >> num;
 for ( i=1; i < 11 ; i++)
 {
  cout << num <<"* "<< i << "=" << num*i <<"\n";
 }
}

5b. Modify your program in (a) above so that after displaying the multiplication table, the program repeats, asking the user to enter another number. If the number entered is non-zero, the multiplication table for the number is displayed and the program repeats. The program terminates if the number entered is zero.

#include <iostream>

using namespace std;
void keep_window_open() { char ch; cin>> ch;}

int main()
{
 //---- variable Declaration---
 int i=1, num;
 
 //prompt user to enter number
 cout <<" Please enter a number:";
 cin >> num;
 
 //While Number not equal to 0
 while ( num !=0)
 {
 for ( i=1; i < 11 ; i++)
 {
 cout << num <<"* "<< i << "=" << num*i <<"\n";
 }
 cout <<" Please enter another number (0 to terminate):";
 cin >> num;
 }
 keep_window_open ();
 return 0;
}

Exercise 7. Page 184
3. Write a program that will analyze, for your class, the grades obtained by all the students for Structured Programming. The program will prompt the user to  enter the grade for each student. Valid grades are A,B, and C. The program calculates and displays the total number of As, Bs and Cs. The user should be able to enter the grades in uppercase or lowercase. You may assume there are only 10 students in your class.

Your program must be modular. Write a function to read and total the grades and other to print the result.
A skeleton of program is given below:
char grade; //--global variables
int totalA, totalB, totalC;

int mian ()
{
   ReadandTotalGrade();
   DisplayTotals();
   return 0;
}


#include <iostream>
using namespace std;
void keep_window_open () { char ch; cin >> ch;}

//---function prototype
void ReadandTotalGrades(void);
void DisplayTotals(void);

char grade; //--global variables
int totalA, totalB, totalC;

int main ()
{
 ReadandTotalGrades();
 DisplayTotals();
 keep_window_open ();
 return 0;
}

void ReadandTotalGrades(void)
{
 int i;
 for (i = 0; i < 10 ; i++)
    {
 cout <<"Enter the grade for student "<< i+1 << endl;
 cin >> grade;
 if ( grade == 'A' || grade == 'a' )
 {
  totalA++;
 }
 else
 {
  if (grade == 'B' || grade == 'b')
  {
   totalB++;
  }
  else
  {
   if ( grade == 'C' || grade == 'c')
   {
    totalC++;
   }
   else
   {
    cout <<"Enter only A / B / C!\n";
    i--;
      }
      }
  }
     }
 return;
}

void DisplayTotals(void)
{
 cout <<" total A: " << totalA << " A";
 cout <<" total B; " << totalB << " B";
 cout <<" total C; " << totalC << " C";
 return;
}

No comments:

Post a Comment