lab 6 work - ME 218 (17795) Alan Shu-Ming Kwok (sk25784)...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ME 218 (17795) Alan Shu-Ming Kwok (sk25784) Lab 6 Problem 1 #include <iostream> using namespace std; class poly { float cofficient[11]; int highest_power; public : void input(); void print(); }; void poly::print() { cout << "\nf(x) = " ; //print constant term and end sentence cout << cofficient[0] << " " ; //print power terms for ( int i=1; i<=highest_power; i++) { if (cofficient[i] > 0) cout << "+" << cofficient[i] << "*x^" << i << " " ; else if (cofficient[i] <0) cout << cofficient[i] << "*x^" << i << " " ; } cout << endl; return ; } void poly::input() { for ( int i=0;i<10;i++) //setting all array values to 0 {cofficient[i] = 0;} //asking for limit of polynomial cout<< "\nWhat is the highest powered term in the polynomial? " ; cin >> highest_power; //checking if the limit exceeds power 10 if (highest_power > 10) { return ;} // ask for powered terms for ( int i = highest_power; i >=1; i--) {cout<< "Enter the coefficient for the x^" <<i<< " term: " ; cin>> cofficient[i];} // ask for constant term cout<< "Enter the coefficient for the constant term: " ; cin>> cofficient[0]; return ; } void main() { poly eqn_1; eqn_1.input(); eqn_1.print(); return ; ME 218 (17795) Alan Shu-Ming Kwok (sk25784) Lab 6 } ME 218 (17795) Alan Shu-Ming Kwok (sk25784) Lab 6 Problem 2 #include <iostream> #include <math.h> using namespace std; class poly { float cofficient[11]; int highest_power; public : void input(); void print(); float calculate( float ); }; float poly::calculate( float x) { //initializing result from constant term float result = cofficient[0]; //add on powered terms for ( int i=1; i<=highest_power; i++) result += pow(x,i)*cofficient[i]; return result; } void poly::print() { //break if highest_power >10 if (highest_power > 10) { return ;} cout << "\nf(x) = " ; //print constant term and end sentence cout << cofficient[0] << " " ; //print power terms...
View Full Document

This note was uploaded on 09/04/2011 for the course ME 218 taught by Professor Unknown during the Spring '08 term at University of Texas at Austin.

Page1 / 9

lab 6 work - ME 218 (17795) Alan Shu-Ming Kwok (sk25784)...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online