Tampilkan postingan dengan label Ebook Problem Solving and Program Design in C. Tampilkan semua postingan
Tampilkan postingan dengan label Ebook Problem Solving and Program Design in C. Tampilkan semua postingan

Kamis, 27 Maret 2014

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar
  • 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;

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar

  • 5.8 Figure 5.14  Validating Input Using do-while Statement
 #include <cstdlib>
#include <iostream>

using namespace std;

 int
 get_int (int n_min, int n_max) {
         int in_val,status,error;
         char skip_ch;

do {
    error=0;
    cout<<"Enter an integer in the range from "<<n_min<<endl;
    cout<<"to inclusive> "<<n_max;
    status = in_val;
  
    if (status != 1){
       error=1;
       cin>>skip_ch;
       cout<<"Invalid character >>"<<skip_ch;
       cout<<"Skipping rest of line."<<endl;
       }
       else if (in_val < n_min || in_val > n_max ) {
            error =1;
            cout<<"Number is not in range."<<in_val;
            }
       do
          cin>> skip_ch;
       while (skip_ch != ' ');
       }
       while (error);
     
       return (in_val);
       }
      

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar

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

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar
  • 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);
              };

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar

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

Compile :

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar
  • 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);
                 }
               
RUN :

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar

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

RUN :

Ebook Chapter 5 Problem Solving and Program Design in C

0komentar

  • 5.4 Figure 5.7 Function to Compute Factorial
#include <cstdlib>
#include <iostream>

using namespace std;

int faktorial(int n) {
    int i, product=1;
   
    for (i=n; i>1; --i) {
        product=product*i;
}      
    return product;

}
int main(int argc, char *argv[])
{
    int n;
    cout<<"Masukkan nilai= ";
    cin>> n ;
    cout<< faktorial(n) ;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}



RUN :

Ebook chapter 5 Problem Solving and Program Design in C

0komentar
  •   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;
 }


RUN :



Senin, 24 Maret 2014

Ebooks 3.6

0komentar
Algoritma :
1. Deklarasi
    r : integer (input angka acak )
    r,z : integer (output)

2. Deskripsi
    read (r)
    perulangan
     Proses c <- 1283
                a <- 106
                m <- r + 1
                x <- 1234
                z <- ((a*x)+c)%m
     Write (r,z)


Flowchart :


c++ :


Ebooks 3.5

0komentar
Algoritma :
1. Deklarasi
    Bil (integer)

2. Deskripsi
    Read (Bil)
    Bil <- bil /2
    Bil <- bil /3
    Write : Faktorial Prima


Flowchart :


c++ :


Ebooks 3.4

0komentar
Algoritma :
1. Deklarasi
    n : integer (input)
    n,i : output

2. Deskripsi
     read (n)
     perulangan
     proses i <- 2
     i % 2=0 && i! = 2
     i %3=0
     i <- i + 1
     write (n,i)

Flowchart :


c++ :



Ebooks 3.3

0komentar
Algoritma :
1. Deklarasi
    m , n : integer (input)
    d : hasil Fpb (output)

2. Deskripsi
    read (m,n)
    a <- m% 1
    b <- n % 1
    d <- 1
    Write (a,b,fpb)

Flowchart :


c++ :



Ebooks 3.2

0komentar
Algoritma :
1. Deklarasi
    x,y : integer (iinput)
    x,y,kpk : output

2. Deskripsi
    Read (x,y)
    a <- m% 1
    b <- n %1
    kondisi a=0 && b=0
    c <- 1
     kondisi i>=kecil
    i<- i + 1
    kpk <- (x*y)/c
    write (x,y,kpk)

Flowchart :


c++ :



Ebooks 3.1

0komentar
Algoritma :
1. Deklarasi
    a : integer (input)
    m,akar: hasil (output)

2. Deskripsi
    read(a)
    m <- a*a
    akar <- sqrt(m)
    write(m,akar)

Flowchart :



c++ :



 

Anita © 2010

Blogger Templates by Splashy Templates