Posted: 5/9/2012 10:47:15 AM EDT
|
Will this work as a function to display the square roots of the numbers 1 -10 on the monitor in C++? I think it is good, but is due in about an hour and wanted to be sure. I had to document it too, but I believe that part is fine. Hope somebody is watching.
#include <iostream>//preprocessor directive to use keyboard and monitor #include <cmath>//directive to include cmath directives using namespace std;//use standard C++ commands int main( )//main function needed to run {//start main program block int number=1//set variable to one and as integer double root = sqrt(number)//set variable to square root of number and as double value while (number < 11)//run this program block while variable is less than 11 {//start program block cout.setf(ios::fixed) cout.setf(ios::showpoint) cout.setf(2)//previous three lines set double value at two decimal points cout << "The square root of " << number << " is " << root << ".\n"//text to monitor to include variables }//end program block number=number++;//change number variable to variable + 1 return 0//indicates successful termination of program }//end program block |
|
1) You need to move the "number=number++;" line inside the while loop. ( put it above the "}" )
2) You need to put "root = sqrt(number);" after the "number++;", otherwise you're not calculating it's value with each iteration. Are you on Windows? Get "Bloodshed Dev-C++". It's a free C/C++ IDE and comes with free (opensource) compilers. You can also shorten the "number=number++;" to just "number++;" if you want. |
|
Quoted:
1) You need to move the "number=number++;" line inside the while loop. ( put it above the "}" ) 2) You need to put "root = sqrt(number)" after the "number++", otherwise you're not calculating it's value with each iteration. Are you on Windows? Get "Bloodshed Dev-C++". It's a free C/C++ IDE and comes with free (opensource) compilers. garhead, if you don't have a compiler for this, you are seriously unprepared. Give this Bloodshed a shot. Find something, at the very least. Not being able to compile your code in a programming class is like not bringing any tools to the construction site. What are you expecting to get out of this class? |
|
Quoted:
Will this work as a function to display the square roots of the numbers 1 -10 on the monitor in C++? I think it is good, but is due in about an hour and wanted to be sure. I had to document it too, but I believe that part is fine. Hope somebody is watching. #include <iostream>//preprocessor directive to use keyboard and monitor #include <cmath>//directive to include cmath directives using namespace std;//use standard C++ commands int main( )//main function needed to run {//start main program block int number=1//set variable to one and as integer double root = sqrt(number)//set variable to square root of number and as double value while (number < 11)//run this program block while variable is less than 11 {//start program block cout.setf(ios::fixed) cout.setf(ios::showpoint) cout.setf(2)//previous three lines set double value at two decimal points cout << "The square root of " << number << " is " << root << ".\n"//text to monitor to include variables }//end program block number=number++;//change number variable to variable + 1 return 0//indicates successful termination of program }//end program block Use more descriptive variable names. I don't remember how doubles are truncated, but make sure it doesn't round in a way that will ding you for accuracy. Your tests should have shown this if it was happening. You didn't indent. I'd ding you for it. Mostly, though, what the first reply said is spot on. If it works, it works. Most instructors won't ding you for style points...they see it done too many ways for them to look at anything other than results. |
|
Quoted:
Quoted:
1) You need to move the "number=number++;" line inside the while loop. ( put it above the "}" ) 2) You need to put "root = sqrt(number)" after the "number++", otherwise you're not calculating it's value with each iteration. Are you on Windows? Get "Bloodshed Dev-C++". It's a free C/C++ IDE and comes with free (opensource) compilers. garhead, if you don't have a compiler for this, you are seriously unprepared. Give this Bloodshed a shot. Find something, at the very least. Not being able to compile your code in a programming class is like not bringing any tools to the construction site. What are you expecting to get out of this class? A passing grade and a basic understanding. Our instructor told us we didn't need a compiler for the class, but said he would provide one if we wanted it. I asked, he didn't. |
Output: The square root of 1 is 1.00 The square root of 2 is 1.41 The square root of 3 is 1.73 The square root of 4 is 2.00 The square root of 5 is 2.24 The square root of 6 is 2.45 The square root of 7 is 2.65 The square root of 8 is 2.83 The square root of 9 is 3.00 The square root of 10 is 3.16 |
|
Quoted:
Or slap together a VM and install linux with the gcc compiler. Thats what I'm playing with for C++ now. (However roughly...lol) Yeah, installing Linux (or my preference, FreeBSD) into a VirtualBox VM would work. It'll take up a lot more space than Dev-C++ does, and Dev-C++ uses the same GNU compilers anyways. He could also install Cygwin if he were feeling adventurous. |
|
Quoted: I dont have a compiler on my computer that I know of. Wow. Hmm. If you will be doing any more, there are plenty of free IDEs which can make life easier. MS used to (presume still do) release a free C++ IDE. I quite like CodeBlocks. Never really used Dev-C++ but seems reasonable. All can be found through the miracle of Google. Yeah, so "write a program in C++ but you don't need to compile it and test it to see if it works", eh? Writing it in psuedo-code, fair enough, I just don't see the rationale behind wanting it in C++ without needing it to at least work. Still, ours not to reason why... |
|
Quoted: #include <iostream>//preprocessor directive to use keyboard and monitor#include <cmath>//directive to include cmath directivesint main( )//main function needed to run{//start main program block int number=1; //set variable to one and as integer float root; while (number < 11)//run this program block while variable is less than 11 {//start program block root = sqrt(number); printf("The square root of %i is %.2f\n", number, root); number++; }//end program block return 0; //indicates successful termination of program}//end program block Output: The square root of 1 is 1.00 The square root of 2 is 1.41 The square root of 3 is 1.73 The square root of 4 is 2.00 The square root of 5 is 2.24 The square root of 6 is 2.45 The square root of 7 is 2.65 The square root of 8 is 2.83 The square root of 9 is 3.00 The square root of 10 is 3.16 You need to include cstdio instead of iostream |
|
Quoted:
A passing grade and a basic understanding. Our instructor told us we didn't need a compiler for the class, but said he would provide one if we wanted it. I asked, he didn't.
Download a Linux live cd. There's also a version of GCC for Windows, I think, if you'd prefer to use a Satan-worshipping OS. |
|
Who the fuck learns a programming language without a compiler!??!?! There are shit tons of free ones already posted, here is another: |
|
Quoted:
Quoted:
Quoted:
1) You need to move the "number=number++;" line inside the while loop. ( put it above the "}" ) 2) You need to put "root = sqrt(number)" after the "number++", otherwise you're not calculating it's value with each iteration. Are you on Windows? Get "Bloodshed Dev-C++". It's a free C/C++ IDE and comes with free (opensource) compilers. garhead, if you don't have a compiler for this, you are seriously unprepared. Give this Bloodshed a shot. Find something, at the very least. Not being able to compile your code in a programming class is like not bringing any tools to the construction site. What are you expecting to get out of this class? A passing grade and a basic understanding. Our instructor told us we didn't need a compiler for the class, but said he would provide one if we wanted it. I asked, he didn't. coding is one of the things i do for a living. though its less c++ and more SQL these days. your prof is a full blown retard. you cannot learn programming without writing code and then running it. there are a zillion free ways to get c++ compilers. i suspect even microsoft has something a student can get for free. then there's gcc and perhaps more. |
|
I know this is a bit late but there's a program called Ch that basically turns C/C++ into an interpreted language. You can type programs line by line and see the output. Pretty cool for testing stuff
http://www.softintegration.com/ |