← Home ← Back to /g/

Thread 106631747

51 posts 12 images /g/
Anonymous No.106631747 [Report] >>106631767 >>106632078 >>106632118 >>106632126 >>106632178 >>106632217 >>106634809 >>106634955 >>106635263 >>106635372
>need to sum sequential array
>memory has 1,2,3,4,5,6,7,8,9,10 in it
>ask programmer
>running sum
>do that, it's 10 ops til I get 55
>ask mathematician
>(n*n+1)/2
>do that, it's 13 ops til I get 55
>neither solutions are correct without dynamic typing, but using it makes real development impossible
Why is programming so shitty?
Anonymous No.106631767 [Report] >>106632178
>>106631747 (OP)
Funny but kys just in case.
Anonymous No.106631797 [Report] >>106631813
wat
why is doing (n*n+1)(2) 13 ops?
what does any of it have to do with dynamic typing?
Anonymous No.106631813 [Report] >>106631822
>>106631797
what size is the array?
Anonymous No.106631822 [Report] >>106631879
>>106631813
It's an array, you already know the size.

If you didn't, how would you even compute the size?
You'd hit a segfault or other shit's memory.

Or are you talking about null-terminated arrays aka C strings?
Anonymous No.106631879 [Report] >>106631932
>>106631822
no no no. That's all the memory you have
Anonymous No.106631932 [Report] >>106631935 >>106637225
>>106631879
Again, how would I count the array?
I need some way to know when to stop incrementing the index/pointer.
Either by knowing the size or by having a terminating symbol.
Anonymous No.106631935 [Report] >>106631947
>>106631932
You seem to think that this is the c memory model for some reason when it's clearly not.
Anonymous No.106631947 [Report] >>106632001
>>106631935
This is not C specific.
I always need to know the size in some way.
Even if I do it in straight assembly.

Maybe you're used to more abstract languages? Those just bake in the size information in the array type.
Anonymous No.106632001 [Report] >>106632016
>>106631947
You don't need to know the size the program can keep running all that matters is the answer is obtained. Whether it goes on forever, hits protected memory or if memory physically stops there is irrelevant to the problem and is a simply and implementation detail of the computer. I suggest you stop asking about it as it's making you look very dumb. Of course neither program checks for null either so why you earlier suggested that makes even less sense.
Anonymous No.106632016 [Report] >>106632039
>>106632001
>You don't need to know the size the program can keep running all that matters is the answer is obtained. Whether it goes on forever, hits protected memory or if memory physically stops there is irrelevant to the problem
How is it irrelevant? If I hit an OOM my computer or process crashes and I can't compute the final answer.

It sounds like you meant to say list instead of array.
Anonymous No.106632039 [Report] >>106632056
>>106632016
You process doesn't crash it is killed by the operating system to prevent instability. Again, an implementation detail, it could just as well hit the end of memory, stop reading the array, and go on to computing with the provided formula. All implementation details irrelevant to the science of programming.
Anonymous No.106632056 [Report] >>106632061
>>106632039
>it could just as well hit the end of memory,
How would I know I hit the end of memory?
That's the whole problem.
Anonymous No.106632061 [Report] >>106632085
>>106632056
I think you're too low iq to understand op
Anonymous No.106632078 [Report] >>106632099 >>106632159
>>106631747 (OP)
If you add 10 things, that's 9 ops you idiot.
How many ops is 1+2?
Anonymous No.106632085 [Report] >>106632159
>>106632061
OP is too low IQ to understand what an array is.

Even if you want to be completely abstract, then an array would be a vector, where again you know the size of it.
What he seems to mean is a list, or more abstract: a sequence or tuple.
Anonymous No.106632099 [Report] >>106632159
>>106632078
2 reads
1 addition
1 store

4 Ops.
Anonymous No.106632118 [Report] >>106632159
>>106631747 (OP)
you are retarded
the mathematical solution is like 5 instructions
- access last element by index - 1
- assignment - 1
- addition - 1
- multiplication - 1
- division - 1
it's 5

if we don't have size beforehand and know the array is statically allocated
another instruction for sizeof (array) / sizeof (array[0])
sizeof (array) - 1
access array[0] - 1
sizeof (array[0]) - 1
division - 1
total 9

the loop is much more
initialize index - 1
initialize total - 1
loop condition - n
reassignment of total in loop - n
addition on reassignment of total in loop - n

if I didn't miss anything it's 3n + 2
if we don't have a size we add what we found in other calculation which is 4
3n + 6

math solution is better
Anonymous No.106632126 [Report] >>106632159 >>106632170 >>106632178
>>106631747 (OP)
Anonymous No.106632149 [Report]
op got this one good
Anonymous No.106632159 [Report] >>106632175 >>106632208
>>106632085
>an array would be a vector
Says who retard?
>>106632078
what?
a+=1
a+=2
...
a+=10
10 ops you are saying it's 9 cause you want to do ?
a+=(1+2)
a+=3
lol retard

>>106632099
>>106632118
Where is this index stored? Might as well just say "move pointer to the block containing 55"
>sizeof
How is that implemented?
>>106632126
Quote the part where my communication was poor or misleading in any way? It's more that you are conflating whatever your pet computer does with a simple question.
Anonymous No.106632170 [Report] >>106632185
>>106632126
Anonymous No.106632175 [Report] >>106632191
>>106632159
>Says who retard?
Everyone?
I don't know why you keep insisting on your wrongness.
Just say "Oh, yeah I meant list, not array" and move on. It's not a big deal.
Anonymous No.106632178 [Report] >>106632218
>>106631747 (OP)
You have to choose to find this funny. I feel like most people understand it as Kid's Looking Touchable the way a southerner refers to a strapping young lad as a kid

>>106632126
I don't even get the hatless guy's joke. "Language" doesn't end in gry

>>106631767
>kys
If you don't apologize I may have to share some touchable looking kids. The great part about doing it when an anti is in the room is destroying their brain with facts and logic while also doing my duty as a Western man by sharing beauty with the world
Anonymous No.106632185 [Report]
>>106632170
nice think there's something called New Zealand dot st so I wipe my ass with the door the mods show me 12 times a day on average
Anonymous No.106632188 [Report]
New anon here.

Am i misunderstanding the question? What do you even want from computers? They're designed, since Turing, to run repetitive sequential instructions on values organized linearly in memory really fast. They don't do clever tricks or common core math to shortcut steps at the bare metal level.
Anonymous No.106632191 [Report] >>106632210
>>106632175
It's an array, the memory containing the indices isn't available to your program. Sorry if that wasn't obvious.
Anonymous No.106632208 [Report] >>106632226
>>106632159
>Quote the part where my communication was poor or misleading in any way?
When you asked for an efficient way to sum an array of numbers letting people assume this is a normal programming question, then started nitpicking about memory usage and disagreeing about what constitutes one operation, making this some kind of weird mathematical problem that you are not clearly defining and therefore cannot be correctly answered
Anonymous No.106632210 [Report]
>>106632191
So tell me what in your mind happens if I go beyond the array size.

Because this matters. If I can just go beyond the array with nothing bad happening, I can simply skip indices (e.g. only look at every odd index, go back one once I overflow)
Anonymous No.106632211 [Report] >>106632219
rewrite the question make it a string of chars and just null terminate it, then ask for the sum. That will keep the c baby ducks happy
Anonymous No.106632217 [Report]
>>106631747 (OP)
If you load your ints into a floating point register, then you don't need dynamic typing at all.
Anonymous No.106632218 [Report] >>106632597
>>106632178
>"Language" doesn't end in gry
the joke is that the hatless guy says there are three words in (the sentence) "the English language"
so the first two are "the" and "English", making "language" the third one
Anonymous No.106632219 [Report] >>106632229 >>106632231 >>106637225
>>106632211
It is unfortunate that you can't talk about computer science without having to bend over backwards framing everything in c terms. One of the many problems with programming.
Anonymous No.106632226 [Report]
>>106632208
are you same guy that didnt realize you were being fucked with when asked why an anon couldnt make a folder called /g/ on linux? you sound like that guy.
Anonymous No.106632229 [Report] >>106632246
>>106632219
Arrays predate C, retard.
Anonymous No.106632231 [Report] >>106632239 >>106632244 >>106632270
>>106632219
What languages let you access indexes past the end of an array?
Anonymous No.106632239 [Report]
>>106632231
Java easily
Anonymous No.106632244 [Report] >>106632281
>>106632231
Sanskrit.
Anonymous No.106632246 [Report]
>>106632229
then replace array with block
Anonymous No.106632270 [Report] >>106632300
>>106632231
As you should have picked up from the question you don't have r/w access to the indices of the array so you can just treat this as a block.
Anonymous No.106632281 [Report]
>>106632244
Anonymous No.106632300 [Report]
>>106632270
So print garbage for fun? Are you going to make angry eyebrows if I ask why?
Anonymous No.106632548 [Report]
Answer me, OP.
Why can't I skip indices with your magic arrays and solve your problem much faster?
Anonymous No.106632597 [Report]
>>106632218
>are three words in (the sentence) "the English language"
>so the first two are "the" and "English", making "language" the third one
No, he says that there's three words in the English language that start with fry

He claims there are 3x with condition a
If he wanted to claim there are 3x without condition a it would be necessary to omit this because these are two different statements. There is no universe where this is a joke
Anonymous No.106634809 [Report]
>>106631747 (OP)
>>(n*n+1)/2
>>do that, it's 13 ops til I get 55
What.
First, it's n*(n+1)/2
Second, this is like 4 CPU operations that the compiler might even further optimize it
You seem to assume that you need to traverse the array. Why?
Anonymous No.106634955 [Report] >>106635092
>>106631747 (OP)
In what scenario would this be a real world problem?
What would be the point of storing an array of sequential integers?

A function that only does addition might be faster when n is very small, yes.
But should you really be spending your time optimizing that?
Just use the mathematical formula and benchmark your program.
If your benchmark shows you spend a lot of time calculating (2*(2+1))/2 then go ahead and optimize.
But then REALLY optimize by using a look-up table for those tiny n values.
Anonymous No.106635092 [Report]
>>106634955
The problem is simplified for the programmer in question to think of the problem itself
Anonymous No.106635263 [Report]
>>106631747 (OP)
>need to sum sequential array
>memory has 1,2,3,4,5,6,7,8,9,10 in it
In arm simd this is just
.data
vector:
.byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.text
ldr r0, =vector
vld1.u8 {s0, s1, s2, s3}, [r0]
vaddv.u8 s0, {s0, s1, s2, s3}
Anonymous No.106635372 [Report]
>>106631747 (OP)
>never lvled up enough to post problems like that on stack overflow saying that it's impossible to do only for autists giving you one solution after another trying to prove you wrong.
ngmi...
Anonymous No.106635701 [Report]
Also you can compute the sum of the array in a single step by representing each integer as a sequence of bytes like peano numbers, e.g. 1 = 1 byte, 2 = 2 bytes, then the sum is just the higher address minus the lower address for the total bytes in the array
one = 0
two = {one, one}
...
array = {one, two, ...}

sum = a[10] - a
Anonymous No.106637225 [Report]
>>106631932
>>106632219
>It is unfortunate that you can't talk about computer science without having to bend over backwards framing everything in c terms. One of the many problems with programming.
C didn't invent arrays. A lot of languages older than C had better arrays before C existed than C has almost 60 years later.
[]INT array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
PROC sum = ([]INT a)INT:
(INT total := 0;
FOR i FROM LWB a TO UPB a DO
total +:= a[i]
OD;
total);
printf(($"The sum is "g(0)l$, sum(array)))