Home    General Stuff    General Chat
#1

Coding.

Archive: 19 posts


Hey there!

Do YOU code? I do. I code in C++. I have for a couple months now. You should try it out! If you want a language that you write like a document and them compile, I suggest C++. If you want a program that you write more like a set of commands for a robot, the try Python. Those are my two favorite languages but since I got started and so into C++ it takes a lot of time for me to try and learn python now.

I have recently finished a code for Project Euler question 6. I like it and I think it's quite efficient. here it is:



#include <stdio.h>
#include <math.h>

int total, totil;

void SumSquare()
{
for(int a=1;a<101;a++)
{

total += pow(a, 2);
}
}

void SquareSum ()
{
int countUp, sumOfOnehundred;
for (countUp = 1, sumOfOnehundred = 0; countUp < 101; countUp++)
{
sumOfOnehundred += countUp;
}
totil = pow(sumOfOnehundred, 2);
}

int main ()
{
SquareSum();
SumSquare();
int totalTotal;
printf("The difference between %i and %i is: n", total, totil);
printf("%i", totil-total);

return 0;
}


I have finished various other codes for Project Euler too. I try to stay away from prime number question though as I still haven't devised my own prime number generator...

Any of YOU program? Care to show us some code?

And if you are going to show us a code, make sure it's in the [ code ] [ / code ] BB tags ok? :3.

Please tell us no matter what skill level! See ya .

EDIT: Sorry I didn't mention, I made it my goal here to not use the namespace std or include <iostream>. I am branching out into other libraries.
2009-04-19 15:08:00

Author:
Unknown User


... I know a bit of Java based on what I learned first semester in computer Programming.

I don't know enough to really code my own things, but I do know enough to read most of what people write and so figure it out.
2009-04-19 15:17:00

Author:
RockSauron
Posts: 10882


Yes as amatter of fact i am a coder.

I got Herbet Schildt's C++ The definitive guide and am well on my way.

Although.. you are using an old compiler and should be using iostream not stdio.
2009-04-19 15:26:00

Author:
CreateNPlay
Posts: 1266


Well I've been getting into Garysmod on Steam and that uses lua but I'll be ****ed if I can use it. It seems much easier than C++ as when i went to try and teach myself that I found it took a paragraph of writing to get the PC to say 'hi' :O2009-04-19 15:32:00

Author:
Shermzor
Posts: 1330


I was learning before but got distracted and quit. I'll probably take it up again this summer.2009-04-19 15:36:00

Author:
Killian
Posts: 2575


Well i tried that for over a week and actually in c++ it's

#include <iostream>
using namespace std;

int main()
{
cout >> "hi"'

std::cin.get();
return 0;
}

that took about 10 secs to write..
2009-04-19 15:36:00

Author:
CreateNPlay
Posts: 1266


Hey LittleBigMe,

Some ways to improve your code snippet:

You never initialise "total" - the compiler will likely set it to 0 at the beginning of execution but that's a dangerous assumption to make.

Also, using globals as a substitute for return values like you do here is generally frowned upon, for a number of good reasons. (In fact, using globals where not strictly necessary is generally considered bad practice, for a slightly smaller number of equally good reasons.)

Lastly, "pow (a, 2)" is an awfully awkward way of re-stating the obvious, "a * a". (The compiler will probably optimise the former into the same inline statement as the latter but it's still unnecessarily convoluted.)

Cheers!
2009-04-19 15:37:00

Author:
tameturtle
Posts: 150


What they said.

I program in C, C++, and Java and have written things that deploy to Windows, Linux, Mac, and Cellular phones. I've done it for a while but for the last year or two I've been trying to work on a couple 3D engines but they end up scrapped... too much work.

Python is a scripting language, it is interpreted rather than compiled and can be linked to your C++ classes.
2009-04-19 15:49:00

Author:
Foofles
Posts: 2278


Well i tried that for over a week and actually in c++ it's

#include <iostream>
using namespace std;

int main()
{
cout >> "hi"'

std::cin.get();
return 0;
}

that took about 10 secs to write..



yes but all of that writing just has the effect of saying 'hi' it baffles me as to what a more complex requirement would fill up such as trying to work out if in a war game someone is shot what angle, what place gives these results :eek:
2009-04-19 16:33:00

Author:
Shermzor
Posts: 1330


Hey LittleBigMe,

Some ways to improve your code snippet:

You never initialise "total" - the compiler will likely set it to 0 at the beginning of execution but that's a dangerous assumption to make.

Also, using globals as a substitute for return values like you do here is generally frowned upon, for a number of good reasons. (In fact, using globals where not strictly necessary is generally considered bad practice, for a slightly smaller number of equally good reasons.)

Lastly, "pow (a, 2)" is an awfully awkward way of re-stating the obvious, "a * a". (The compiler will probably optimise the former into the same inline statement as the latter but it's still unnecessarily convoluted.)

Cheers!

OK! So. I realize what you are saying there. I used pow(a, 2) so that I could start to use operators and functions in the maths.h library. I know it was clumsy. I have noted what you said about making a dangerous assumption, and I will remember it. I used globals because they had to be used in both the functions and main(). I know there are some ways around this but I didn't see anything wrong with using a global when it was the simplest solution.


Well i tried that for over a week and actually in c++ it's

#include <iostream>
using namespace std;

int main()
{
cout >> "hi"'

std::cin.get();
return 0;
}

that took about 10 secs to write..

What was the point of std::cin.get() in your code? And if it does have any relevance, you have already declared that you are using namespace std.


Yes as amatter of fact i am a coder.

I got Herbet Schildt's C++ The definitive guide and am well on my way.

Although.. you are using an old compiler and should be using iostream not stdio.

I know that it would have been easier to just use <iostream> in this code but it made little difference. I am just trying to branch out into other libraries. I have edited the post to explain why I used <stdio.h> and <math.h>.
2009-04-19 16:41:00

Author:
Unknown User


I can only do a bit of HTML, which isnt really coding as I am not really doing anything complex...

I wanna learn C++, but I dont know where to start.
2009-04-19 16:49:00

Author:
moleynator
Posts: 2914


I can only do a bit of HTML, which isnt really coding as I am not really doing anything complex...

I wanna learn C++, but I dont know where to start.



...

Here. (www.cplusplus.com/doc/tutorial)

Thats where I started and basically my only resource. It's a great tutorial!

Night all.
2009-04-19 16:55:00

Author:
Unknown User


Well, only ActionScript. And nothing big, either. Only the raw basics so I can add functions to buttons and stuff.

Here are a couple of examples:



on (release) {
_root.counter -= "-1";
if (_root.counter<="10") {
_root.spin.play();
} else if (_root.counter>="10") {
_root.spin.gotoAndStop(25);
}
}


That's a very simple counter. Wow(!)



speed = 5;
car.onEnterFrame = function() {
with (car) {
if (Key.isDown(Key.RIGHT)) {
_x += speed;
if (_x > 339) {
_x = 339;
}
_root.display = "Right";
} else if (Key.isDown(Key.LEFT)) {
_x -= speed;
if (_x < 60) {
_x = 60;
}
_root.display = "Left";
} else if (Key.isDown(Key.UP)) {
_y -= speed;
if (_y < 114) {
_y = 114;
}
_root.display = "Up";
} else if (Key.isDown(Key.DOWN)) {
_y += speed;
if (_y > 244) {
_y = 244;
}
_root.display = "Down";
}
}
};


Press a button to move a red circle around. I called the circle car, because "car" is quicker than "circle" to type!



on (release) {
if (_root.pass == "1024") {
play();
_root.pass = "";
_root.msg = "";
} else if (_root.pass == "1,024") {
play();
_root.pass = "";
_root.msg = "";
} else if (_root.pass == "EXIT") {
gotoAndPlay(1);
_root.pass = "";
_root.msg = "";
} else if (_root.pass == "INSTRUCTIONS") {
gotoAndPlay(47);
_root.pass = "";
_root.msg = "";
} else if (_root.pass == "instructions") {
gotoAndPlay(47);
_root.pass = "";
_root.msg = ""
} else if (_root.pass == "CREDITS") {
gotoAndPlay(17);
} else {
_root.msg = "Try again.";
_root.pass = "";
}
}


I made a quiz, and this is one of the questions. It has I believe around 20+ questions... so yeah... took way longer than I expected. LOL.

Alex.

Edit: Aww, its all messy. Sorry
2009-04-19 16:56:00

Author:
alexbull_uk
Posts: 1287


I started by using Game Maker, which uses both Drag and Drop, and Line by Line. I quickly moved to the line by line, and can make pretty advanced stuff in Game Maker. I just don't have the graphics drawing capabilities to make anything advanced. I know coding principles well enough to pick up most languages fairly quickly (I've dabbled in LUA, VB, actionscript, and C++), but I don't code much. I would love to code properly some day, as in making simple games, but yeah.

And LBM, this:


int SquareSum ()
{
int countUp, sumOfOnehundred;
for (countUp = 1, sumOfOnehundred = 0; countUp < 101; countUp++)
{
sumOfOnehundred += countUp;
}
totil = pow(sumOfOnehundred, 2);
return sumOfOnehundred;//LBM, surely it should be return totil at any rate?
}

Can be shortened to


int SquareSum ()
{
int sumOfOneHundred;
sumOfOneHundred=(100*101)/2;
totil=sumOfOneHundred*sumOfOneHundred;
return totil;
}
2009-04-19 16:58:00

Author:
dawesbr
Posts: 3280


...

Here. (www.cplucplus.com/doc/tutorial)

Thats where I started and basically my only resource. It's a great tutorial!

Night all.

Im guessing its plus, not pluc...

Thanks and goodnight my ozzy friend.
2009-04-19 16:59:00

Author:
moleynator
Posts: 2914


I started by using Game Maker, which uses both Drag and Drop, and Line by Line. I quickly moved to the line by line, and can make pretty advanced stuff in Game Maker. I just don't have the graphics drawing capabilities to make anything advanced. I know coding principles well enough to pick up most languages fairly quickly (I've dabbled in LUA, VB, actionscript, and C++), but I don't code much. I would love to code properly some day, as in making simple games, but yeah.

And LBM, this:


int SquareSum ()
{
int countUp, sumOfOnehundred;
for (countUp = 1, sumOfOnehundred = 0; countUp < 101; countUp++)
{
sumOfOnehundred += countUp;
}
totil = pow(sumOfOnehundred, 2);
return sumOfOnehundred;//LBM, surely it should be return totil at any rate?
}

Can be shortened to


int SquareSum ()
{
int sumOfOneHundred;
sumOfOneHundred=(100*101)/2;
totil=sumOfOneHundred*sumOfOneHundred;
return totil;
}

My coding style is to work things out in the code. I didn't know that is was going to equal 5050 * 5050... And yes it would return it anyway, I don't know why I put that in there. I have changed it to a void function.
2009-04-19 17:03:00

Author:
Unknown User


LBM, the sum of any list of consecutive numbers starting from 1 is the multiple of the highest number and itself plus one, all over two. So:


int sumOfSeries (upto)
{
int total;
total=(upto*(upto+1))/2;
return total;
}

That will return the sum of any set of consecutive numbers starting from zero. And:


int sumOfSeries (lower,upper)
{
int total;
total=((upper*(upper+1))/2)-((lower*(lower+1))/2);
return total;
}

is for any set of consecutive numbers within the upper and lower bounds (upper, lower)
2009-04-19 17:07:00

Author:
dawesbr
Posts: 3280


Ooooooh, thanks. Very helpful!2009-04-19 17:28:00

Author:
Unknown User


I know, it was such a revelation when I found it out!2009-04-19 17:29:00

Author:
dawesbr
Posts: 3280


LBPCentral Archive Statistics
Posts: 1077139    Threads: 69970    Members: 9661    Archive-Date: 2019-01-19

Datenschutz
Aus dem Archiv wurden alle persönlichen Daten wie Name, Anschrift, Email etc. - aber auch sämtliche Inhalte wie z.B. persönliche Nachrichten - entfernt.
Die Nutzung dieser Webseite erfolgt ohne Speicherung personenbezogener Daten. Es werden keinerlei Cookies, Logs, 3rd-Party-Plugins etc. verwendet.