[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hi /g/, programming noob here. I made a tiny c++ script that
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 49
Thread images: 2
Hi /g/, programming noob here.
I made a tiny c++ script that takes some amount of numbers and returns their GCF. It works, but I have doubts regarding efficiency.
If anyone would check the code and write back, I'd be grateful.
http://pastebin.com/nVTP42n6
Comments included for readability.
>>
Looks fine, but you can do this in for loops to make it look a little better (in my opinion)

[Code]
For(int I =0; I < whatever; i++)
[/code]
>>
>>51912441
shouldn't gcf = nums[0] be outside the for loop here?

for (i = 0; i < c; i++) {
gcf = nums[0];
if (nums[i] < gcf) {gcf = nums[i];}
}


also check out the wiki page for other algorithms involving finding gcd

https://en.wikipedia.org/wiki/Greatest_common_divisor
>>
>>51912441
Why do you need to input amount of numbers if you space-separate them anyway
>>
>>51912565
because OP is a programming noob.
>>
>>51912441
>C++
>runtime-sized array
turn on warnings OP, that's not C++
>>
>>51912441
http://www.wolframalpha.com/input/?i=gcd+of+45+60+90+120
>>
>>51912599
This. What the actual fuck?
>>
>>51912652
no fake fucks given
>>
How do you start programming in C++? In java you need to download the JDK, anything like that for C++?
Can I just use eclipse?
Thanks yall
>>
>>51912441
>C++
>script
kek
>>
>>51912679
g++ and a good text editor (vim)
>>
>>51912679
You just need a compiler.
>>
>>51912441
>c++
>script

if you're overly worried about efficiency, then just use the STL. the odds are great the implementers of you're compiler have done it more efficiently than you can.
#include <experimental/numeric>
using std::experimental::gcd;

http://en.cppreference.com/w/cpp/experimental/gcd

otherwise, you're already using a close to the hardware language. profile to find out if you're worried.
>>
>>51912679
https://isocpp.org/get-started
>>
>>51912694
so eclipse would work, yeah?
>>
>>51912709
>then the implementers of your compiler
this has nothing to do with the compiler, right? more like the library implementors?

no sarcasm / hostility meant at all - just trying to figure out if I'm missing anything.
>>
>>51912709
I except Boost is more efficient, http://www.boost.org/doc/libs/1_34_1/doc/html/boost_math/gcd_lcm.html.
>>
>>51912599
I did have warnings on and saw it's not OK to do it, but it worked. Alternatives to this?

>>51912686
What's a script and what isn't?
>>
>>51912733
While this particular function is still in the TS stage, and therefore not yet part of the official standard, the Standard Library is in fact required to be part of any C++ implementation by definition.

Therefore, saying "Use the STL" simply implies using the standard library implementation provided by the compiler vendor, whatever it's source (eg, MS contracts w/ Dinkumware for the bulk of theirs).

There are ofc thousands of libraries that are part of C++, but the Standard Library, by definition, is.
>>
>>51912758
A script is interpreted not compiled.
>>
>>51912733
Most of the time the standard library more or less comes with the compiler, e.g. gcc and libstdc++, clang and libc++, Visual Studio and its standard library.
>>
>>51912772
>that are not part of C++*

>>51912758
>What's a script and what isn't?
C++ code certainly isn't by long tradition, but w/e.
>>
>>51912790
>C++ code certainly isn't by long tradition, but w/e.
cling exists: https://github.com/vgvassilev/cling ;_;
>>
>>51912798
that is pretty cool, need to try it out.
>>
>>51912441
You program is shit. It can't even count the amount of numbers you give it? Absolute cancer

You commenting style is shit
    int c /*Entries in array*/, i /*Control for inner loops*/, o /*Control for outer loops*/;


Would work much better as
   
int c; //Entries in array
int i; //Control for inner loops
int o; //Control for outer loops


Because it's readable. And your variables are shit too. It's a compiled language for fucks sake, name your variables something that makes fucking more sense than just single letters
>>
>>51912844
iterates can be one letter, especially when using >>51912477
>>
>>51912709
Alternately use http://www.boost.org/doc/libs/1_59_0/libs/math/doc/html/gcd_lcm.html
>>
>>51912755
maybe, maybe not (though I wouldn't be surprised). always profile first instead of just taking a comment pulled out of some anon on the internet's ass at face value.

as is often the case at this stage, it's likely that the boost implementation is the primary model for what will become part of the standard--though it gets wrung out much more fully before the standardization is accepted than boost will manage with it.
>>
>>51912844
Let me add something
        if (gcf == 1) {
cout << "None found, sorry m8.";
return 1;} //Return 1 to end the program prematurely and not show the success message.
cout << endl << "Found it! \nThe greatest common factor of your numbers is " << gcf << "." << endl;
cout << "Ain't you glad you wrote this handsome time-saving script?";

return 0;}


Your indentation is cancerous
>>
>>51912862
good point. at least you link the latest version heh.
>>
>>51912758
Alternative is using dynamic memory allocation (new/delete in c++)
>>
>>51912758
>Alternatives to this?
Using C++ standard data container such as std::vector<> (or even std::array). Not archaic approaches like this one:
>>51912894
>>
>>51912914
This. You should never use new/delete in modern C++. If you absolutely, positively have to manually allocate memory, use std::make_unique or std::make_shared.
>>
>>51912948
>If you absolutely, positively have to manually allocate memory
Or put the low-level ops inside a similar RAII-capable wrapper.
>>
>>51912960
Put std::unique_ptr/std::shared_ptr into your RAII-capable wrapper.
>>
>>51912995
Not if you're writing library code. But w/e that's not a typical topic in general for this type of forum. Sure, we'll go with you're previous advice in general anon.
>>
>>51912914
Thanks for the pointer. Would you know where to find a decent tutorial on those?

>>51912844
>>51912866
t-thanks
>>
>>51912844
//-style comments are so gauche
>>
>>51912441
If you want to write efficient loops, you need to optimize the pipeline.
Read this:
https://en.wikipedia.org/wiki/Instruction_pipelining
Then rewirte everything in Assembler.
>>
>>51913333
>quads chkd

The best way is to use a good book like Stroustrup's freshman textbook. Otherwise, you can use the cplusplus.com tutorial on it anon.

The idea is simple: Declare the type that is contained in the vector:
vector<int> vi;


You can also initialize them at the time of instantiation:
vector<int> vi{1, 2, 3, 4, 5};


Accessing elements individually is easy:
for (int i = 0; i < vi.size(); ++i)
cout << vi[i] << endl;


and, in modern C++ it's even easier:
for (auto e : vi)
cout << e << endl;


Those are some basics anon. Consult PPP2 or the tutorial site for more info.
>>
>>51913373
>gauche
nah senpai they're way nicer looking than /* comment */
using /* */ looks messy unless ur commenting a shit ton of stuff
>>
>>51913764
In the punctuation-littered world of C syntax the /* */ delimiters just fit in better typographically, and they are much more versatile. // looks too sleek for C and it would fit in better with a more sparsely punctuated language like Python
>>
File: lebron.jpg (41 KB, 500x333) Image search: [Google]
lebron.jpg
41 KB, 500x333
>>51913916
aight fair enough. I was thinking from a java point of view so you're probably right
>mfw senpai filters to senpai
>>
>>51912441
>C++ script
>>
>>51912441
Asking the user how many numbers there are is stupid.
You can just get it from the number of numbers the user enters, which should be passed from the command-line for convenience.
>>
>>51912441
Shit placement of braces.
>>
>>51912844
int c; //!<Entries in array
int i; //!<Control for inner loops
int o; //!<Control for outer loops


:^)
>>
>>51914137
My classmates tell me the same, but it's just comfortable for me. Is there a standard on brace placement or something?
Thread replies: 49
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.