// problem 4 int total = 0; int numSpecial = 0; // count # of special numbers int num; cout << "enter a number> "; cin >> num; while (num != 0){ if (num > 0 && IsSpecial(num)){ // check precondition/specialness total += num; numSpecial++; } cout << "enter a number> "; cin >> num; } double avg = 0.0; if (numSpecial != 0){ avg = double(total)/numSpecial; } cout << "average of special numbers = " << avg << endl; //------------------------------------------------------------------------- // problem 5 #include #include void GetVitals(double & weight, int & height) // postcondition: sets weight (kgs) and height (cms) { cout << "enter weight (in lbs.): "; cin >> weight; weight /= 2.2; // convert to kilograms cout << "enter height (in inches): "; cin >> height; height *= 2.54; // convert to centimeters } double SurfaceArea(double height, double weight) // precondition: height and weight given in cms and kgs respectively // postcondition: returns surface area of person with given height/weight { return Pow(7.184,-3) * Pow(weight,0.452) * Pow(height,0.725); } main() { double weight; int height; GetVitals(weight,height); cout << "your surface area = " << SurfaceArea(height,weight) << " sq. meters" << endl; } //------------------------------------------------------------------------- // several options for finding elements main() { Element elem(1); // initialize to something String name; cout << "enter name of element (control-D to finish): "; while (cin >> name){ int k; for(k=1; k <= 103; k++){ elem.SetAtomic(k); if (elem.Name() == name){ cout << name << ": number = " << elem.AtomicNumber() << ", weight = " << elem.Weight() << endl; break; } } if (k > 103){ cout << name << ": not a recognized element" << endl; } } } // other solution main() { Element elem(1); // initialize to something String name; cout << "enter name of element (control-D to finish): "; while (cin >> name){ int k = 0; do{ k++; elem.SetAtomic(k); } while (elem.Name() != name && k < 103); if (elem.Name() == name){ cout << name << ": number = " << elem.AtomicNumber() << ", weight = " << elem.Weight() << endl; break; } else{ cout << name << ": not a recognized element" << endl; } } }