Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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
5/9/2012 10:52:38 AM EDT
[#1]
Um...  the nice thing about programming assignments is that you can test them and see if they work before you turn them in.  So... did it work?
5/9/2012 11:00:17 AM EDT
[#2]
I dont have a compiler on my computer that I know of.
5/9/2012 11:03:40 AM EDT
[#3]
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.
5/9/2012 11:07:03 AM EDT
[#4]
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?
5/9/2012 11:08:26 AM EDT
[#5]
Yeah, I'm running windows.  I will check it out.  Thanks for the help.  I really appreciate it.
5/9/2012 11:11:25 AM EDT
[#6]
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.
5/9/2012 11:12:27 AM EDT
[#7]
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.
5/9/2012 11:17:48 AM EDT
[#8]
Also, you need semicolons after all the lines.  (but before the comments that you have at the end of some of the lines)
5/9/2012 11:24:03 AM EDT
[#9]

#include <iostream>//preprocessor directive to use keyboard and monitor
#include <cmath>//directive to include cmath directives

int 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
5/9/2012 11:26:02 AM EDT
[#10]
Thanks for looking it over everybody.  It is submitted and I am done with it for the day.  Think I will go look up that compiler now.
5/9/2012 11:27:42 AM EDT
[#11]
Or slap together a VM and install linux with the gcc compiler. Thats what I'm playing with for C++ now. (However roughly...lol)
5/9/2012 11:32:06 AM EDT
[#12]
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.
5/9/2012 12:03:24 PM EDT
[#13]
Quoted:
Um...  the nice thing about programming assignments is that you can test them and see if they work before you turn them in.  So... did it work?


Not without semicolons.
5/9/2012 2:20:37 PM EDT
[#14]



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...



 
5/9/2012 2:33:17 PM EDT
[#15]



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

 
5/9/2012 2:49:58 PM EDT
[#16]
logic fail, syntax fail
5/9/2012 4:31:47 PM EDT
[#17]
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.
5/9/2012 4:57:42 PM EDT
[#18]
Who the fuck learns a programming language without a compiler!??!?! There are shit tons of free ones already posted, here is another:







 
5/9/2012 4:59:10 PM EDT
[#19]
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.

5/9/2012 5:06:53 PM EDT
[#20]
chew on this one for a while..;

class Base {
int _value;

public:
   Base() {
       _value = g();
   }

   virtual int f() = 0;

   int g() { return f(); }
};

class Derived: Base {  
public:
   Derived(): Base()
   { /* init Derived */ }

   int f() { /* implementation */ }
}
5/12/2012 11:48:04 PM EDT
[#21]
I like eclipse for java and there is a plugin so you are able to compile c++, and for php also. It's pretty versatile.
5/18/2012 7:52:21 PM EDT
[#22]
Quoted:
Who the fuck learns a programming language without a compiler!??!?! [/div]

Those who write in Interpretive languages?

5/20/2012 7:57:37 AM EDT
[#23]
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/