Tuesday, July 28, 2009

Any Experienced Programmers...Need Help With For Loops in C code?

im having the toughest time with this assignment, ima novice programmer and i just went a blank...please help...





1. Generate (to the monitor) a table of mathematical functions i.e. Square Root,


Square and Cube.


2. Tell the user about the program and have the user enter the beginning, ending


and incrementing values. The only requirements are, no error checking


required, that the Beginning is less than End and the Incrementing value must


be positive.


3. Use the double data types for all values.


4. Show six (6) decimal place accuracy for the Square Root, and zero (0) decimal


place accuracy for the Square and Cube.

Any Experienced Programmers...Need Help With For Loops in C code?
I tried a little something :


(compiled under Linux)


(i'm not using the nicest sentences, but that's up to you!)











#include %26lt;cmath%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main() {


double start, end, increment;


bool test = true;


cout %26lt;%26lt; "This program will blah blah" %26lt;%26lt; endl;


while (test) {


cout %26lt;%26lt; "start : ";


cin %26gt;%26gt; start;


cout %26lt;%26lt; "end : ";


cin %26gt;%26gt; end;


cout %26lt;%26lt; "increment : ";


cin %26gt;%26gt; increment;


if ((start %26lt; end) %26amp;%26amp; (increment %26gt; 0))


test = false;


else {


cout %26lt;%26lt; "start must be lower than end" %26lt;%26lt; endl;


cout %26lt;%26lt; "and increment must be positive" %26lt;%26lt; endl;


}


}


while (start %26lt; end) {


cout.width(10);


cout.precision(6);


cout %26lt;%26lt; sqrt(start);


cout.width(10);


cout.precision(0);


cout %26lt;%26lt; fixed %26lt;%26lt; pow(start, 2);


cout.width(10);


cout %26lt;%26lt; fixed %26lt;%26lt; pow(start, 3) %26lt;%26lt; endl;


start += increment;


}


cout %26lt;%26lt; "Done!" %26lt;%26lt; endl;


return 0;


}











Just tried many tests, works pretty well :)


=)





just sad we don't see the spacing... I hope I didn't let any typos there, sorry if so, it should be easy to correct!


No comments:

Post a Comment