/*program for water bill problem*/
#include<stdio.h>
#define DEMAND_CHG 35.00 /*basic waterdemand charge*/
#define PER_1000_CHG 1.10 /*charge per thousand gallons used */
#define LATE_CHG 2.00 /* surcharge assessed on unpaid balance */
/*Function prototype*/
void instruct_water(void);
double comp_use_charge(int previous, int current);
double comp_late_charge(double unpaid);
void display_bill(double late_charge, double bill, double unpaid);
int
main(void)
{
int previous; /*input - matter reading from previous quarter*/
int current; //input -meter reading from current quarter
double unpaid; //input -unpaid
double bill; //output - water bill
int used;
double use_charge;
double late_charge;
//display user instruction.
instruct_water();
/*Get data: unpaid balance, previous and current meter reading*/
printf("Enter unpaid balance> $");
scanf("%lf", &unpaid);
printf("Enter previous meter reading> ");
scanf("%d",&previous);
printf("Enter current meter reading>");
scanf("%d",¤t);
//compute use charge
use_charge = comp_use_charge(previous,current);
//determine applicable late charge
late_charge = comp_late_charge(unpaid);
//figure bill
bill = DEMAND_CHG + use_charge + unpaid + late_charge;
//print bill
display_bill(late_charge, bill,unpaid);
return (0);
}
/*
*Display user instructions
*/
void instruct_water(void)
{
printf("This problem figures a water bill");
printf("based on the demand charge\n");
printf("($%.2f) and a $%.2f per 1000", DEMAND_CHG,PER_1000_CHG);
printf("gallons use charge.\n\n");
printf("A $%.2f surcharge is added to ", LATE_CHG);
printf("accounts with an unpaid balance.\n");
printf("\nEnter unpaid balance, previous.\n");
printf("and current meter readings\n");
printf("on separate lines after the prompts. \n");
printf("Press<return> or <enter>after");
printf("typing each number.\n\n");
}
/*
*
* computes use charge
*/
double
comp_use_charge(int previous,int current)
{
int used; //gallons of water used
double use_charge;
used = current - previous;
use_charge = used*PER_1000_CHG;
return (use_charge);
}
/*
*computes late charge
*/
double
compu_late_charge(double unpaid)
{
double late_charge;
if(unpaid>0)
late_charge = LATE_CHG;
else
late_charge = 0.0;
return (late_charge);
}
/*
*
*Displays late charge if any and bill,
* pre: late charege
*/
void display_bill(double late_charge, double bill, double unpaid)
{
if(late_charge > 0.0) {
printf("\nBill includes $%.2f late charge",late_charge);
printf("on unpaid balance of $%.2f\n",unpaid);
}
printf("\nTotal due = $%.2f\n",bill);
}
4.5 Deciscion Steps in Algorithms
page 170
Saturday, December 4, 2010
source code
#include<stdio.h>
#include<math.h>
double scale(double x, int n);
int main(void)
{
double num_1;
int num_2;
//get values for num_1 and num_2
printf("Enter a real number>");
scanf("%lf",&num_1);
printf("Enter an integer>");
scanf("%d",&num_2);
/*call scale and display result*/
printf("Result ofcall to function scale is %f\n",scale(num_1,num_2));
return (0);
//num_1 is called "actual argument"
//num_2 is called " actual arguent"
}
double scale(double x,int n)
{
//dkf
//dfkd
//x is formal parameter
//n is formal parameter
double scale_factor;
//local variable -10 to power n
scale_factor = pow(10,n);
return(x*scale_factor);
}
http://codepad.org/Gj2sasoY
online compiler
so easy and fast
it is better than other IDE or any other thing
#include<math.h>
double scale(double x, int n);
int main(void)
{
double num_1;
int num_2;
//get values for num_1 and num_2
printf("Enter a real number>");
scanf("%lf",&num_1);
printf("Enter an integer>");
scanf("%d",&num_2);
/*call scale and display result*/
printf("Result ofcall to function scale is %f\n",scale(num_1,num_2));
return (0);
//num_1 is called "actual argument"
//num_2 is called " actual arguent"
}
double scale(double x,int n)
{
//dkf
//dfkd
//x is formal parameter
//n is formal parameter
double scale_factor;
//local variable -10 to power n
scale_factor = pow(10,n);
return(x*scale_factor);
}
http://codepad.org/Gj2sasoY
online compiler
so easy and fast
it is better than other IDE or any other thing
Subscribe to:
Comments (Atom)