[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
/dpt/ - Daily Programming Thread
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: 255
Thread images: 36
File: himegoto.webm (3 MB, 720x405) Image search: [Google]
himegoto.webm
3 MB, 720x405
old thread: >>54377924

What are you working on, /g/?
>>
>>54383013
Reading a book while trying not to fall asleep at my desk
>>
I picked up this bash line on 4chan somewhere. I love it, and maybe someone else can use it.

It downloads all the pics/webms/gifs in a thread.

wget  -q -O - $link | sed "s/>/>\n/g" | grep "eThum" | cut -d ' ' -f 3 | sed -e "s/href=\"/http:/g;s/.$//" | wget -q -i -
>>
>>54383013
studying for my CS/discrete math exam on Friday

wondering what to do for fun
something with C++ (>fun) or Java (>fun)
>>
>>54383013
Thread started by
Retarded
Anon
Phaggot
edition
>>
>>54383013
>Posted before the bump limit
>Spamming your faggy cancer
Delete this thread and kill yourself
>>
>>54383074
>>54383111
Dumbass.
>>
>>54383013
I want to fuck Hime until my dick breaks
>>
File: wheeeee.gif (178 KB, 160x160) Image search: [Google]
wheeeee.gif
178 KB, 160x160
My linked list implementation features random access in O(1) time!
struct list {
struct node *head;
struct node **addresses;
unsigned items;
};


addresses is an array of node pointers that points to every node in the list!
>>
>>54383342
your insertion and deletion are slower of course but that doesn't matter really

When would you ever need random access to a linked list though?
>>
>>54383342
wow!
>>
>>54383364
now that I think about it, deletion is really bad. O(n) worst case.
>>
>>54383342

Well, now you can no longer O(1) insert at middle (have to update all of the pointers in the array backup). Why not just use a vector?
>>
>>54383488
because they don't teach what vectors are in intro to cs
>>
>>54383500
Right. They teach them in Linear Algebra.
>>
>>54383592
uh, no, they teach them linked lists
>>
File: image.png (699 KB, 1024x456) Image search: [Google]
image.png
699 KB, 1024x456
>>54383013
The SICPWriter is done. Not entirely sure why I did this. Slightly regret it.
>>
>>54383682
Does it actually have anything to do with SICP?
>>
>>54383721
No. I write my notes in it and it saves them as .sicp.
>>
>>54383742
Did you at least write it in scheme?
>>
/dpt/, I saw a simple problem that I actually found to be a breath of fresh air after endless amounts of fizzbuzz and array rotation type programming 101 interview questions; I'm curious to see how you guys go about solving this if any of you choose to take a stab at it:
>Shuffle an array of characters so that no duplicate characters are next to each other.
ie. ['a', 'b', 'b', 'c'] would need to be shuffled so that 'b' is not next to the other 'b' such that ['b', 'a', 'b', 'c']
Whether or not this is treated as a circular buffer where the first and last elements are considered 'adjacent' to each other was not specified. I want to say for this problem that the first and last elements are not adjacent to each other.
>>
>>54383749
of course not.
why would i learn an archaic scripting language like lisp?
>>
>>54383837
I'm just trying to figure out why you wrote a text editor in the first place.
>>
>>54383500

So? They're still a relatively simple data structure that gets used all the fucking time.
>>
>>54383682
How do you store your text? An array? Linked list?
>>
>>54383869
I'm agreeing with you famalam, I'm also not the anon that posted the linked list struct
They definitely should be taught, I was saying that the reason he isn't using a vector is because it's not taught in babby classes like it should be
>>
>>54383874
>How do you store your text?
>Linked list?
what
>>
>>54370354
a bit late... but, look into gore:
https://github.com/motemen/gore
>>
>>54383914
I'm just listing data structures other than the correct one. I'm curious to how it's done, unless it was all using provided constructs, in which case it's not more impressive than a temperature converter.
>>
>>54383937
I honestly haven't built a text editor but I assume you would use some sort of dynamic list of arrays, i.e. list[0] contains the first 1kb, list[1] contains the second 1kb, etc. This way if the user goes over 2kb, you just grab another kb, but if they go under 2kb again, you don't have to shift all of the data around, just 1kb
I doubt 1kb is the best size but I think I read something like that somewhere
>>
>>54383982
You're close. You also have to remember where the cursor is and have insertion, deletion and moving the cursor forwards and backwards be quick.
>>
>>54383801
Does the result need to be random (ie, every possible output is equally likely), or is any result where no two chars are adjacent fine?
>>
>>54384001
Yeah I'm not sure how in any way insertion and deletion would be quick with what I imagined.
>>
>>54383982
umm ok
now how do you insert text in the middle of your text document?

Just use a flat array text buffer you dumb shit.
>>
>>54384017
>flat array text buffer
top
>>
>>54384016
When I did this for class, we used a gap buffer that fit 16 chars. These were connected to each other like a linked list.
>>
>>54384066
neat
>>
>>54383874
The guy who wrote it here.

I save the text in to a .sicp text file. I don't need to handle how it's stored in the actual GUI, the plaintextedit widget handles it all.
>>
File: Java_ai.png (12 KB, 550x550) Image search: [Google]
Java_ai.png
12 KB, 550x550
>>54383013
I have to implement a file system in Java for a project. Any tips? I've already downloaded 9 projects from GitHub to help me get this done.
>>
>>54384154
What's a filesystem?
>>
>>54384161
Are you asking so he has to come up with an answer or do you not know?
>>
>>54383801
I did it in pseudo code for the algorithm:

make a blank char array that will act as the new array.
Add the first char of the buffer to the new array and have an iterator point to it. loop through the old array starting at 1 and only add to the old array if that char doesn't exist at the most recent position of the new array. If the chars match then send a new iter in the old array from where you are and look for a char that is different and add that to the new array and mark the old array spot as NULL and then add the one you were stuck on. and proceed from there.

idk if it's a good algorithm but it should work. The only problem is the worst case of a buffer containing a fuck ton of the same characters will need to a time complexity near O(n^2)
>>
File: Screenshot_2016-05-04_19-47-56.png (45 KB, 590x324) Image search: [Google]
Screenshot_2016-05-04_19-47-56.png
45 KB, 590x324
>>54383801
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void *shuffle(char *arr, size_t len)
{
char prev = 0, curr;
char ret[len];
time_t t;
int index = 0;

srand((unsigned) time(&t));

for(int i = 0; i < len; ++i)
{
index = rand() % len;
curr = arr[index];
if(curr == prev || curr == 0)
{
--i;
continue;
}
ret[i] = prev = curr;
arr[index] = 0;
}

for(int j = 0; j < len; ++j)
{
arr[j] = ret[j];
}
}

int main(int argc, char *argv[])
{
char arr[] = {'a', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'f', 'f'};
size_t len = 17;

shuffle(arr, len);

for(int i = 0; i < len; ++i)
{
printf("%c%c", arr[i], i + 1 == len ? '\n' : ',');
}

return 0;
}


It's not perfect, as it will sometimes hang (I haven't investigated why yet), but when it works, it works
>>
>>54384169
it's bait
>>
>>54384161
I think it's a way of keeping track of data on disk.
>>
>>54384154
/dev/null makes a fast filesystem.
>>
>>54384169
I don't know the answer and how I write the solution depends on it.
>>
>>54384173
I can do it in O(n logn). Sort the array, place the first half of the sorted array on a new array of the same size on each even index and the second half on each odd index.
>>
does anyone have good places where I can take a practice coding test?

I have seen hacker rank but they only have one practice exam that I can take.

I am already reading Programming Interviews exposed but trying to get a place where I can get to practice under timed stress.
>>
>>54384247
neat!
>>
Still little progress on the job hunt. Web Dev is getting tempting as fuck now for a modicum of employment
>>
>>54384213
I'm doing this on Windows, and the file system is supposed to be OS independent. It's running on the JVM.
>>
>>54384253
Look at university's sites.
I googled "university past exams computer science" and found this great resource:
http://www.cl.cam.ac.uk/teaching/exams/pastpapers/

It's not exactly programming but it will test your knowledge for sure.

For actual programming, I'd look at exercises in textbooks. If you're learning C, C Primer Plus has great exercises at each section for the topics taught in that chapter. If you already know the language you're learning, some good practice problems can be found in Cracking the Coding Interview.
>>
>>54384281
I'm a bit confused on what exactly you're going to be doing by making a file system. Are you going to have one file, and then your program will either 'mount' it to the drive so you can actually explore it with your native file explorer and then 'unmount' it by saving that directory to that one file again and removing the directory, or does your program just open the file and have its own file explorer, no mounting necessary? Or something completely different?
>>
>>54384213
But is it web scale?
>>
>>54384009
That is left up to you to decide.

>>54384173
Try implementing that. How would you go about improving this? How should you go about handling the buffer containing a fuck ton of the same characters? Are there any edge cases you may have overlooked?

>>54384178
Interesting. I'll take a closer look at that a bit later. I'm curious to know if you figure out why it's hanging at times; I'd look into it myself but I'm in the office at the moment.
>>
>>54384339
Actually, yes.
https://devnull-as-a-service.com/
>>
>>54384339
Of coursh
>>
>>54384178
What are you doing? Your algo makes no sense. Not to mention it's super easy for it to access out-of-bound because of the --i; continue;. What's the point of the prev if you can't cache all previously stored values? Also there may be multiple of the same value, caching the previous would break it in that corner case, no?

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
>>
>>54384362
Oh shit. I realize that it isn't a generic shuffle. Disregard me.
>>
>>54384336
It's totally self contained and written in Java. It runs a shell to make directories and files.
>>
>>54384409
Where are you actually saving the files to?
>>
>>54384304
ok I will try to do that. I dont have a leaning as far as language. Currently I have worked more with java but I have experience in C++.

its just I have only gone into one interview and have answered the questions they gave. like yesterday I had an interview and the guy was asking me the log time of bubble sort. The problem is that I used it analy. of algorithms class but I could not remember it and endded up guessing. I mean I should have known it but the guy did pick a single sort and quizzed me on it. I know there is no way for me to know every single sort but men its stinks that I couldnt answer it, end of losing an internship over it
>>
>>54384453
You may not need to memorize the run time of every single sort, but Bubble Sort is notorious for being horrible. You should know how its implemented and be able to realize the run time knowing that, in case you forgot it. It sounds like you shouldn't have gotten the job anyway famalam
>>
>>54384451
An emulated disk written in Java.
>>
>>54383801

How does this look?
Python 3.4:

import itertools

li = ["a", "b", "b", "c"]

def find(x, y, z):
return (x, y) in zip(z, z[1:])

for x in itertools.permutations(li):
duplicate = False
for element in x:
if find(element, element, x):
duplicate = True
break

if not duplicate:
print(x)



Output:

('a', 'b', 'c', 'b')
('a', 'b', 'c', 'b')
('b', 'a', 'b', 'c')
('b', 'a', 'c', 'b')
('b', 'c', 'a', 'b')
('b', 'c', 'b', 'a')
('b', 'a', 'b', 'c')
('b', 'a', 'c', 'b')
('b', 'c', 'a', 'b')
('b', 'c', 'b', 'a')
('c', 'b', 'a', 'b')
('c', 'b', 'a', 'b')
>>
>>54384154
https://github.com/philipl/pifs
implement this in java
>>
>>54384253

google foobar
>>
New thread: >>54384420
>>
>>54383801
take array and place chars into a multiset sorted by the number of each character (inserts are O(1), sort inverse map is O(n lg(n)) ). Start with the character with the most count and do step =ceil(len/count). Step along the array in 'step' intervals putting the character in each position; factor any non-null spaces out of the positional count. Decrement the total length of the array by the count of the character. Repeat until no characters remain in the multiset. This is O(n).

Also you can do a quick sanity check in O(n). If any of the count of characters in the multiset is greater or equal to half the length of the array, no such solution can be found.
>>
>>54384495
No thanks. I already have 9 example filesystems in Java.
>>
>>54384491
It's clever and stupid at the same time. I'm curious why you didn't define find as find(x, z) that returns (x, x) in zip(z, z[1:]). But your implementation is like O(n*n!) or something.
>>
>>54384491
>permutations
Figures a pyshitter would come up with this solution.
>>
>>54384473
This. Just ask some clarifying questions.
>Hm I don't remember the complexity of that, but the Java standard lib has n * log (n ) ...
>Well let's see if I can work it out. What is bubblesort, again? If I recall, this is the algorithm (writes peudocode) does that check out?
>Ok, I see two nested loops here. Right away that makes me think O (n^2)....
>>
>>54384508
less than 100 posts

>>54384473
i could have done without that last sentence
>>
>>54384550
Because I remember using that function in another piece of software that I did, which originally I think I stole from stackoverflow. So I copied it directly in and used it because I was lazy.

Also, I don't doubt how inefficient it is.
>>
>>54384575
You didn't have to make this post, everything you said it pretty much expected from a python user.
>>
>>54384595
>everything you said it pretty much expected from a python user.
Yeah, like results?
>>
>>54384615
Where in that post did you say anything about results?
>>
>>54384627
The result is my working Python solution.
>>
>>54384658
An interviewer would not hire you for such a solution.
>>
>>54384679
Fortunately this isn't a job interview, or even remotely close.
>>
>>54384679
Why ate you bullying? It's better than the C solution, which is O(forever). That makes it the best solution so far.
>>
>>54384178
>trying to debug in gdb
>now it starts working every single time
>try it outside the debugger
>fails immediately
GOD FUCKING DAMMIT
>>
Hey /dpt/.

I feel like I can't actually do anything useful. I can make shitty games, I can do semi-difficult math problems, but all of my projects have been things for fun. I've seen people (i.e. github users) with impressive, useful projects that a lot of people have found useful or at least interesting. How do I start actually making things? Things people will use?
I know C, Java, Python, I could brush up on my C++ if I wanted. I know SDL, libGDX as far as libraries go.
What should I learn that could enable me to make useful things?
>>
>>54384734
Well its either a race condition or compiler optimizations.
>>
>>54384734
Start your program normally and then attach gdb rather than starting your program from within gdb.
>>
>>54384350

quick implementation before I start studying for databases:

#include "iostream";
using namespace std;


void randomize(char shuffle[]);
int main(void)
{
char shuffle[11] = { 'a', 'b', 'b', 'c', 'd', 'b', 'b', 'b', 'd', 'd', 'h' };
int length = 11;
int i = 0;
cout << "not-random array: ";
while (i < 11)
{
cout << shuffle[i];

i++;
}
cout << endl;
randomize(shuffle);
}

void randomize(char shuffle[])
{
char random[11];
int i = 1;
int j = 1;
int k = 0;
memset(&random[0], 0, sizeof(random));

random[0] = shuffle[0];

while (i < 11)
{
if (shuffle[i] == random[k])
{
j = i;
while (shuffle[j] == random[k] || shuffle[j] == 'T')
{
j++;

}
k++;
random[k] = shuffle[j];
shuffle[j] = 'T';


}
while(shuffle[i] == 'T' && i < 11)
{
i++;
}
k++;
random[k] = shuffle[i];
i++;

}
i = 0;
cout << "random array: ";
while (i < 11)
{
cout << random[i];

i++;
}
cout << endl;


}


A lot can be improved and I can just omit the second array I think. It may involve using a head and tail iterator instead of just starting at the front.
>>
>>54384530
from collections import Counter as Multiset
from math import ceil

q = ['a', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'f', 'f']

p = list(
sorted(
map(
lambda x: (x[1], x[0]), #flip pair
Multiset(q).items()
),
reverse=True
)
)

r = [None]*len(q)
l = len(r)
for (num, char) in p:
i = 0
step = ceil(l/num)
for p in range(len(r)):
if r[p] == None:
if (i % step == 0):
r[p] = char
i += 1
l -= num

print(r)
>>
>>54384833
>>54384868
I just figured it out
My program sometimes reaches the point where there's only one character left in arr[] (when I was testing, it was 'f') and the character in prev is the same character ('f' again in my case). At that point prev won't change and my program keeps picking random characters from arr[] until it finds one that is neither '\0' nor one that matches prev (which it won't in this case since it'll only get '\0' or 'f')
>>
>>54384980
Is that because the implementation is bad, or because there are more than length/2 number of that character in the array?
>>
>>54383013
Seeing Home so often is making me love traps but I'm not gay.... Atleast I think I'm not
>>
>>54385003
Bad implementation, this is the array I'm testing with
{'a', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'f', 'f'}
>>
>>54385020
>>>/gif/trap
>>
File: home-loan.jpg (17 KB, 500x333) Image search: [Google]
home-loan.jpg
17 KB, 500x333
>>54385020
Does this turn you on by any chance?
>>
>>54385042
great post
>>
It's been a while, but I think I'm going to implement functions for my shitty lang. Arrays may or may not come next, they'll actually be more difficult.
>>
>>54385020
it's not gay if they're wearing a skirt
>>
why am i so terrible at programming /g/


everything i make is a bloated mess


at least it works
>>
>>54385092
Here's an example btw
>>
Is there any functional difference from uint8_t and unsigned? I have a number I know can't be more than 100. What happens when someone tries to put more than what's allowed in uint8_t anyways?
>>
>>54385173
yes, using uint8_t makes your code unportable
>>
>>54385186
>Unportable
What do you mean, and how? This is C++ btw, not sure if there's differnet support in C.
>>
>>54384173
That would actually be O(n^m). You would have to recurse over some subset of the array a number of times that correlates to the value of the input and not just the size. If I'm understanding it correctly. It's a unique solution though.
>>
>>54385173
>Is there any functional difference from uint8_t and unsigned?
Yes. uint8_t is always an unsigned 8-bit number and has a range of [0, 255]. unsigned implies int, and an unsigned int can be an unsigned 16-bit or 32-bit number.
>What happens when someone tries to put more than what's allowed in uint8_t anyways?
If you put a number larger than the range of the storage space the number is truncated, bitwise.
>>
New thread here: >>54384420
>>
>>54385020
it's not gay if she has a feminine penis
>>
>>54385253
>107 posts
Fucking die.
>>
>>54385220
> You would have to recurse over some subset of the array a number of times that correlates to the value of the input and not just the size.

yeah that's the problem. in a worst case of all the chars in the array the algorithm will do terrible.

I coded it here:>>54384900
there are some errors but it shows my thought process more clearly.

> It's a unique solution though.

ain't the best though. Would get destroyed for having such a crappy run time and space complexity.
>>
The Linux kernel by default uses the host gcc to compile itself, but osdev.org always tells me not to.
Who's right?
>>
>>54385389
>but osdev.org always tells me not to.
Where does it say that? The kernel can only be compiled with GCC. Clang is like 90% of the way there however.
>>
>>54385389
If you're following osdev.org for everything else, follow them on that.
>>
>>54385507
No, I'm talking about the HOST gcc installation.
OSDev says to always build a cross-compiler and use that to compile your own kernel, which I've done.

However I just built the Linux kernel and I noticed that it uses the host compiler, doesn't this cause problems? Why does OSDev always recommend against it?

>Where does it say that?
There are many instances of them saying it, here's just one article: http://wiki.osdev.org/GCC_Cross-Compiler

>>54385512
I don't exactly get what you mean.
>>
do any of you guys have experience with dedicated or cloud hosting?

I have a fairly small c++ app that I'm looking to get hosted, it uses a postgres database also. I'm researching it all right now but I honestly don't even know how it works, like do I just upload my executable and create my database?

the websites for the hosting companies throw around a bunch of buzzwords and seem to offer few facts and details.
>>
>>54385361
After looking closer it's not dependent upon the input, it's actually O(n^2). It's pretty clever, almost like a selectionsort, but swaps on difference. Mine is less of a swap. It collects the input and puts it back using some simple math to ensure no two similar touch each other.
>>54384915
>>
1) Best Android Programming books?
2) Any website like codeschool you guys would actually recommend? getting a birthday present soon and figured might as well signup for one of those sites
>>
>>54385870
Additionally, it's adaptive. It has a best case of O(n) if the input is already good. You might be able to make it only use one array by instead of setting 'T', inserting the number at the found position at the destination position and pushing the rest of the values up. That's a few calls to memmove.

It might be interesting to see how many different answers there are given some input. Combinatorics make me horny.
>>
File: art.png (674 KB, 1634x982) Image search: [Google]
art.png
674 KB, 1634x982
Too lazy to fix my minesweeper program, so I turned it into art.
>>
Can't get a job so I might as well try making something for experience. Any ideas?
>>
>>54384530
I implemented something similar in Haskell. I believe its just as efficient.
shuffle :: String -> Maybe String
shuffle xs =
case canBeSolved lettersByCount of
True -> Just $ go lettersByCount []
False -> Nothing
where
-- there is no solution if any character takes up more than half the list
canBeSolved :: [String] -> Bool
canBeSolved [] = True
canBeSolved as = length (head as) * 2 < (sum $ map length as)

-- sorts the input and groups it by character, then sorts that list by length
lettersByCount :: [[Char]]
lettersByCount = reverse . sortBy (compare `on` length) $ groupBy (==) $ sort xs

-- our recursive function. builds the result by sucessively appending the most frequent characters that remain
go :: [String] -> String -> String
-- if we've exhausted the input pool of letters, we're done
go [] bs = bs
-- if this is the last element, we're done. append it to result and return
go [[a]] bs = (a : bs)
-- if the first list is empty, remove it
go ([]:bs) result = go bs result
-- if the second list is empty, remove it
go (a:[]:bs) result = go (a:bs) result
-- if the first two lists have at least one element, append each to result
go ((a:as):(b:bs):cs) results = go (as:bs:cs) (a:b:results)

How the OP expects people to come up with this in 10 minutes is beyond me. It took three iterations to get O(n log(n))
>>
>>54383013
I want to make my own programming language. What's the best/ easiest way to generate machine code?

LLVM looks pretty nasty so far.
>>
>>54385114
Sauce? is it a pseudo assembler?
>>
File: android essentials.jpg (24 KB, 260x320) Image search: [Google]
android essentials.jpg
24 KB, 260x320
>>54385987
I know in my android programming app class we used "Android Studio Developement essentials 6 edition" by neil smyth.

Chapters are short and the examples are easy to follow. Also using google helped a lot too.

for the second part most of the stuff I found were free.
>>
>>54386021
bump

>>54386495
use lex and yacc
>>
>>54386600
also found these series of videos very helpful at least to begin with

https://www.youtube.com/watch?v=bsB2JUgXeGs
>>
>>54386625
>not doing everything yourself from scratch
uhhh
>>
File: 1454699993551.png (6 KB, 288x331) Image search: [Google]
1454699993551.png
6 KB, 288x331
>>54385114

I ended up putting functions in SP4. No fancy scoping rules, though. Only function-locals and globals.
>>
>>54386662
if you don't want help, don't ask for it
>>
>>54386665
Is there a builtin FizzBuzz function?
>>
File: cons_works.png (34 KB, 979x382) Image search: [Google]
cons_works.png
34 KB, 979x382
>>54386698

No. I never even wrote fizzbuzz in SP4, actually. I ended up getting sidetracked with my not-lisp.
>>
>>54386665
Heh, real neat. I just added an exit function for shits, decided not to do anything with it; my time is better spent doing things that aren't shit.

>>54386698
I can add one to mine real quick :^)
>>
>>54386675
oh im not him anon
>>
File: black_waifu_simulator.png (1 KB, 298x54) Image search: [Google]
black_waifu_simulator.png
1 KB, 298x54
>>54386709
>my time is better spent doing things that aren't

My time is essentially worthless, so it's all good fun.
>>
programming challenge!!!

You have to complete the FizzBuzz program with no modulo operator. The only rule is to not use any code similar to the one above yours. Ill start:
int main()
{
int i;
int three = 3;
int five = 5;
for (i = 0; i < 100; i++) {
if (three && five) {
printf("%d", i);
}
if (!three) {
printf("Fizz");
three = 3;
}
if (!five) {
printf("Buzz");
five = 5;
}
five--;
three--;
printf("\n");
}
getchar();
return 0;
}
>>
>>54386747
http://pastebin.com/XyXvpVCD
>>
>>54386743
Of course, if I felt like I didn't have anything I needed to work towards I'd do things just for fun and it would be great. also nice meme pic
>>
>>54386773

That's actually why I hadn't done much with anything lately. I've been caught up with an FAA project which, luckily, just ended.
>>
>>54386625
>use lex and yacc
>to generate machine code
I don't think you know what you're talking about.
>>
>>54386827
>>use lex and yacc
he meant flex and bison, clearly
>>
>>54386842
Both of which do not generate machine code either.
>>
>>54386747
int mod(int n, int m)
{
while (n >= m)
n -= m;
return n;
}

void fizzbuzz(int iters)
{
unsigned i;
for (i = 1; i <= iters; i++)
{
if (!mod(i, 3) && !mod(i, 5))
printf("fizzbuzz\n");
else if (!mod(i, 3))
printf("fizz\n");
else if (!mod(i, 5))
printf("buzz\n");
else
printf("%d\n", i);
}
}


can i have job now?
>>
>>54386871
One question first anon. It says here that you really like something called "traps". Care to explain what a trap is?
>>
>>54386902
I collect antique mousetraps and animal traps of all kinds, as I'm fascinated with their inner workings.
I also like to go to anime conventions while dressed like a 2D anime schoolgirl just because.
>>
File: Capture.png (442 KB, 942x509) Image search: [Google]
Capture.png
442 KB, 942x509
animu indexer.
finished indexing everything to around the end of 2008.

now going to work on user registration and a proper search and API.
after that going to see how far I can get with the anidb API.
>>
>>54386942
Hmm, interesting. I don't quite understand what the second part of your answer has to do with traps. Could you share some examples of "2D anime schoolgirls"?
>>
R8 my heap allocator.
http://pastebin.com/8jEhw7N2
>>
Yes this is a homework question.

It's an intro to micro-controllers and C class, my current problem is trying to understand a question we have and the language used to describe a bitwise operation.

'answer_1()' receives a bit number. Return a byte with the corresponding bit
set, and all other bits cleared

int n;
unsigned char ex_uc, ac_uc;

//rands a between 0-8 mods
n = rand() % 8;
//stores ex_uc by shifting a bit by n
ex_uc = (1 << n);
//
ac_uc = answer_1(n);

unsigned char answer_1(int arg0) {

for (int i = 0; i < 8; i++){
arg0 &= ~(1 << i);
}
return arg0;
}



I simply don't understand this aspect. What I've pasted is not the only attempts at this, the given materials don't go over an example like this to get me started so I'm lost.
>>
>>54387268
>//rands a between 0-8 mods
It's 0-7
>Return a byte with the corresponding bit set, and all other bits cleared
Literally all you need is
1 << n
. It's already in your code.
>>
>>54387268
a byte is 8 bits going from right to left (on little endian systems)

You're flipping bit #4 on a byte that looks like 0b00000000.
So that would look like 0b00010000, which also happens to be represent the integer 16.
>>
>>54387330
>(on little endian systems)
Endianess is irrelevant when it comes to bit ordering. It only matters when it comes to byte ordering.
>>
>>54387268
dude, you're doing it the stupid way

just do

unsigned char answer_1(int arg0)  return 1 << arg0;
>>
>>54387200
segfaults for me
>>
File: Alan-Turing-796x1024.png (544 KB, 796x1024) Image search: [Google]
Alan-Turing-796x1024.png
544 KB, 796x1024
You have been visited by the Alan Turing of coding prowess. Superhuman programming ability will come to you, but only if you write a virtual machine that can print the fibonacci sequence within the next 60 minutes.


>ps: I just did it in ten, so it's easily doable
>>
>>54383013
>What are you working on, /g/?
Lately I've been working on programming myself out of a crippling depression. I haven't been very successful and have been drinking myself to sleep more often than not
>>
>>54387621
>you have been visited by the homosexual computer wizard

ok
>>
>>54387621
I'm a bit confused by your assignment, do you mean write a virtual machine, and then write machine code for it to print out the fibonacci sequence?
Also, I'm not going to do your homework for you, but I might do it for myself
>>
>>54387637
Correct, virtual machine plus instructions
>implying I'm even a CS major
>>
>>54383342
>god only knows the cache performance
>delete and insert are slower than vector performance, best case, always
this doesn't work like you think it works.
>>
>>54387622
protip: don't drink
Replace it with something else to drink, honestly. It'll make this whole thing a lot easier.
Make sure you listen to music when you program too, and make sure you're following a book (if you're learning to program now) so that there's a clear progression ahead.
>>
whats a good naming convention for member variables.

I do "Width" "Height" but I see people do "m_Width" "m_Height".

what do you do.
>>
>>54387678

Publics: camelcase
Privates: camelcase with underscore

At least, that's how I do it.
>>
>>54387678
Depends on the language, but protip, don't start with a capital unless you're using C#

I do this >>54387689 as well
>>
>>54387320
>>54387554

Thanks, yes I don't know all the shortcuts yet. When I do come across them I get confused because it's not quite how we're taught.
>>
>>54387689

A leading underscore, I should note.
>>
>>54387689
>camelcase
EWWW
>>
>>54387678
camelCaseIsTheBestWay

~m_Because_Excessive_Punctuation_Makes<Code>*::Unreadable
>>
>SICP curriculum no longer prepared engineers for what engineering is like today.

http://www.posteriorscience.net/?p=206

I'll just leave this here...
>>
>>54387708
snake_case_is_objectively_better
easier_to_see_word_boundaries
>>
>>54387708
>camel Casels The Best Way
>>
>>54387704

fug u! xDD
>>
>>54387697
<< and >> deal with bits, not bytes for future reference
>>
>>54387736
except_it_still_gets -> messy::when<You> ~have_shittilly* = !designed_languages(that_use, way_too, much_punctuation);
>>
>>54386495
i dont like LLVM much either. you could compile to an existing VM (JVM, CLR, etc.) or an existing compiler's IR (for GCC, etc.). you could also compile to C if you dont care about performance too much for the time being. personally, i think the most fun is writing your own VM to target
>>
>>54387766
then don't use sepples you tard
>>
>>54387776
Or Haskell or Rust or some parts of Java
>>
>>54387645
>protip: don't drink
Easier said than done

>following a book (if you're learning to program now) so that there's a clear progression ahead.
That's half my problem. Programming is my day job, I'm actually pretty good at it(programming, but also my job), but I hate that the day to day is nothing but stupid device-specific tickets, lower-than-middle management, and trivial features (oh hey, let's write modal code for the trillionth time!). I want to do research-level stuff again but I'm too much of a materialistic to go back to the poverty that grad school implies. I've torn between good work and money and I've managed to pick money for the last 12 months.
>>
File: remove-kebab.jpg (126 KB, 1041x1066) Image search: [Google]
remove-kebab.jpg
126 KB, 1041x1066
>>54387708
>>54387736
>>54387766
all wrong, it's quite clearly kebab-case
>>
>>54387708
>not instinctively recognizing angel-parameter and star-reference syntax
I mean you may be right that C++ has objectively excessive line noise but if you haven't learned to stop letting that bother you by now you still need to get good.
>>
>>54387766
You should name your variables according to their scope.
Variables with small scopes like structs members should be short.
Variables with large scopes like global constants should be long and ALL_CAPS.
>>
File: c++ in a nutshell.png (72 KB, 1016x98) Image search: [Google]
c++ in a nutshell.png
72 KB, 1016x98
>>54387805
>sepplesfags will defend this
>>
Do you ever open up a project and just stare at your code and awe of how beautifully everything works together?
>>
>>54387824
I do that a lot.
Not because I want to masturbate to my old code, but I have this nagging feeling that I'll forget how to write code like that if I don't revisit it at least once a month.
>>
>>54387819
>c++ namespacing
>typename/struct/related syntactic stupidity
No, I won't defend that

>the almost-lisp-macro-tier profound power of C++ templates
I will defend this, to my death. Anyone who doesn't like templates is a stupid pussy who doesn't understand metaprogramming.
>>
>>54387824
i dont do it often but sometimes i look back to see how shitty my code was and laugh at it. or if other people in like an IRC chan are doing it ill find code from 5-6 years ago and then somehow find the courage to upload it
>>
>>54387621
#include <stdio.h>
#include <stdlib.h>

typedef unsigned long long reg;
struct vm {
reg reg0, reg1;
int* code;
int ip;
};

#define CODE_FIB 0
#define CODE_SHOW 1
#define CODE_HALT 2

void vm_init (struct vm* vm, int* code)
{
vm->reg0 = 0;
vm->reg1 = 1;
vm->code = code;
vm->ip = 0;
}

int vm_step (struct vm* vm)
{
reg t;
switch (vm->code[vm->ip++]) {
case CODE_FIB:
t = vm->reg0;
vm->reg0 = vm->reg1;
vm->reg1 = vm->reg1 + t;
return 0;
case CODE_SHOW:
printf("%llu ", vm->reg0);
return 0;
case CODE_HALT:
default:
printf("\n");
return 1;
}
}

static int* generate_fib_code (int n)
{
int i, ip = 0;
int* code = malloc((n * 2 + 1) * sizeof(int));
for (i = 0; i < n; ++i) {
code[ip++] = CODE_FIB;
code[ip++] = CODE_SHOW;
}
code[ip] = CODE_HALT;
return code;
}

int main (int argc, char** argv)
{
int N;
struct vm vm;

if (argc > 1) {
N = strtol(argv[1], NULL, 0);
} else {
N = 10;
}

int* fib_code = generate_fib_code(N);
for (vm_init(&vm, fib_code); vm_step(&vm) == 0; )
;
free(fib_code);
return 0;
}


How'd I do?
>>
>>54387907
I mean, that's not really the spirit of the challenge, but you're technically correct.
>>
>>54387621
Thank you so much for posting this challenge, I'm working on it right now and having a blast, writing tests in hex mode in vim and seeing it work is so fulfilling. This is my first time doing anything like this, I've always done high level stuff. Thanks anon, and I'll post my shitty code when I'm done.
>>
void function() {
SomeObject* object = MightReturnNullOrSomeObject();
function2(object);
}

void function2(SomeObject *&ref) {
if(ref == nullptr) {
return;
}
...
}

Fucking pointers are going to kill me.
Is this being null checked correctly or have I misunderstood pointer to ref?
>>
>>54388024
>nullptr

what is this hand-holdy babby tier shit?
>>
>>54388024
that's correct
>>
>>54388032
C++ replaced NULL with nullptr
C++ is far from hand hold-holdy babby tier shit
>>
>>54388043
so wait, a null byte and a nullptr address are different in C++?
that's fucking dumb
>>
>>54387621
What if my virtual machine only has one instruction?
>>
>>54387200
Your allocator allocates memory, not heaps.
>>
>>54387200
Is this a buddy system?
>>
>>54388065
I'm trying to do it with three: mov, add, and print
lol
>>
whats a cool thing to emulate like CHIP8 or whatever I want to make a small interpreter and eventually expand it into a recompiler maybe
>>
>>54388135
CHIP8 is an okay place to start if you don't follow that one shitty tutorial and instead just read the wikipedia and write it by yourself
>>
>>54388141
I wrote one but I want something more complex than the CHIP8(or its "expansion").
>>
next to rm -rf /usr is this the worst thing you can do to a server?

echo shutdown now >> ~/.bash_profile;
echo shutdown now >> ~/.bash_login;
echo shutdown now >> ~/.profile;
shutdown now
>>
>>54388162
fuck I meant
echo shutdown now >> ~/.bash_profile/fuck_you.sh;
echo shutdown now >> ~/.bash_login/fuck_you.sh;
echo shutdown now >> ~/.profile/fuck_you.sh;
shutdown now
>>
>>54388162
This is easily recoverable.
>>
>>54383342
>i don't understand how linked lists work

just use a vector / implement one please
>>
>>54388183
he's taking the piss m8
>>
>>54388183
The fact that anon is using a struct means he's using C, not C++

>>54388171
damn, I guess I'm not knowledgable enough to be devious. Maybe just smashing shit with a brick is the smart route
>>
>>54388053
They're the same, it's because C++ is trying to move away from C's (void*) shenanigans.
Implicit pointer casts work differently in C++, so you can't do
int* x = NULL;

I'm pretty sure. nullptr fixes this because it has a specific type 'nullptr_t' which CAN implicitly cast to any pointer type. In terms of actual output code, and underlying data, they are exactly the same.
But nullptr also allows you to specifically override methods like so:
void foo (int* p) { ... (A) }
void foo (nullptr_t) { ... (B) }

...
int x; foo(&x); // will choose (A)
foo(nullptr); // will choose (B)
...

This is used internally for shared_ptr<> and unique_ptr<> constructors, possible some others too.

>>54388088
Eh, terminology. It allocates chunks.

>>54388115
No not really. It just allocates stuff one after another and merges chunks together.
However
for (i = 0; i < n; ++i)
T_alloc(K);

is O(n^2) so I'm working on a different algorithm that's going to be closer to O(n) for the above code
>>
>>54388191
vectors are simple as hell to implement in C you moran
>>
>>54388191
>The fact that anon is using a struct means he's using C, not C++

C++ has structs, my guy.
>>
>>54388251
Join the never-encapsulating always-struct master race with me, trip fag!
>>
>>54388261

I use structs in C# from time to time. When you want something that's JUST ties data together, it makes sense.
>>
>>54388275
>When you want something that's JUST ties data together, it makes sense
Fuck OOP. Let data be data.
>>
>>54388282

That's why structs exist, right? Feels good.
>>
>>54388282
It's like you want spaghetti code or something.

OOP makes sense, but only if you realize that object are supposed to be treated like their own little self-contained state machines. Then it's extremely powerful.
>>
What should I program today?
>>
>>54388353
See >>54387621
>>
>>54388580
>yfw you don't complete it in 60 minutes
It's been like 2 hours and I can't figure out what's wrong with it. It seems to mostly work most of the time. A lot of error has been with me correctly finding the hex values for the commands I want to do, mistyping etc.
The opcode is the first 2 bytes, the register is the third byte, the fourth byte is whether the next value is an integer literal or a register, the remaining four bytes are either an integer or a register (there are only two registers)
so
 
16 mov reg0, 1 00000001 01
17 mov reg1, 1 00100001 21
18 print reg0 10010000 90
19 print reg1 10010001 91
20 add reg0, reg1 01010001 51
21 print reg0 10010000 90
22 add reg1, reg0 01110000 70
23 print reg1 10010001 91
24 add reg0, reg1 01010001 51
25 print reg0 10010000 90
26 add reg1, reg0 01110000 70
27 print reg1 10010001 91

I think I'm supposed to print the register I just added to, right?
I'm not sure. It's 4am and I have an exam on Friday that is extremely important, so I'm gonna get to sleep. I'll post my vm code after, the comment was too big:
>>
>>54388651
And the vm:
  1 #include <stdio.h>
2
3 int main(int arc, char **argv) {
4 FILE *src = fopen(argv[1], "rb");
5 unsigned char registers[2] = {0,0};
6 unsigned char *ip;
7 unsigned char program[1024];
8 fread(program, 1, 1024, src);
9 fclose(src);
10
11 int i = 0;
12 for (ip = program; *ip != 192; ip++) {
13 if (i++ == 1025) break;
14 switch((*ip & (3 << 6)) >> 6) {
15 case 0: /* mov */
16 if ((*ip >> 4) & 1) /* moving val from reg */
17 registers[(*ip >> 5) & 1] = registers[*ip & 1];
18 else
19 registers[(*ip >> 5) & 1] = *ip & 31;
20 break;
21 case 1: /* add */
22 if ((*ip >> 4) & 1) /* adding val from reg */
23 {registers[(*ip >> 5) & 1] += registers[*ip & 1];
24 printf("Adding %d to reg%d\n", registers[*ip & 1], (*ip >> 5) & 1);}
25 else
26 registers[(*ip >> 5) & 1] += *ip & 31;
27 break;
28 case 2: /* print value */
29 if ((*ip >> 4) & 1) /* printing value in register */
30 printf("reg%d: %d, ", (*ip >> 5) & 1, registers[((*ip >> 5) & 1)]);
31 else /* printing real val */
32 printf("%d, ", *ip & 31);
33 break;
34 case 3: goto end;
35 }
36 printf("\nregisters: %d, %d\n", registers[0], registers[1]);
37 }
38 end:
39 return 0;
40 }


I must note this is my first time using bitwise operators since I learned them two years ago, so figuring out exactly which values to use to get the bits I want took some time.
>>
>>54388658
You can make it as high or low level as you want. Personally I did it really quickly in javascript, using an array to store the functions for the instruction logic.
>>
>>54388658
Yikes, can't you factor just a little bit?
>>
>>54388735
It's C
>>
>>54388746
It's really shitty C.
>>
>>54388735
Why would I fambly?
>>
>>54388759
y-you too

I could make macros for all of the bitwise ops, like get_opcode, get_reg, etc. I might I guess.
>>
>>54388761
So I have even the remotest idea what your bitshits are doing.

>>54388777
That's what I would do personally, or just make the bytecode extremely simple and no bit shifts because of the time constraint.
>>
File: Untitled.png (64 KB, 1038x879) Image search: [Google]
Untitled.png
64 KB, 1038x879
>take a break from regular work, decide to make a small rpg binary module game mode for gmod for fun
>days later, still writing boilerplate database code
>lua doesn't have structs, don't know how to mess around with tables on the lua stack, have to send each variable one by one
kill me
>>
>>54388759

It's not too bad. The bit fiddling could be pulled out into some functions, but it's otherwise readable.
>>
>>54388783
I just decided to use 1 byte per op, and divided that byte into what I saw necessary. But yeah, the macros will help with that.

>>54388786
thanks love
>>
>>54388777
You could improve the readability quite a bit by just spacing the code out a bit, jesus christ.
>>
>>54388313
>but only if you realize that object are supposed to be treated like their own little self-contained state machines
It never works that way in practice. Objects are always reaching into the state of other objects.
>>
>>54388800
I think it's fine in that regard :)
>>
>>54388786
Fuck off your opinion is worthless.
>>
>>54388818

thank u
>>
>>54388823
Fuck off and drink bleach.
>>
>>54388823
I would also like you to fuck off you niggerloving prick
>>
>>54388784
Anon your code is terrible
What type are health/magic/agility/etc?
>>
>>54388829
>>54388836

Why are you foreigners such awful people?
>>
>>54388843
Fuck off and kill yourself.
>>
File: Untitled.png (9 KB, 255x342) Image search: [Google]
Untitled.png
9 KB, 255x342
>>54388838
I'm trying to get better. I'm still pretty new to programming, sorry if its not too good

Most of the them are ints. Lua doesn't actually have ints though, theres only doubles. its so shit
>>
>>54387907
>>54388860
stdint.h you retard.
>>
>>54388872
>being mean to a newbie
you're worse than him fambly
>>
>>54388860
>__int64
Pointlessly non-portable.
>Lua doesn't actually have ints though
They added actual integers in 5.3 or some shit.
>>
>>54388872
>>54388880
I was using stdint.h earlier, but oddly enough these are the types otl uses, which is more free than the mysql connector

>>54388880
Gmod lua doesn't have ints
>>
>>54388860
Assuming that's C++:

for (auto& a : { Data.Level, Data.Agility, Data.Endurance, Data.Intelligence, Data.Luck, Data.Speed, Data.Strength, Data.Willpower })
LUA->PushNumber(a);

for (auto& a : { Data.Index, Data.Health, Data.Magic })
LUA->PushNumber(a);
etc

I don't believe there is a built in zip in C++, but your Query bit is basically this:

Fold((+),Zip((+), {"agility","endurance","intelligence",...}, Map(std::to_string, {Data.Agility, Data.Endurance, }})
Will show you in F# in a sec
>>
>>54388892
What on earth are you going on about?
Anyway, stop what you're doing and always use stdint.h for specific sizes.
>>
>>54388872
I am >>54387907
Why am I obligated to use stdint.h??
>>
>>54388919
More portable when needing a specific size, and it's clearer.
You're a retarded newbie if you don't use it.
>>
>>54388951
Go to Rust if you want specific sized integers so badly. I just wanted long longs so I could get big fibonacci numbers, I never cared about the internal bit numbers.
>>
>>54388904
thank you!

>>54388951
Alrighty. Is this better?
    struct CharacterData
{
uint64_t Index;
std::string SteamID;

uint32_t Level;
uint64_t Health;
uint64_t Magic;

uint32_t Agility;
uint32_t Endurance;
uint32_t Intelligence;
uint32_t Luck;
uint32_t Speed;
uint32_t Strength;
uint32_t Willpower;

int32_t Notoriety;
int64_t Money;
};
>>
>>54388966
>Not using (u)intmax_t
>>
>>54388975
Sure, but you usually only need it if the specific size is important.
For something like a health counter the size is not important, and you can use an int.
>>
File: eg4.png (63 KB, 1296x610) Image search: [Google]
eg4.png
63 KB, 1296x610
>>54388975
Ok, here's an F# example (pic related)
In C++ you could do that like this

auto FieldNames = { "Agility", "Endurance", ... } ; // order must match

auto GetFields = [](CharacterData& Data) -> std::initializer_list<std::reference_wrapper<uint32_t>> {
return { std::ref(Data.Agility), std::ref(Data.Endurance), ... };
}
>>
>>54389022
Zip is like this:
list1; list2;

for (size_t i = 0; i < min(list1.size, list2.size); ++i);
// do something with: list1[i] and list2[i]
// e.g. push_back (make_pair(list1[i], list2[i]))

Fold is like this:
initValue; list;
for (size_t i = 0; i < list.size; ++i);
//change initValue based on list[i]
//e.g. initValue += list[i]
>>
>>54389020
Not according to >>54388951
>>
>>54389063

You should already realize that everything on /dpt/ is, in some way, entangled in a dispute of fundamentals.

I mean, there was literally an hours long argument over nulls and their effect on type systems.

There is nothing too stupid to be uttered here.
>>
File: eg6.png (59 KB, 1150x688) Image search: [Google]
eg6.png
59 KB, 1150x688
for anon

honestly it'd be better to not even hardcode this, but use a map or something
>>
void fill_2d(int **t,int s1,int s2){
int i,j;
for(i=0;i<s1;i++){
for(j=0;i<s2;j++){
t[i][j]=i*s2+j;
}
}
}


why the fuck it throws EXCEPTION_ACCESS_VIOLATION
Thread replies: 255
Thread images: 36

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.