Page 2 of 2
Posted: Mon Jan 05, 2004 5:12 am
by yiuyuho
if(UP<1.0 && cin.eof())
becomes
if(UP<1.0)
Posted: Mon Jan 05, 2004 7:20 pm
by aakash_mandhar
tried that..
Can u send me a case where it does not work...
Thx
Aakash
Posted: Mon Jan 05, 2004 7:26 pm
by yiuyuho
just change the stuff to
if(UP<1) break;
should give you AC.
It is not a particular case that makes your program WA, but the terminating condition.
so use sample input and instead of 0.0 for last row,
use 0.5 7 7 7 8 9.876 9.88 456 2342 22 9.876
This should give you sample output.
Posted: Fri Jan 09, 2004 8:56 pm
by beeplove
Why I am getting WA?
Can anyone help me?
[cpp]
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <stdio.h>
//
struct data {
double UP; // up hill
double D; // well diameter
double L; // water level
double B; // bucket volume
double P; // bucket ascent rate
double DOWN; // down hill
double V; // required volume
double TOTAL; // time required to finish the operation
data *next; // pointer of next data set
};
class jack_and_jill {
data *node, *tail, *head; // linked list for input
double level; // current water level (in feet) below from the top
double increment; // level decresed after pull up one bucket (in feet)
//double radius; // radius of well (in feet)
double PI; // constant to get the value of PI
double ASC; // acceleration rate
public:
jack_and_jill (void); // constructor
~jack_and_jill (); // destructor
void get_inputs (void); // collect all input data and store them
void process_inputs (void); // process all inputs, ( all inputs are in linked list)
void print_output (void); // print the output
protected:
void store_input (double UP, double D, double L, double B, double P, double DOWN, double V);
// store the value in linked list
double drop_bucket (data *node); // time required to drop the bucket in well
double pull_bucket (data *node); // time required to pull up the bucket from well
};
// class constructor
jack_and_jill::jack_and_jill (void) {
head = NULL;
node = NULL;
tail = NULL;
PI = acos(-1);
ASC = 32.2;
}
// class destructor
jack_and_jill::~jack_and_jill () {
head = NULL;
tail = NULL;
node = NULL;
}
//
void jack_and_jill::get_inputs (void ) {
double UP, D, L, B, P, DOWN, V;
while (cin >> UP) {
if ( UP < 1.0) break;
cin >> D >> L >> B >> P >> DOWN >> V;
store_input(UP, D, L, B, P, DOWN, V);
}
}
//
void jack_and_jill::store_input(double UP, double D, double L, double B, double P, double DOWN, double V) {
node = new data;
if (head == NULL) head = node;
else tail->next = node;
node->UP = UP;
node->D = D;
node->L = L;
node->B = B;
node->P = P;
node->DOWN = DOWN;
node->V = V;
node->next = NULL;
tail = node;
}
//
void jack_and_jill::process_inputs (void) {
double total;
double n; // number of required travel to pail required volume
double r; // radius of well (in feet)
for (node = head; node; node = node->next) {
level = (node->L) / 12;
r = ((node->D) / 12) / 2;
increment = (node->B) / (PI * r * r);
n = ceil( (node->V) / (node->B) );
total = n * (node->UP);
for (double i = 1; i <= n; i++) {
double drop, pull;
drop = drop_bucket(node);
pull = pull_bucket(node);
//cout << "## Time required to drop bucket for " << i << " run: " << drop << "\n";
//cout << "## Time required to pull bucket for " << i << " run: " << pull << "\n";
total = total + drop + pull;
}
total = total + ( n * (node->DOWN) );
node->TOTAL = total;
}
}
//
double jack_and_jill::drop_bucket (data *node) {
double t; // time required to drop bucket
// d = (v * t) + ( 0.5 * a * (t ^ 2) )
t = sqrt( (level) / (0.5 * 32.2) );
return t;
}
//
double jack_and_jill::pull_bucket (data *node) {
double t;
t = ( (level / (node->P)) * 12 );
level = level + increment;
return t;
}
//
void jack_and_jill::print_output (void) {
int i = 1;
for (node = head; node; node = node->next) {
cout << "Scenario " << i << ":" << endl;
/***************************************
cout << " up hill ";
cout << node->UP << " sec" << endl;
cout << " well diameter ";
cout << node->D << " in" << endl;
cout << " water level ";
cout << node->L << " in" << endl;
cout << " bucket volume ";
cout << node->B << " cu ft" << endl;
cout << " bucket ascent rate";
cout << node->P << " in/sec" << endl;
cout << " down hill ";
cout << node->DOWN << " sec" << endl;
cout << " required volume ";
cout << node->V << " cu ft" << endl;
cout << " TIME REQUIRED ";
cout << setw(8) << setprecision(2) << node->TOTAL << " sec" << endl;
***************************/
printf (" up hill %8.2f sec\n", node->UP);
printf (" well diameter %8.2f in\n", node->D);
printf (" water level %8.2f in\n", node->L);
printf (" bucket volume %8.2f cu ft\n", node->B);
printf (" bucket ascent rate%8.2f in/sec\n", node->P);
printf (" down hill %8.2f sec\n", node->DOWN);
printf (" required volume %8.2f cu ft\n", node->V);
printf (" TIME REQUIRED %8.2f sec\n", node->TOTAL);
if (node->next) cout << endl;
i++;
}
}
int main (void) {
jack_and_jill jack;
jack.get_inputs();
jack.process_inputs();
jack.print_output();
return 0;
}
[/cpp]
Posted: Fri Jan 09, 2004 11:54 pm
by yiuyuho
3 problems:
(1) for printing, you should use only 1 stream: don't use cout and printf for the same program because they have different pipes and arrive speed can be different: try a program with cout<<"Hello"<<endl; printf("EEE\n");
you will see
Hello
EEE
and
EEE
Hello
as output after some runs.
(2) you need tolerance: n=ceil(V/B) should become n=ceil(V/B-1e-12);
(3) use integer for looping, declear n as integer as well.
why????? I am getting wrong ... !!!
Posted: Fri Mar 12, 2004 10:20 pm
by beeplove
Can you please see my code again.. where is wrong in my code?
I would reallly appreciate it.
Thanks in advance.
[cpp]
#include <iostream>
#include <iomanip>
#include <math.h>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::ios;
struct data {
double UP; // time to go up hill in sec
double D; // well diameter in inch
double L; // water level in inch
double B; // bucket volume in cft
double P; // bucket ascent rate in in/sec
double DOWN; // time to go down hill in sec
double V; // required volume in cft
double TOTAL; // total time required in sec
data *next; // pointer of next data set
};
class jack_and_jill {
data *node, *tail, *head; // linked list for input
double level; // current water level (in inch) below from the top
double increment; // level decresed after pull up one bucket (in inch)
double PI; // constant to get the value of PI
double ASC; // acceleration rate
public:
jack_and_jill (void); // constructor
~jack_and_jill (); // destructor
void get_inputs (void); // collect all input data and store them
void process_inputs (void); // process all inputs, ( all inputs are in linked list)
void print_output (void); // print the output
protected:
void store_input (double UP, double D, double L, double B, double P, double DOWN, double V);
// store the value in linked list
double drop_bucket (data *node); // time required to drop the bucket in well
double pull_bucket (data *node); // time required to pull up the bucket from well
void print_line (char *str, double value, char *unit);
};
// class constructor
jack_and_jill::jack_and_jill (void) {
head = NULL;
node = NULL;
tail = NULL;
PI = acos(-1);
ASC = 32.2 * 12; // converted to inch
}
// class destructor
jack_and_jill::~jack_and_jill () {
head = NULL;
tail = NULL;
node = NULL;
}
//
void jack_and_jill::get_inputs (void ) {
double UP, D, L, B, P, DOWN, V;
while (cin >> UP) {
if ( UP < 1.0) break;
cin >> D >> L >> B >> P >> DOWN >> V;
store_input(UP, D, L, B, P, DOWN, V);
}
}
//
void jack_and_jill::store_input(double UP, double D, double L, double B, double P, double DOWN, double V) {
node = new data;
if (head == NULL) head = node;
else tail->next = node;
node->UP = UP;
node->D = D;
node->L = L;
node->B = B;
node->P = P;
node->DOWN = DOWN;
node->V = V;
node->next = NULL;
tail = node;
}
//
void jack_and_jill::process_inputs (void) {
double total;
int n; // number of required travel to pail required volume
double r; // radius of well (in inch)
for (node = head; node; node = node->next) {
level = (node->L);
r = ( (node->D) / 2 );
increment = ( ((node->B) * 12 * 12 * 12) / (PI * r * r) ); // node->B converted to cu inch
n = (int)ceil( (node->V) / (node->B) ); // both B & V is in ft .. dont need to convert in inch
total = n * (node->UP);
for (int i = 1; i <= n; i++) {
double drop, pull;
drop = drop_bucket(node);
pull = pull_bucket(node);
total = total + drop + pull;
}
total = total + ( n * (node->DOWN) );
node->TOTAL = total;
}
}
//
double jack_and_jill::drop_bucket (data *node) {
double t; // time required to drop bucket
// d = (v * t) + ( 0.5 * a * (t ^ 2) )
t = sqrt( (level) / (0.5 * 32.2 * 12) );
return t;
}
//
double jack_and_jill::pull_bucket (data *node) {
double t;
t = (level / (node->P));
level = level + increment;
return t;
}
//
void jack_and_jill::print_output (void) {
int i = 1;
for (node = head; node; node = node->next) {
cout << "Scenario " << i << ":" << endl;
cout.setf(std::ios::showpoint);
cout << setprecision(2);
cout.setf(std::ios::fixed);
print_line("up hill", node->UP, "sec");
print_line("well diameter", node->D, "in");
print_line("water level", node->L, "in");
print_line("bucket volume", node->B, "cu ft");
print_line("bucket ascent rate", node->P, "in/sec");
print_line("down hill", node->DOWN, "sec");
print_line("required volume", node->V, "cu ft");
print_line("TIME REQUIRED", node->TOTAL, "sec");
if (node->next) cout << endl;
i++;
}
}
void jack_and_jill::print_line (char *str, double value, char *unit) {
ios::fmtflags old_flags = cout.flags();
cout
<< setw(5)
<< setiosflags(std::ios::left)
<< " "
<< setw(18)
<< setiosflags(std::ios::left)
<< str
<< setw(8)
<< setiosflags(std::ios::right)
<< value
<<setw(1)
<< setiosflags(std::ios::left)
<< " "
<< unit << endl;
cout.flags(old_flags);
}
int main (void) {
jack_and_jill jack;
jack.get_inputs();
jack.process_inputs();
jack.print_output();
return 0;
}
[/cpp]
i think .. i know
Posted: Fri Mar 12, 2004 11:13 pm
by beeplove
I think, I know where is error in my code..
get_inputs method is not functioning properly.