You can replace this text by going to "Layout" and then "Page Elements" section. Edit " About "

24/03/14

  • 5.11 Figure 5.23 Program to draw a moving ball
#include <cstdlib>
#include <iostream>
#include <graphics.h>
#define TRUE 1

using namespace std;

int main(void)
{
    const int PAUSE=20;
    const int DELTA=5;
    const int RADIUS=30;
    const int COLOR=RED;
   
    int width,height,x,y,stepX,stepY;
    width = getmaxwidth();
    height = getmaxheight();
    initwindow(width, height,"Pong - close window to quit",0,0,TRUE);
    x = RADIUS;
    y = RADIUS;
    stepX = DELTA;
    stepY = DELTA;
   
    while (TRUE){
          clearviewport();
          setfillstyle(SOLID_FILL, COLOR);
          fillellipse(x, y, RADIUS, RADIUS);
         
          swapbuffers();
          delay(PAUSE);
         
          if(x<= RADIUS)
                 stepX = DELTA;
          else if (x>= width - RADIUS)
               stepX = -DELTA;
              
          if(y <= RADIUS)
               stepY = DELTA;
          else if (y >= height - RADIUS)
               stepY + -DELTA;
              
          x = x + stepX;
          y = y + stepY;
          }
   
    closegraph();
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.2 Figure 5.2 Program Fragment With a Loop
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int count_emp=0,hours;
float rate,pay;

while(count_emp<7){
    cout<<"Hours> ";
    cin>>hours;
    cout<<"Rate> ";
    cin>>rate;
    pay= hours*rate;
    cout<<"Pay is= $ "<< pay <<endl;
    count_emp=count_emp+1;
}

 cout<<"All employees processed"<<endl;
 
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.3 Figure 5.4 Program to Compute Company Payroll
include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int count_emp=0, number_emp;
    double rate,pay,total_pay=0,hours;
   
    cout<<"Enter number of employees> ";
    cin>>number_emp;
   
    while(count_emp<number_emp){
    cout<<"Karyawan "<<count_emp+1<<endl;
    cout<<"Hours> ";
    cin>>hours;
    cout<<"Rate> ";
    cin>>rate;
    pay= hours*rate;
    cout<<"Pay is= $ "<< pay<<endl;
    total_pay=total_pay+pay;
    count_emp=count_emp+1;
}

 cout<<"All employees processed"<<endl;
 cout<<"Total payroll is " <<total_pay<<endl; 
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.4 Figure 5.5 Using a for Statement in a Counting Loop
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int count_emp, number_emp;
    float rate,pay,total_pay=0.0,hours;
   
    cout<<"Enter number of employees> ";
    cin>>number_emp;
    for (count_emp=0;count_emp < number_emp; count_emp += 1) {
    cout<<"Hours> ";
    cin>>hours;
    cout<<"Rate> $";
    cin>>rate;
    pay= hours*rate;
    cout<<"Pay is= $ "<< pay<<endl;
    total_pay=total_pay+pay;
}

 cout<<"All employees processed"<<endl;
 cout<<"Total payroll is " <<total_pay<<endl; 
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.4 Figure 5.5 Using a for Statement in a Counting Loop
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int count_emp, number_emp;
    float rate,pay,total_pay=0.0,hours;
   
    cout<<"Enter number of employees> ";
    cin>>number_emp;
    for (count_emp=0;count_emp < number_emp; count_emp += 1) {
    cout<<"Hours> ";
    cin>>hours;
    cout<<"Rate> $";
    cin>>rate;
    pay= hours*rate;
    cout<<"Pay is= $ "<< pay<<endl;
    total_pay=total_pay+pay;
}

 cout<<"All employees processed"<<endl;
 cout<<"Total payroll is " <<total_pay<<endl; 
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.4 Figure 5.8 Displaying a Celsius-to-Fahrenheit Conversion Table
#include <cstdlib>
#include <iostream>
#define cbegin 10
#define climit -5
#define cstep 5
using namespace std;

int main(int argc, char *argv[])
{
    int celcius;
    double fahrenheit;
   
    cout<<"Celcius \tFahrenheit"<<endl<<endl;
   
    for (celcius=cbegin;celcius>=climit;celcius -= cstep) {
        fahrenheit= 1.8*celcius+32.0;
        cout<<"celcius "<<celcius<<"\tfahrenheit "<<fahrenheit<<endl;
        }
       
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.5 Figure 5.9 Program to Monitor Gasoline Storage Tank
#include <cstdlib>
#include <iostream>
#define capacity 80000.0
#define min_pct 10
#define gals_per_brl 42.0

double monitor_gas(double min_supplay,double start_supply);

using namespace std;

int main(void)
{
    double start_supply, min_supply, current;
    min_supply = min_pct/100.0*capacity;
    cout<<"Number of barrels currently in tank> ";
    cin>>start_supply;
  
    current = monitor_gas(min_supply,start_supply);
    cout<<"only barrels are left."<<current<<endl<<endl;
    cout<<"*** WARNING ***"<<endl;
    cout<<"Available supply is less than percent of"<<min_pct<<"tank's "<<endl;
    cout<<capacity<<"barrel capacity."<<endl;
  

  
    system("PAUSE");
    return EXIT_SUCCESS;
}

double monitor_gas(double min_supply, double start_supply)
{
       double remov_gals, remov_brls,current;
              for (current = start_supply; current >= min_supply; current -= remov_brls){
                  cout<<"barrels are available."<<current<<endl<<endl;
                  cout<<"Enter number of gallons removed> ";
                  cin>>remov_gals;
                  remov_brls = remov_gals/gals_per_brl;
                
                  cout<<"After removal of"<<remov_gals<<" gallone {"<<remov_brls<<" barrels)"<<endl;
                  }
                 return (current);
                 }
  • 5.6 Figure 5.10 Sentinel-Controlled while Loop
#include <cstdlib>
#include <iostream>
#define sentinel -99

using namespace std;

int main(int argc, char *argv[])
{
    int sum=0, score;
   
    cout<<"Enter first score(or "<< sentinel<<" to quit)> ";
    cin>>score;
    while (score != sentinel) {
          sum += score;
          cout<<"Enter next score ("<<sentinel<<"to quit)> ";
          cin>>score;
          }
    cout<<"Sum of exam scores is "<<sum<<endl;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.6 Figure 5.11 Batch Version of Sum of Exam Scores Program
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
    int sum=0,score,input_status;
   
    cout<<"Score=";

    cin>>score; return input_status;
   
   
    while (input_status != EOF) {
          cout<<"     "<<score<<endl;
          sum+=score;
            cin>>score; return input_status;
          }
   
    cout<<"Sum of exam scores is "<<sum;

    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.7 Figure 5.12  Program to Process Bald Eagle Sightings for a Year
#include <cstdlib>
#include <iostream>
#define SENTINEL 0
#define NUM_MONTHS 12


using namespace std;

int main(void)
{
    int month,mem_sight,sightings;
   
    cout<<"BALD EAGLE SIGHTINGS"<<endl;
    for(month=1;month<=NUM_MONTHS;month++){
        sightings=0;
        cin>>mem_sight;
    while (mem_sight != SENTINEL){
          if(mem_sight>=0)
             sightings += mem_sight;
          else
          cout<<"Warning, negative count ignored"<<mem_sight<<endl;
          cin>>mem_sight;
          }
         
          cout<<"month  :  "<<month<<sightings;
          }
         
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.7 Figure 5.13  Nested Counting Loop Program
#include <cstdlib>
#include <iostream>

using namespace std;

int main(void)
{
    int i,j;
    cout<<"\ti\tj"<<endl;
   
                for (i=1;i<4;++i){
                    cout<<"Outer      "<<i<<endl;
                for(j=0;j<i;++j){
                                 cout<<"Inner         "<<j<<endl;
                                 }
                    }
               
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.9 Figure 5.16  Using a Function Parameter
#include <cstdlib>
#include <iostream>

using namespace std;

    void evaluate ( double f(double f_arg), double pt1,double pt2,double pt3){
         cout<<" = "<<pt1<<f(pt1);
         cout<<" = "<<pt2<<f(pt2);
         cout<<" = "<<pt3<<f(pt3);
         }
int main(int argc, char *argv[])
{

    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 5.9 Figure 5.19 Finding a Function Root Using the Bisection Method
#include <cstdlib>
#include <iostream>
#include <math.h>

#define FALSE 0
#define TRUE 1
#define NO_ROOT -99999.0

double bisect(double x_left,double x_right, double epsilon, double f( double farg));
double g(double x);
double h(double x);

using namespace std;

int main(void)
{
    double x_left,x_right,epsilon,root;
   
    cout<<"Enter interval endpoints> ";
    cin>>x_left>>x_right;
    cout<<"Enter tolerance> ";
    cin>>epsilon;
   
    cout<<"Function g"<<endl;
    root = bisect(x_left,x_right,epsilon,g);
    if(root != NO_ROOT)
            cout<<"g       ="<<root<<g(root)<<endl<<endl;
            cout<<"Function h"<<endl;
            root= bisect(x_left,x_right,epsilon,h);
            if(root != NO_ROOT)
                    cout<<"h       =n"<<root<<h(root);
                   
    return(0);
}

double bisect(double x_left, double x_right, double epsilon, double f(double farg))
{
       double x_mid, f_left, f_mid, f_right;
      
       int root_found;
       f_left = f(x_left);
       f_right = f(x_right);
      
       if(f_left * f_right>0){
                 cout<<"May be no root in       "<<x_left<<x_right<<endl;
                 return NO_ROOT;
                 }
                
       root_found = FALSE;
       while(fabs(x_right - x_left) > epsilon && !root_found)
       {
           x_mid = (x_left + x_right)/2.0;
           f_mid = f(x_mid);
          
       if(f_mid == 0.0){
                root_found = TRUE;
                }
       else if (f_left * f_mid < 0.0) {
            x_right = x_mid;
            }
       else {
            x_left + x_mid;
            }
           
       if(root_found)
                     cout<<"Root found at x =      ,midpoint of       "<<x_mid<<x_right<<endl;
       else
           cout<<"New interval is       "<<x_left<<x_right;
           }
      
       return ((x_left + x_right)/2.0);
       }
      
       double g(double x) {
              return (5 * pow(x, 3.0) -2 *pow(x,2.0)+3);
              }
             
       double h(double x){
              return(pow(x, 4.0) - 3 * pow(x,2.0) - 8);
              };
  • 5.11 Figure 5.22 Program to draw a quilt
#include <cstdlib>
#include <iostream>

using namespace std;

int main(void)
{
    int x1,y1,x2,y2,stepX,stepY,foreColor,numBars,width, height;
   
    cout<<"Enter number of bars> ";
    cin>>numBars;
   
    width = getmaxwidth();
    height = getmaxheight();
   
    initwindow(width, height, "Quilt");
   
    x1=0;
    x2=width;
    y1=0;
    y2=height;
   
    stepX = width/(2 * numBars);
    stepX = height/(2 * numBars);
   
    for (int i= 1; i<= numBars; ++i){
        foreColor= i%16;
        setcolor(foreColor);
        setfillstyle(i%12, foreColor);
        bar(x1, y1, x2, y2);
        x1 = x1 + stepX;
        y1 = y1 + stepY;
        x2 = x2 - stepX;
        y2 = y2 - stepY;
        }
       
    closegraph();
    system("PAUSE");
    return EXIT_SUCCESS;
}

Artikel Terkait:

0 komentar :

Posting Komentar