Jump to content


Photo
- - - - -

Newbie here General Guidance Needed


  • Please log in to reply
17 replies to this topic

#1 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 22 April 2011 - 10:44 AM

Hi,
i was googling for a hacking resource to start learning when i stumbled across binrev and i thought this was as good a place as any to start.
I've been interested in computers and hacking in general for awhile now and i've always wanted to know more about it but have had problems finding a simple and intuitive guide for beginners on the internet, hence my posting here on the forums.I would like to start off simple, by maybe writing simple executable programs and slowly work my way up to game modding, ie. character models and textures etc, as well as game hacking in the likes of teams such as Skidrow and RAZOR 1911.Also of interest to me would be hacking like denial of service, brute forcing, etc.

I know that most hackers are self-taught and do not like free-loaders but i would appreciate any help that i can get, or if someone would just point me in the right direction to start or the best language to starto ff with.I've heard that i should start off with languages like java or C,C++,C#(what's the difference with the 3 C languages?), as i can do many things with them.I've also taken a module in basic programming in html a few years back and learnt a little about algorithmns though i dont realy remember much of it anymore.

Thanks in advance

#2 5imp7y

5imp7y

    Hakker addict

  • Binrev Financier
  • 507 posts
  • Country:
  • Gender:Male
  • Location:PA again....

Posted 22 April 2011 - 01:28 PM

Hi,
i was googling for a hacking resource to start learning when i stumbled across binrev and i thought this was as good a place as any to start.
I've been interested in computers and hacking in general for awhile now and i've always wanted to know more about it but have had problems finding a simple and intuitive guide for beginners on the internet, hence my posting here on the forums.I would like to start off simple, by maybe writing simple executable programs and slowly work my way up to game modding, ie. character models and textures etc, as well as game hacking in the likes of teams such as Skidrow and RAZOR 1911.Also of interest to me would be hacking like denial of service, brute forcing, etc.

I know that most hackers are self-taught and do not like free-loaders but i would appreciate any help that i can get, or if someone would just point me in the right direction to start or the best language to starto ff with.I've heard that i should start off with languages like java or C,C++,C#(what's the difference with the 3 C languages?), as i can do many things with them.I've also taken a module in basic programming in html a few years back and learnt a little about algorithmns though i dont realy remember much of it anymore.

Thanks in advance

Well first off, WELCOME.
We all like to help here.
I started out my journey with programming and making simple executables much like what you are talking about. I more so like to take the computers apart now but i know a bit about how the innards work.
you should try some c++ and include a little c into the code. C++, although it isnt the prettiest, is a very useful language i find. It is also fairly easy to start off with. You ned a compiler and some TuTs. I would honestly start with the exstream basics, no handels and vecotrs yet. Start with Hello World, a very common "first Program" for every.

Here is a good and basic compiler that i also started with.
And This, although not much a tut does explain the hello world program fairly well.

Good luck to you and message me for any help with c++ im not perfect with it, but i can help
~5imp7y

#3 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 23 April 2011 - 01:41 AM

Thanks for replying 5imp7y,
I've downloaded the compiler and used the Tut that you've linked me to start on my hello world program though i have some questions about it.

Qn1.the tutorial that you've linked me to uses the line (using namespace std;),yet when i checked up namespaces on google, i came across some people saying that using (using namespace std;) in a program is a bad programming habit and is inadvisable to do so and that i should be writing (using std::cout;) in my program instead.I understand that this is so that there will not be any errors caused if i am using 2 different libraries with the word cout included in both of them in a single program i am writing.However, isn't the std library pretty much universal and contains everything thats pretty much needed for programming in C++? If so, what is the point of writing (using std::cout;) in my program if i am unlikely to use any other library then the std library in C++ seeing as it contains pretty much everythign i need?Also, assuming that i am using a personal,customised library for coding, why would i be doing so instead of using the std library?

Qn2.What is the difference between void main () and int main()?I get an error when i write void main() but don't when i use int main() although they are both the same as i write return 0; in when i am using int main()

Qn3. I can't view the output "hello world" as it just flashes up in a command prompt/windows wordpad looking windows before dissappearing 0.5 second later.Is there any way to make the output screen not dissappear immediately so that i can take a good look at it? xD

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\Beginniner projects\starter 1.cpp" -o "C:\Dev-Cpp\Beginniner projects\starter 1.exe" -pg -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lgmon -pg -g3
Execution terminated
Compilation successful

PS: thats the compilation log if its needed

Thanks!

#4 5imp7y

5imp7y

    Hakker addict

  • Binrev Financier
  • 507 posts
  • Country:
  • Gender:Male
  • Location:PA again....

Posted 23 April 2011 - 12:56 PM

Thanks for replying 5imp7y,
I've downloaded the compiler and used the Tut that you've linked me to start on my hello world program though i have some questions about it.

Qn1.the tutorial that you've linked me to uses the line (using namespace std;),yet when i checked up namespaces on google, i came across some people saying that using (using namespace std;) in a program is a bad programming habit and is inadvisable to do so and that i should be writing (using std::cout;) in my program instead.I understand that this is so that there will not be any errors caused if i am using 2 different libraries with the word cout included in both of them in a single program i am writing.However, isn't the std library pretty much universal and contains everything thats pretty much needed for programming in C++? If so, what is the point of writing (using std::cout;) in my program if i am unlikely to use any other library then the std library in C++ seeing as it contains pretty much everythign i need?Also, assuming that i am using a personal,customised library for coding, why would i be doing so instead of using the std library?

Qn2.What is the difference between void main () and int main()?I get an error when i write void main() but don't when i use int main() although they are both the same as i write return 0; in when i am using int main()

Qn3. I can't view the output "hello world" as it just flashes up in a command prompt/windows wordpad looking windows before dissappearing 0.5 second later.Is there any way to make the output screen not dissappear immediately so that i can take a good look at it? xD

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\Beginniner projects\starter 1.cpp" -o "C:\Dev-Cpp\Beginniner projects\starter 1.exe" -pg -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lgmon -pg -g3
Execution terminated
Compilation successful

PS: thats the compilation log if its needed

Thanks!


Using namespace std is alot easier that typing std::cout. For simple programs such as this one all calls will be from the standard namespace, so declaring it in the beginning for a basic program is fine.

I am not sure the differences between int and void main, other than implementing it. Void main should never come first and never return to int. Try changing the void main() to void hello().

Well this is where dev c++ shows a flaw. Unlike some other compilets out there, i.e. gcc, windows visual... blah blah blah, it doesnt should your the out put in the compilter. Before your return 0; or return EXIT_SUCCESS, on its own line add the fallowing code.
system("PAUSE");

That will promt the user (you) to press any key to continue. This is very usfull for adding data using the cin function.

The reason you get an error when you use void main is cause there is no int main() before it.

My hello world:
#include <iostream>

using namespace std; // standard lib

int main(int argc, char *argv[])//you need an int main function 
{
    return EXIT_SUCCESS;
}
void hello()
{
cout << "Hello World!" << endl; // endl; ends the line and creats a new one, you can also add a /n before the constructors os carots <<

system("PAUSE");
}

Flashes on the screen for .5 seconds. IDK why the system pause doesnt work, but in a void function system calls dont work i think :-/

My basic hello world:
#include <iostream>

using namespace std; // standard lib

int main(int argc, char *argv[])//you need an int main function 
{
    cout << "Hello World!" << endl; // endl; ends the line and creats a new one, you can also add a /n before the constructors os carots <<
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


hope this helps :-)

#5 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 24 April 2011 - 11:02 AM

Thanks for your explanation on the hello world program, it really helped out a lot, though im now having problems again with loops =X

#include <iostream>
using namespace std;
int main()
{
int i=0;
bool done=false;
while(!done)
{
cout<<i<<endl;
i++;
if(i==10)
done=true;
}
cout<<"All Finished!"<<endl;
system("pause");
return 0;
}

QN:While i do not have problems with a simple while loop using conditions such as while(i<=10), do etc etc, the logic behind the code above eludes me.While i get that [while(!done)] translates in english to "while not done, do the following...", what does it actually mean?


1:While not done(that is to say [bool done = false]), run the code till i=10 at which point, the value of done would be changed to true, hence terminating the loop. OR

2:While not done(that is to say "done" is not equals to false(i.e done = true), as the value of done was set as false above,)run the code till i=10, at which point the loop will terminate.

I think the correct answer is the first option, as the second option makes no sense to me, seeing as how the value of "done" is changed to "true" once "i==10", why does the loop not execute when i set "while(done=false)".I've also tried setting the variable done as a integer rather than a boolean variable yet i cant seem to get the loop to work when i set the loop as "while(done=false)".Am i mistaken in assuming that "while(done=false)" is the same as "while(!done)" when they are in fact different? Or am i committing an error which i am not aware of ?

Sorry to trouble you yet again


WebNinja

#6 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 24 April 2011 - 11:34 PM

Thanks for your explanation on the hello world program, it really helped out a lot, though im now having problems again with loops =X

#include <iostream>
using namespace std;
int main()
{
int i=0;
bool done=false;
while(!done)
{
cout<<i<<endl;
i++;
if(i==10)
done=true;
}
cout<<"All Finished!"<<endl;
system("pause");
return 0;
}

QN:While i do not have problems with a simple while loop using conditions such as while(i<=10), do etc etc, the logic behind the code above eludes me.While i get that [while(!done)] translates in english to "while not done, do the following...", what does it actually mean?


1:While not done(that is to say [bool done = false]), run the code till i=10 at which point, the value of done would be changed to true, hence terminating the loop. OR

2:While not done(that is to say "done" is not equals to false(i.e done = true), as the value of done was set as false above,)run the code till i=10, at which point the loop will terminate.

I think the correct answer is the first option, as the second option makes no sense to me, seeing as how the value of "done" is changed to "true" once "i==10", why does the loop not execute when i set "while(done=false)".I've also tried setting the variable done as a integer rather than a boolean variable yet i cant seem to get the loop to work when i set the loop as "while(done=false)".Am i mistaken in assuming that "while(done=false)" is the same as "while(!done)" when they are in fact different? Or am i committing an error which i am not aware of ?

Sorry to trouble you yet again


WebNinja

I read,
while (!done) as while not done,
while done (does NOT have value ie: equals 0), execute the code block.

C and C++ (someone correct me here) doesn't have true boolean functions. It really just uses 0 to mean false, and 1 for true. In reality, false is 0, and true is anything other then 0.

while (x=y)
will assign x to equal y and flag as true if it's a non-zero result, false if it's a zero result. ! NOT flips the logic, so untrue becomes true, and true becomes untrue. (think XOR, but it's NOT :)

while (x==y)
will compare x to y, and evaluate to true only if they are equal. the value of x will remain unchanged.

You are probably looking for "while (done == false)" blah blah.. because while (done = false) will set done to false, which is 0, and skip out of the while block.



Also try to use the code block quotes when you post (look for the bluish <> above the editor window).. makes it a tad easier to read.

#include <iostream>
using namespace std;
int main()
{
    int i=0;
    bool done=false;
    while(!done)
    {
                cout<<i<<endl;
                i++;
                if(i==10)
                done=true;
    }
                cout<<"All Finished!"<<endl;
                system("pause");
                return 0;
}


it's getting late, and I hope I haven't confused you or myself further

Edited by PurpleJesus, 24 April 2011 - 11:39 PM.


#7 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 25 April 2011 - 12:20 PM

Thanks for answering my noobish queries in a clear and concise manner PurpleJesus.The learning curve of C++ is steeper than i had originally thought as the logic and the usage of operators in conjunction with syntax does sometimes confuse me,as well as the placement or the semi colons having a huge impact on the way end result of the program, ie. if(i==5) v.s if(i==5); .I would like to ask how long have you been coding, seeing as you are experienced in programming, and how long did it take you to be fairly proficient enough to write your own simple program? This would pretty serve as a marker as to my progress thus far, and a measure of how far more i have to go to reach a certain proficiency in C++.

WebNinja

#8 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 25 April 2011 - 02:00 PM

Thanks for answering my noobish queries in a clear and concise manner PurpleJesus.The learning curve of C++ is steeper than i had originally thought as the logic and the usage of operators in conjunction with syntax does sometimes confuse me,as well as the placement or the semi colons having a huge impact on the way end result of the program, ie. if(i==5) v.s if(i==5); .I would like to ask how long have you been coding, seeing as you are experienced in programming, and how long did it take you to be fairly proficient enough to write your own simple program? This would pretty serve as a marker as to my progress thus far, and a measure of how far more i have to go to reach a certain proficiency in C++.

WebNinja


yeah, semi-colons are fun. Basically they signal the end of a statement. They are a signal to the compiler, Stop doing what you were doing, and now do something else. How they are used in a for(x=0;x<10;x++) loop still isn't clear to me.. but I've typed in enough of them that I know that's what it takes... That's what you're going to need to do.. Start typing in code. Don't copy/paste it until you've got the syntax down and you can just 'feel' it. I'm not near as good as some of the people around here. I'm still learning. I learned BASIC when I was 13, then Assembly when 15, Walked away when I was 19, came back about 15 years later.. and have been dabbling in C for the last few years.

Don't put a clock on when you will be proficient in C. It's different for everyone. In reality, it takes years to become 'good'. Just stick to it. One thing I did improve to myself was to model simple logic games and write brute forcers to discover every combination possible.. Such as this one: triangle peg board

#9 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 27 April 2011 - 07:00 AM

Thanks for your input thus far PurpleJesus, i have a few more questions about programming.

1.When and where do i have to use null characters in a program?I understand that null characters are used to terminate a string in c/c++, yet i have also read that everything contained in double quotes has an implicit null character included in it.If so, what is the point of the null character as it can be easily replaced by double quotes?

2.MakeFiles.Can i have a crash course or simplified explanation of makefiles if it isn't too much trouble? My c++ book introduces makefiles but only touches on it briefly while introducing lots of wierd looking code.I've read that the make utility automatically builds executable programs and libraries from source code by reading make files, which specify how to derive the target program.However, IDEs and language specific compiler features can also be used to manage the build process in modern systems, with the make utility only being widely used in unix and/or unix-like systems.If so, as i am currently on a windows 7 OS,is there any need for me to touch on makefiles?Also, why does my IDE create a makefile when i was trying out some codes?

PS:I am currently using dev c++ as my IDE, trying out creating multiple source files with the use of headers and global variables, using the C++ for dummies book.

#10 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 27 April 2011 - 11:42 AM

Thanks for your input thus far PurpleJesus, i have a few more questions about programming.

1.When and where do i have to use null characters in a program?I understand that null characters are used to terminate a string in c/c++, yet i have also read that everything contained in double quotes has an implicit null character included in it.If so, what is the point of the null character as it can be easily replaced by double quotes?

2.MakeFiles.Can i have a crash course or simplified explanation of makefiles if it isn't too much trouble? My c++ book introduces makefiles but only touches on it briefly while introducing lots of wierd looking code.I've read that the make utility automatically builds executable programs and libraries from source code by reading make files, which specify how to derive the target program.However, IDEs and language specific compiler features can also be used to manage the build process in modern systems, with the make utility only being widely used in unix and/or unix-like systems.If so, as i am currently on a windows 7 OS,is there any need for me to touch on makefiles?Also, why does my IDE create a makefile when i was trying out some codes?

PS:I am currently using dev c++ as my IDE, trying out creating multiple source files with the use of headers and global variables, using the C++ for dummies book.


You use nulls when you need them. Like you said, quotes auto add them for you. If you're doing something and want to trunc a string or merge them, you need to pay attention to nulls then. There are other places too, like pointers you want to watch out for null pointers. Screwy stuff can happen.

I'm not too up on Makefile's. Not much I do requires them. Occasionally when I'm cross compiling for a microcontroller I do use them to save on typing the same commands over and over again. Also I'll add a command to the Makefile to make programming my target board easier. Hopefully someone else can step in and answer better. But basically if you have a program that links a bunch of stuff together, use a Makefile to save yourself typing and the occasional typo.

Here's a tutorial found on google with the search terms "makefile tutorial" It's about as crash as you can get.
http://www.opussoftw...TutMakefile.htm

#11 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 28 April 2011 - 11:07 AM

I've already came across that tutorial when i was googling makefiles before posting on binrev but got lost halfway through the tutorial when they started talking about macros,macros modifiers and inference rules haha.I think i'll be skipping makefiles for now and revisit them later when i am more confident in my coding skills and have more knowledge about computers and programming in general though that would probably be quite some time as i still have some 600 odd pages left to go before i finish my c++ for dummies book , which coupled with my schooling commitments only give me an hour or two for coding daily lulz.Thanks for actually taking the time to look up a tutorial for me though purplejesus, i really apreciate it.

#12 double.emms

double.emms

    elite

  • Members
  • 103 posts

Posted 02 May 2011 - 03:48 AM

Thanks for replying 5imp7y,
I've downloaded the compiler and used the Tut that you've linked me to start on my hello world program though i have some questions about it.

Qn1.the tutorial that you've linked me to uses the line (using namespace std;),yet when i checked up namespaces on google, i came across some people saying that using (using namespace std;) in a program is a bad programming habit and is inadvisable to do so and that i should be writing (using std::cout;) in my program instead.I understand that this is so that there will not be any errors caused if i am using 2 different libraries with the word cout included in both of them in a single program i am writing.However, isn't the std library pretty much universal and contains everything thats pretty much needed for programming in C++? If so, what is the point of writing (using std::cout;) in my program if i am unlikely to use any other library then the std library in C++ seeing as it contains pretty much everythign i need?Also, assuming that i am using a personal,customised library for coding, why would i be doing so instead of using the std library?

Qn2.What is the difference between void main () and int main()?I get an error when i write void main() but don't when i use int main() although they are both the same as i write return 0; in when i am using int main()


Two of the things you're curious about--polluting the std namespace and why you'd need makefiles--are hard to visualize right now, because you are working on small programs. As you begin working with sufficiently large bodies of code, it becomes much more of a challenge to keep it all in your head. It's easy to focus on a local block of code--a few lines, functions, or even classes. Eventually, though, you end up with lots of lines that you either haven't looked at in awhile or didn't write yourself. Developing good habits like not treading on std will save you headaches down the road, though it doesn't really matter for what you're doing right now.

With makefiles, the same sort of thing applies. It's tough right now to see how they might be useful, but eventually, you might be working with more complex programs that are complicated to build. Makefiles let you establish relationships and script out an automated plan to control the build process.

As far as int main() vs. void main(), the difference is the return type. The first function returns an integer, the second returns nothing. Returning an integer can be useful to indicate the state of completion--i.e. return a zero for success and something else to indicate failure (possibly different numbers for different error codes). There is some debate about the matter, but according to the standard, void main() is not allowed (in C++, anyway).

http://homepage.ntlw...-void-main.html

#13 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 03 May 2011 - 06:19 AM

Hey guys,i've been looking for beginner programming challenges on the internet and i came across the fibonacci number sequence challenge so i decided to give it a shot.I'm apparently not allowed to upload the c++ source file so i've copy and pasted it below.

/*Generates the fibonacci sequence up to the nth number which the user inputs*/

//fibonacci sequence is 0,1,1,2,3,5,8,13,21,...,
#include <iostream>

using namespace std;

int Fibonacci(int &number)
{
int counter;
int firstnumber=0;
int secondnumber=1;
int total=0;
int quit;
cout<<"The fibonacci number sequence is : "<<endl;
cout<<firstnumber<<" ";//entering it manually
cout<<secondnumber<<" ";//entering it manually

for(counter=0;total<number;counter++)
{
quit=total+firstnumber+secondnumber;
total=firstnumber + secondnumber;
cout<<total<<" ";
firstnumber=secondnumber;
secondnumber=total;

if(quit>=number)
{
break;
}
}
}

int main()
{
int input;
cout<<"Enter a number"<<endl;
cin>>input;
cout<<endl;

if(input==0)
{
cout<<"Please enter a number greater than 0"<<endl;
}
else if(input==1)
{
cout<<"Please enter a number greater than 1"<<endl;
}
else
{
Fibonacci(input);
}

system("PAUSE");
return EXIT_SUCCESS;
}


so the challenge is to create a program which generates fibonacci series till a number 'n' where 'n' is entered by the user. For eg. if the user enters 10 then the output would be: 1 1 2 3 5 8.
The challenge was listed as beginner difficulty but i still took nearly an hour to write the program, and only after a quick peek at the internet.Can anyone give me pointers on how to further improve my code, and how to automatically output the first 0 and 1 in the number sequence instead of manually instructing the program to output it?

http://talkbinary.co...fibonacci-in-c/ (This is the site i referred to for help)

Thanks

WebNinja

Edited by WebNinja, 03 May 2011 - 06:20 AM.


#14 heisenbug

heisenbug

    Gibson Hacker

  • Members
  • 79 posts
  • Country:
  • Gender:Male

Posted 03 May 2011 - 07:48 AM

1. Can anyone give me pointers on how to further improve my code
2. and how to automatically output the first 0 and 1 in the number sequence instead of manually instructing the program to output it?


This should really go in the programming thread, but since it is a newbie question I'll help.


PART 1. You are making it much more difficult than it needs to be. Here is the program in Perl.
#!/usr/bin/perl

print "\nHow many numbers of the sequence would you like? ";
chomp($n = <STDIN>);
&fibonacci($n);

sub fibonacci {
  $a = 0;
  $b = 1;
  $n = shift;

  for ($i=0;$i<$n;$i++){
    printf "%d\n", $a;
    $sum = $a + $b;
    $a = $b;
    $b = $sum;
  }
}



PART 2. To just have it run automatically with only the first two entries, you just hard code it. For example.

&fibonacci(2);


#15 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 03 May 2011 - 11:18 AM

i dont know how to code in perl, but from what i've googled on the syntax you used heisenbug, i think i somewhat understand what you are trying to do with your code though i have a few questions.

QN1.From what i can make of your subroutine(function?), it pretty much does the same thing as my fibonacci function in the code i posted.However i see that you do not include a break in your for loop, but instead use ($i<$n) where 'i' is the counter and 'n' being the value the user inputs.By doing so, won't your loop output (n-1)counts of fibonacci numbers instead of fibonacci numbers up to the nth value?(which is entered by the user).

E.g(Assuming n=10)
your loop will output '0,1,1,2,3,5,8,13,21,34' while my loop will output'0,1,1,2,3,5,8', which is what is requested by the challenge?

QN2.By automatically output i did not mean hardcoding the first 2 fibonacci values(0,1) but by somehow including them into the code in the for loop so that then i run the program,it will simply calculate and output all fibonacci values including the first 2 values(0,1) instead of starting at the 3rd value (which is also 1) as i did.I tried googling fibonacci number sequences in c++ but the only code sample that i could find that seems to output the whole fibonacci sequence was a code that made use of recursion which i have not learnt yet,so i was wondering if there was a way to code the program to automatically output the whole fibonacci number sequence without using recursions.

PS:i dont understand what &fibonacci($n) does though google tells me that $n=shift; does something with arrays.I apologize if my questions were somehow answered by these 2 lines of code.

WebNinja

#16 heisenbug

heisenbug

    Gibson Hacker

  • Members
  • 79 posts
  • Country:
  • Gender:Male

Posted 03 May 2011 - 12:06 PM

QN1.From what i can make of your subroutine(function?), it pretty much does the same thing as my fibonacci function in the code i posted.However i see that you do not include a break in your for loop, but instead use ($i<$n) where 'i' is the counter and 'n' being the value the user inputs.By doing so, won't your loop output (n-1)counts of fibonacci numbers instead of fibonacci numbers up to the nth value?(which is entered by the user).
E.g(Assuming n=10)
your loop will output '0,1,1,2,3,5,8,13,21,34' while my loop will output'0,1,1,2,3,5,8', which is what is requested by the challenge?

Sorry I thought the challenge was the number of items entered by the user, not the closest fibonacci number. That's an easy fix, it's a while loop instead of a for loop.

#!/usr/bin/perl

print "\nHow many numbers of the sequence would you like? ";
chomp($n = <STDIN>);
&fibonacci($n);

sub fibonacci {
  $a = 0;
  $b = 1;
  $n = shift;

  while ($a <= $n){
    printf "%d\n", $a;
    $sum = $a + $b;
    $a = $b;
    $b = $sum;
  }
}



QN2.By automatically output i did not mean hardcoding the first 2 fibonacci values(0,1) but by somehow including them into the code in the for loop so that then i run the program,it will simply calculate and output all fibonacci values including the first 2 values(0,1) instead of starting at the 3rd value (which is also 1) as i did.I tried googling fibonacci number sequences in c++ but the only code sample that i could find that seems to output the whole fibonacci sequence was a code that made use of recursion which i have not learnt yet,so i was wondering if there was a way to code the program to automatically output the whole fibonacci number sequence without using recursions.

You could use the previous code and do-while with two iterations in the do section.

However, if you really want to play around and have fun with it, a recursive function could be fun with this. You could play with code similar to this....
sub fibonacci {
	 $num = shift;
	 $calc = 0; 
	 if ($num <= 1) 
	     $calc = $num;
	 else          
	     $calc = fibonacci($num - 1) + fibonacci($num);
	 return $calc;
    }


PS:i dont understand what &fibonacci($n) does though google tells me that $n=shift; does something with arrays.I apologize if my questions were somehow answered by these 2 lines of code.

Sorry about the shorthand, I didn't want to do your homework for you, but still help you. That's why I didn't give it to you in C.

Shift just takes the first item off an array. If the array isn't listed, Perl assumes it is @_ and since that is what is passed to the subroutine, that's the passed variable.

#17 WebNinja

WebNinja

    Will I break 10 posts?

  • Members
  • 9 posts
  • Gender:Male
  • Location:Under your bed

Posted 03 May 2011 - 01:24 PM

I understand what you are doing with your codes now heisenbug,it's pretty cool how i can pretty much figure out the code in perl and the logic behind it even without really bothering about the perl syntaxes.The recursions are pretty interesting seeing as how google shows how it can be used in multiple situations other then fibonacci number sequences.Thanks for your help =)

WebNinja

#18 heisenbug

heisenbug

    Gibson Hacker

  • Members
  • 79 posts
  • Country:
  • Gender:Male

Posted 03 May 2011 - 01:59 PM

I understand what you are doing with your codes now heisenbug,it's pretty cool how i can pretty much figure out the code in perl and the logic behind it even without really bothering about the perl syntaxes.The recursions are pretty interesting seeing as how google shows how it can be used in multiple situations other then fibonacci number sequences.Thanks for your help =)

WebNinja


Glad I could help.

Most languages are fairly similar if you code cleanly. It's not that hard to learn another once you get the first language down. Some people get lazy with their code and obfuscate it quite a bit, and I could have written the same thing in a perl one liner program, but I specifically made it easy for you to read. Most developers work on a team, and it makes sense to write code in a way that others can understand by a simple glance at it.

As long as you code cleanly, you should be able to move through languages with ease. Good luck with your programming.




BinRev is hosted by the great people at Lunarpages!