techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Jan 11, 2012, 10:03 PM   #1
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.38/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

C++ small tutorial

I was reading a book and went through a problem of mine.
In November i tried to make a formula in math, but the number was way too huge and
i couldn't hope anymore in my skills in math. I just found the square power of it,
but not the cube. So now i remember it all, and i was thinking...
What about this one: 2^2^2 ?
And made it even more interesting finding a new programming method like this:
pow(2, pow(2, 2));
from this:
pow(2, 2);
This means that you can call same function inside the function!
Well i didn't use it and didn't think of it till now...
So let's hope someone else will find a use of it and here is all the program:
Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    cout << pow(2, pow(2, 2));
    cin.get();
}
Aleksander is online now  
Reply With Quote
Old Jan 11, 2012, 10:38 PM   #2
brandonwh64
Addicted to Bacon and StarCrunches!!!
 
brandonwh64's Avatar
 
Join Date: Sep 2009
Location: Chatsworth, GA
Posts: 13,572 (10.00/day)
Thanks: 2,149
Thanked 5,342 Times in 3,697 Posts
Send a message via ICQ to brandonwh64 Send a message via AIM to brandonwh64 Send a message via MSN to brandonwh64 Send a message via Yahoo to brandonwh64

System Specs

ALEK you are going to be my hero! I am currently in a C++ class and I am struggling bad Hope if you dont mind, I could ask you some questions soon?


*EDIT*

I tried to use your code and got an error, it would not compile

__________________
Cruncher's:
All GPU's
GPU's:
7970 3GB *Unlocked* = 8 Threads
5770 1GB OCed = 2 Threads
brandonwh64 is offline  
Crunching for Team TPU
Reply With Quote
Old Jan 11, 2012, 11:03 PM   #3
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.38/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

Actually you are using the microsoft VC++ which is a different compiler from the gcc
In order to compile my program, you need to download codeblocks as an ide with its mingw
compiler:
http://sourceforge.net/projects/code...ingw-setup.exe

If you want to go advanced you can download only the mingw compiler and write the program on a notepad and save it as program.cpp, than open cmd, find the file path and than type in gcc program.cpp.
Here is the MinGW:
http://sourceforge.net/projects/mingw/files/MinGW/

Another way is to link the libraries with your visual studio, but that would take more thinking time to link cuz there are 2 libraries.
(and i don't know if this one works)

Last, but not least:
http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

This is for your compiler and i don't know the VC++, i cannot help you there!

EDIT: I remember reading from the book that instead of:
#include <iostream>
#include <cmath>

Some old compilers have it:
#include <iostream.h>
#include <math.h>

Maybe it is worth a try

Last edited by Aleksander; Jan 11, 2012 at 11:09 PM.
Aleksander is online now  
Reply With Quote
Old Jan 11, 2012, 11:08 PM   #4
brandonwh64
Addicted to Bacon and StarCrunches!!!
 
brandonwh64's Avatar
 
Join Date: Sep 2009
Location: Chatsworth, GA
Posts: 13,572 (10.00/day)
Thanks: 2,149
Thanked 5,342 Times in 3,697 Posts
Send a message via ICQ to brandonwh64 Send a message via AIM to brandonwh64 Send a message via MSN to brandonwh64 Send a message via Yahoo to brandonwh64

System Specs

Ahhh ok, We have to use this in class
__________________
Cruncher's:
All GPU's
GPU's:
7970 3GB *Unlocked* = 8 Threads
5770 1GB OCed = 2 Threads
brandonwh64 is offline  
Crunching for Team TPU
Reply With Quote
Old Jan 11, 2012, 11:52 PM   #5
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

In VC++ the math function POW is overloaded to accept a double, float and long double.
By giving the POW function two integers the compiler cannot figure out what you want it to use.

Try this ...
Code:
cout << Pow((double)2, Pow((double)2, 2));
Given the overloads a standard ANSI C++ compiler must give this error. VC++ has attempted to adhere to ANSI standard C++ compiler as much as possible.

As a side note, you are not calling a function within a function. You are telling the compiler to call the Pow function and use it as input to another function (in this case also Pow). This is very common in programming.

You are really doing this ...
Code:
int a = Pow((double)2, 2);
cout << Pow((double)2, a);
The difference with placing the function inline as an input paramter is that the compiler is creating an unnamed variable at runtime instead of you explicitely creating one at design time.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Jan 12, 2012, 12:02 AM   #6
brandonwh64
Addicted to Bacon and StarCrunches!!!
 
brandonwh64's Avatar
 
Join Date: Sep 2009
Location: Chatsworth, GA
Posts: 13,572 (10.00/day)
Thanks: 2,149
Thanked 5,342 Times in 3,697 Posts
Send a message via ICQ to brandonwh64 Send a message via AIM to brandonwh64 Send a message via MSN to brandonwh64 Send a message via Yahoo to brandonwh64

System Specs

Nope I just copied and pasted his that he made. Im a novice with only 1 week of hands on C++

I just need the basics explained to me instead of what my teacher did and give me 2 chapters to read that I couldn't understand a damn thing LOL
__________________
Cruncher's:
All GPU's
GPU's:
7970 3GB *Unlocked* = 8 Threads
5770 1GB OCed = 2 Threads
brandonwh64 is offline  
Crunching for Team TPU
Reply With Quote
Old Jan 12, 2012, 12:06 AM   #7
Steevo
Eligible for custom title
 
Steevo's Avatar
 
Join Date: Nov 2005
Posts: 5,567 (2.02/day)
Thanks: 238
Thanked 979 Times in 729 Posts

System Specs

by creating a unnamed parameter you may also find that the contents read from whatever junk leftover in that block of system ram will create issues.
__________________

“it would have been perfect....its got trains and the line"tech your kids not to do what iv done"(or similar) because i had obviously done something to warrent 2 e-thugs to come 4000miles out of their way and kill me.” -Solaris17
“yeah i failed. i noticed the "coming soon" part after i posted.” -Mussels
“people are just stupid.” -W1zzard
Yes I am evil, yes you can have some.
Steevo is offline  
Reply With Quote
Old Jan 12, 2012, 12:09 AM   #8
brandonwh64
Addicted to Bacon and StarCrunches!!!
 
brandonwh64's Avatar
 
Join Date: Sep 2009
Location: Chatsworth, GA
Posts: 13,572 (10.00/day)
Thanks: 2,149
Thanked 5,342 Times in 3,697 Posts
Send a message via ICQ to brandonwh64 Send a message via AIM to brandonwh64 Send a message via MSN to brandonwh64 Send a message via Yahoo to brandonwh64

System Specs

I still get errors when I use your code kreji
__________________
Cruncher's:
All GPU's
GPU's:
7970 3GB *Unlocked* = 8 Threads
5770 1GB OCed = 2 Threads
brandonwh64 is offline  
Crunching for Team TPU
Reply With Quote
Old Jan 12, 2012, 12:10 AM   #9
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Feel free to start your own thread with C++ questions Brandon.

Okay, I didn't test in in the compiler and just spewed from memory.
Back in a few minutes.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Jan 12, 2012, 12:11 AM   #10
brandonwh64
Addicted to Bacon and StarCrunches!!!
 
brandonwh64's Avatar
 
Join Date: Sep 2009
Location: Chatsworth, GA
Posts: 13,572 (10.00/day)
Thanks: 2,149
Thanked 5,342 Times in 3,697 Posts
Send a message via ICQ to brandonwh64 Send a message via AIM to brandonwh64 Send a message via MSN to brandonwh64 Send a message via Yahoo to brandonwh64

System Specs

Quote:
Originally Posted by Kreij View Post
Feel free to start your own thread with C++ questions Brandon.

Okay, I didn't test in in the compiler and just spewed from memory.
Back in a few minutes.
Oh, I thought this was a C++ Tutorial thread? Im sorry kreji Just trying to get a better understanding of this stuff and by watching the masters (You guys here at TPU) I may catch on.
__________________
Cruncher's:
All GPU's
GPU's:
7970 3GB *Unlocked* = 8 Threads
5770 1GB OCed = 2 Threads
brandonwh64 is offline  
Crunching for Team TPU
Reply With Quote
Old Jan 12, 2012, 12:31 AM   #11
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Sorry Brandon, I did not mean to imply you could not ask question here, just that you could start your own thread if you want.

VC++ is fussing still. Have to get back to you on that as I''m out of time tonight.


Got it ... here you go ...
Code:
#include "stdafx.h"
#include "iostream"
#include "cmath"

using namespace System;

int main(array<System::String ^> ^args)
{
    using namespace std;
    cout << pow((double)2, pow((double)2, 2));
    return 0;
}
Sorry, I'm a little rusty at C++. DOH !!
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Jan 12, 2012, 05:13 PM   #12
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.38/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

If you make it at school, than the best tutorial is the book in school
The teacher wont give anything outside the book
We made C++ and Java without a book and it was extremely difficult to understand
Aleksander is online now  
Reply With Quote
Old Jan 13, 2012, 08:59 AM   #13
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

When I first learned C++ years ago, I found the hardest part to be the transition from working in a functional programming language (C, Pascal, etc.) to an object oriented language. After writing code for years with a functional mindset, the abstract concepts of OOP were quite foriegn and almost seems superfluous at the time. Of course, once I really understood OOP, there was no looking back as it's advantages and benefits became readily apparant.

Learning the new syntax of different language is really the easy part, as once you get some code working you basically won't forget how to use the syntax for any given example, or at least know where to find it in a programming reference.

IMHO, the best thing anyone could possible do is to find themselves a mentor who is willing to answer all the questions that you will have on the undelying concepts of a given programming paradigm, and hopefully one who has a great deal of patience and the ability to present things from different angles when something is just not sinking in. Thankfully, I was blessed with a mentor who besides being a coding god, had both of those traits, and to this day is a very good friend. He has since moved into higher management levels at GE and I now code more than he does, but I still pick his brain on the rare occasion when I have questions on really low level code.

When I was learning to code, the internet as we know it did not exist, so the new coders have a great advantage as there are many fine coders here on TPU who are willing to share their knowledge and experience simply for the asking, and who remember what it was like to be a coding n00b with more questions than answers.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Jan 13, 2012, 10:12 AM   #14
Horrux
500 Posts
 
Horrux's Avatar
 
Join Date: Jun 2011
Posts: 641 (0.89/day)
Thanks: 335
Thanked 132 Times in 101 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
When I first learned C++ years ago, I found the hardest part to be the transition from working in a functional programming language (C, Pascal, etc.) to an object oriented language. After writing code for years with a functional mindset, the abstract concepts of OOP were quite foriegn and almost seems superfluous at the time. Of course, once I really understood OOP, there was no looking back as it's advantages and benefits became readily apparant.

Learning the new syntax of different language is really the easy part, as once you get some code working you basically won't forget how to use the syntax for any given example, or at least know where to find it in a programming reference.

IMHO, the best thing anyone could possible do is to find themselves a mentor who is willing to answer all the questions that you will have on the undelying concepts of a given programming paradigm, and hopefully one who has a great deal of patience and the ability to present things from different angles when something is just not sinking in. Thankfully, I was blessed with a mentor who besides being a coding god, had both of those traits, and to this day is a very good friend. He has since moved into higher management levels at GE and I now code more than he does, but I still pick his brain on the rare occasion when I have questions on really low level code.

When I was learning to code, the internet as we know it did not exist, so the new coders have a great advantage as there are many fine coders here on TPU who are willing to share their knowledge and experience simply for the asking, and who remember what it was like to be a coding n00b with more questions than answers.
As a self-taught C programmer of decent skill back in the DOS days, teaching myself C++ failed miserably.

Also, what is "cout"?
__________________
The head honcho at the Serious Gamers' Association

Criticism
“However, the Intel CPU dispatcher does not only check which instruction set is supported by the CPU, it also checks the vendor ID string. If the vendor string is "GenuineIntel" then it uses the optimal code path. If the CPU is not from Intel then, in most cases, it will run the slowest possible version of the code, even if the CPU is fully compatible with a better version. ”
Horrux is offline  
Reply With Quote
Old Jan 13, 2012, 10:16 AM   #15
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

It's the command to send output to the console.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Jan 13, 2012, 10:17 AM   #16
Horrux
500 Posts
 
Horrux's Avatar
 
Join Date: Jun 2011
Posts: 641 (0.89/day)
Thanks: 335
Thanked 132 Times in 101 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
It's the command to send output to the console.
Damn where are the days when things were at least a little bit self-explanatory LOL.
__________________
The head honcho at the Serious Gamers' Association

Criticism
“However, the Intel CPU dispatcher does not only check which instruction set is supported by the CPU, it also checks the vendor ID string. If the vendor string is "GenuineIntel" then it uses the optimal code path. If the CPU is not from Intel then, in most cases, it will run the slowest possible version of the code, even if the CPU is fully compatible with a better version. ”
Horrux is offline  
Reply With Quote
Old Jan 13, 2012, 12:02 PM   #17
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.38/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
When I first learned C++ years ago, I found the hardest part to be the transition from working in a functional programming language (C, Pascal, etc.) to an object oriented language. After writing code for years with a functional mindset, the abstract concepts of OOP were quite foriegn and almost seems superfluous at the time. Of course, once I really understood OOP, there was no looking back as it's advantages and benefits became readily apparant.

Learning the new syntax of different language is really the easy part, as once you get some code working you basically won't forget how to use the syntax for any given example, or at least know where to find it in a programming reference.

IMHO, the best thing anyone could possible do is to find themselves a mentor who is willing to answer all the questions that you will have on the undelying concepts of a given programming paradigm, and hopefully one who has a great deal of patience and the ability to present things from different angles when something is just not sinking in. Thankfully, I was blessed with a mentor who besides being a coding god, had both of those traits, and to this day is a very good friend. He has since moved into higher management levels at GE and I now code more than he does, but I still pick his brain on the rare occasion when I have questions on really low level code.

When I was learning to code, the internet as we know it did not exist, so the new coders have a great advantage as there are many fine coders here on TPU who are willing to share their knowledge and experience simply for the asking, and who remember what it was like to be a coding n00b with more questions than answers.
Your post is really motivating and i hoped to have your chances too in C++.
Unfortunately my mentor rejected me of being part of a project with Ogre3D for 9 months and accepted a girl who doesn't know how many bytes takes an int. That made me "VERY ANGRY" and i made the project using XNA in a single day. Now i know he was not a
mentor. He was a fool trying to tell us that call of duty black ops uses the best vertex shaders ever made. I didn't lose my patience with him and went on programming myself.
What is worse i have a lot of people arguing very bad about me and they don't want me to
study programming, but i didn't hear anything and went on reading a 1200 pages book.
Aleksander is online now  
Reply With Quote
Old Jan 13, 2012, 01:20 PM   #18
razaron
500 Posts
 
razaron's Avatar
 
Join Date: Apr 2008
Location: london
Posts: 608 (0.33/day)
Thanks: 170
Thanked 185 Times in 140 Posts

System Specs

When making a DnD dice roller a couple of months ago I remember using several math.min functions inside each other to work out 4d6 minus the lowest roll.
__________________
My Anime List
BattleTag (EU): razaron#1882
Steam: Razaron
razaron is offline  
Reply With Quote
Old Jan 13, 2012, 04:54 PM   #19
ctrain
200 Posts
 
Join Date: Jan 2010
Posts: 384 (0.31/day)
Thanks: 1
Thanked 72 Times in 59 Posts

Quote:
Originally Posted by Aleksander Dishnica View Post
I was reading a book and went through a problem of mine.
In November i tried to make a formula in math, but the number was way too huge and
i couldn't hope anymore in my skills in math. I just found the square power of it,
but not the cube. So now i remember it all, and i was thinking...
What about this one: 2^2^2 ?
And made it even more interesting finding a new programming method like this:
pow(2, pow(2, 2));
from this:
pow(2, 2);
This means that you can call same function inside the function!
Well i didn't use it and didn't think of it till now...
So let's hope someone else will find a use of it and here is all the program:
Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    cout << pow(2, pow(2, 2));
    cin.get();
}
it doesn't have to be the same function.

the return is used for the argument.

Quote:
Originally Posted by Kreij View Post
Code:
#include "stdafx.h"
#include "iostream"
#include "cmath"

using namespace System;

int main(array<System::String ^> ^args)
{
    using namespace std;
    cout << pow((double)2, pow((double)2, 2));
    return 0;
}
Sorry, I'm a little rusty at C++. DOH !!
this is C++/CLI (.NET), not native C++

Last edited by ctrain; Jan 13, 2012 at 05:00 PM.
ctrain is offline  
Reply With Quote
Old Jan 13, 2012, 08:37 PM   #20
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

I know, ctrain ... that is because Brandon said he had to use VC++ in his uni classes.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Jan 17, 2012, 11:34 PM   #21
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.38/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

Quote:
Originally Posted by brandonwh64 View Post
*EDIT*

I tried to use your code and got an error, it would not compile
Just found your problem.
I tried it on visual studio 2010 it didn't work
It will work if you add a
file -> new project -> visual C++ -> win32 -> win32 console application -> empty project
Copy paste it and it will work. It worked on my pc
If you have trouble just post your problem or watch the steps again
Aleksander is online now  
Reply With Quote
Old Jan 17, 2012, 11:37 PM   #22
brandonwh64
Addicted to Bacon and StarCrunches!!!
 
brandonwh64's Avatar
 
Join Date: Sep 2009
Location: Chatsworth, GA
Posts: 13,572 (10.00/day)
Thanks: 2,149
Thanked 5,342 Times in 3,697 Posts
Send a message via ICQ to brandonwh64 Send a message via AIM to brandonwh64 Send a message via MSN to brandonwh64 Send a message via Yahoo to brandonwh64

System Specs

Quote:
Originally Posted by Aleksander Dishnica View Post
Just found your problem.
I tried it on visual studio 2010 it didn't work
It will work if you add a
file -> new project -> visual C++ -> win32 -> win32 console application -> empty project
Copy paste it and it will work. It worked on my pc
If you have trouble just post your problem or watch the steps again
This is what I first tried and it errored when I compiled it
__________________
Cruncher's:
All GPU's
GPU's:
7970 3GB *Unlocked* = 8 Threads
5770 1GB OCed = 2 Threads
brandonwh64 is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 30, 2012, 04:16 PM   #23
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.38/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

I know this thread is old, but to help out:
I fixed the error in Visual Studio 2010 using math.h header instead of cmath.
You should also remove the precompiled headers when starting a new empty project.
In Visual Studio 2012, it works perfect since it has the new headers and C++11.
Aleksander is online now  
Reply With Quote
Old Nov 30, 2012, 08:10 PM   #24
MrSeanKon
200 Posts
 
MrSeanKon's Avatar
 
Join Date: Nov 2006
Location: Athens in love with Anna :)
Posts: 253 (0.11/day)
Thanks: 28
Thanked 65 Times in 24 Posts

Quote:
Originally Posted by Aleksander Dishnica View Post
This means that you can call same function inside the function!
This is called recursion or not?
Because I remember now the recursive algorithm of Fibonacci formula.
MrSeanKon is offline  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error reporting on tutorial BAGZZlash RBE 4 Dec 24, 2012 12:31 AM
OpenGL Tutorial Aleksander Programming & Webmastering 0 May 16, 2011 01:29 PM
Access tutorial reverze Programming & Webmastering 1 Jun 3, 2010 02:53 PM
the burn in tutorial psychomage343 Overclocking & Cooling 32 Apr 11, 2007 09:25 PM


All times are GMT. The time now is 08:26 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts