Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Can I assign a variable with the value 0++ or something similar to that?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Sat Mar 09, 2013 4:39 pm   Post subject: Can I assign a variable with the value 0++ or something similar to that?

Can I assign a variable with the value 0++ or something similar to that? I'm making a question from an ebook's practice question, which I need to show the first 20 squared numbers (not 20 outputs obviously though).
Here's what I tried to do:
c++:

#include <iostream>
using namespace std;

int main ()
{
      int s = 0++;
      for ( int i = 0; i <= 20; i++ )
      {
              cout << i * s << endl;
      }
}
Sponsor
Sponsor
Sponsor
sponsor
nullptr




PostPosted: Sat Mar 09, 2013 5:47 pm   Post subject: Re: Can I assign a variable with the value 0++ or something similar to that?

What exactly do you want the variable s to hold? Remember - it has to be an integer like 1 or 17.
DemonWasp




PostPosted: Sat Mar 09, 2013 6:28 pm   Post subject: RE:Can I assign a variable with the value 0++ or something similar to that?

You cannot increment (++) the constant value 0, because it is constant. You could say:

c++:

int s = 0;
s++;


Which would make s = 1.

You cannot make a simple variable that will increment every time you access it. Technically you could use macros or operator overloads or one of dozens of other tricks C++ has, but I really can't recommend it.

If you want the first 20 square numbers, though, why not just square i?
Cancer Sol




PostPosted: Sat Mar 09, 2013 8:45 pm   Post subject: Re: RE:Can I assign a variable with the value 0++ or something similar to that?

DemonWasp @ 3/9/2013, 6:28 pm wrote:
You cannot increment (++) the constant value 0, because it is constant. You could say:

c++:

int s = 0;
s++;


Which would make s = 1.

You cannot make a simple variable that will increment every time you access it. Technically you could use macros or operator overloads or one of dozens of other tricks C++ has, but I really can't recommend it.

If you want the first 20 square numbers, though, why not just square i?


Ugh.. I'm so stupid LOL. It's because I saw someone else's program on some other site that was basically the same thing, but that other person did var * var instead of var squared.
How do I do i++ squared then? What's the code for it? In turing, I know it's ** , but I tried it for C++ and it didn't work :/
Insectoid




PostPosted: Sat Mar 09, 2013 8:47 pm   Post subject: RE:Can I assign a variable with the value 0++ or something similar to that?

How do you square a number on paper?
Cancer Sol




PostPosted: Sat Mar 09, 2013 8:48 pm   Post subject: Re: RE:Can I assign a variable with the value 0++ or something similar to that?

Insectoid @ 3/9/2013, 8:47 pm wrote:
How do you square a number on paper?


Well... I a small 2 I guess? But how do I input that on a computer? Sorry, I'm stupid Razz

@nullptr I was trying to have var s' value just like var i, increasing by a value of 1 each time the loop loops again.
Insectoid




PostPosted: Sat Mar 09, 2013 8:50 pm   Post subject: RE:Can I assign a variable with the value 0++ or something similar to that?

When you square a number, what are you actually doing to it? What is the definition of squaring?
Cancer Sol




PostPosted: Sat Mar 09, 2013 9:02 pm   Post subject: Re: RE:Can I assign a variable with the value 0++ or something similar to that?

Insectoid @ 3/9/2013, 8:50 pm wrote:
When you square a number, what are you actually doing to it? What is the definition of squaring?


Holy crap. I'm sooo stupid.
Well.. squaring a number is multiplying it by itself right? Is there an faster way to have a number to the power of (let's say) 40? I wouldn't want to have to multiply i 40 times Razz
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Mar 09, 2013 9:11 pm   Post subject: RE:Can I assign a variable with the value 0++ or something similar to that?

Yes, but I wanted you to understand that point first Razz. The ** operator will do any power for you. 5**2 is 5 squared. 5**10 is 5 to the power of 10.
DemonWasp




PostPosted: Sat Mar 09, 2013 11:20 pm   Post subject: RE:Can I assign a variable with the value 0++ or something similar to that?

There's a function, pow, that will perform exponentiation (which is handled by the ** operator in Turing and the ^ in some languages--but NOT C or C++). Good documentation can be found here: http://en.cppreference.com/w/cpp/numeric/math/pow .

This is overkill for squaring a number. Just square it and be done.

If you want to get into fast ways to do higher powers than repeated multiplication, see: http://en.wikipedia.org/wiki/Exponentiation#Efficient_computation_of_integer_powers .
Cancer Sol




PostPosted: Sun Mar 10, 2013 5:24 pm   Post subject: Re: RE:Can I assign a variable with the value 0++ or something similar to that?

DemonWasp @ 3/9/2013, 11:20 pm wrote:
There's a function, pow, that will perform exponentiation (which is handled by the ** operator in Turing and the ^ in some languages--but NOT C or C++). Good documentation can be found here: http://en.cppreference.com/w/cpp/numeric/math/pow .

This is overkill for squaring a number. Just square it and be done.

If you want to get into fast ways to do higher powers than repeated multiplication, see: http://en.wikipedia.org/wiki/Exponentiation#Efficient_computation_of_integer_powers .


I still don't get it :/
Oh well. Thanks for all the help guys Smile
Insectoid




PostPosted: Sun Mar 10, 2013 5:32 pm   Post subject: RE:Can I assign a variable with the value 0++ or something similar to that?

Pow is a function that takes 2 arguments and returns a number (the return type depends on the arguments you pass it). The first argument is the number you want to raise to a power. The second argument is the power you want to raise it to. So, pow(5,2) is 5 squared. This function lives in math.h, so you're going to have to #include <cmath> to use it.
Cancer Sol




PostPosted: Sun Mar 10, 2013 8:38 pm   Post subject: Re: Can I assign a variable with the value 0++ or something similar to that?

Thanks a lot! Smile
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: