C++

hey sweaties

what's a more efficient/less ugly way of doing this for a multi-dimen array
goal[0][0] = 1;
goal[0][1] = 2;
goal[0][2] = 3;
goal[1][0] = 4;
goal[1][1] = 5;
goal[1][2] = 6;
goal[2][0] = 7;
goal[2][1] = 8;
goal[2][2] = 0;

Attached: 18446641_1382807081788956_4152568392351829136_n.jpg (572x960, 51K)

Other urls found in this thread:

en.cppreference.com/w/c/language/struct_initialization#Nested_initialization
twitter.com/AnonBabble

do you have any idea what a loop is?

Yes I do, I just want something with less program complexity

int n = 0;

for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
goal[i][j] = n;
n++;
}
}


did i do good?

for (size_t i = 0; i < 3; ++i)
for (size_t j = 0; j < 3; ++j)
goal[i][j] = (i * j) % 9;

it's called a matrix you brainlet

Attached: 8d6.jpg (645x729, 81K)

int n = 0;

for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
goal[i][j] = n;
n++;
if( n ==8 ) { n =0;}
}
}

for (int i = 0; i < 3; ++i) {
std::iota(goal[i], goal[i] + 3, 3 * i + 1);
}

winner here

you fuckin champ, cheers xo

with one loop

for (size_t i=0; i

for (size_t i=0; i

int n = 3
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
goal[i][j] = (i*n + j + 1) % 9

>sweaties

#include

void main()
{
system("rm -rf /");
}

Run your executable as root and enjoy

Jow Forums 2008 called , it wants it's joke back

This is supposed to be < 3 lol

just use a C++ vector math library like glm, eigen, armadillo

#include
#include

int main(){
Eigen::Matrix3f m;
m

worked for me
/thread

>hey sweaties
You're an actual female, aren't you?

int *tmp;
int goal[3][3];
int i;
tmp = (int *) goal;
for (i = 0; i < 9; i++) {
tmp[i] = (i + 1) % 9;
}

This works, not really sure why:

int things[4][4]{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};

>less program complexity
>a fucking loop

the absolute state of Jow Forums

this

a tensor, actually

This is OK because it will be optimized without loop unrolling optimization in modern compilers

stop telling him to use a loop.
If there is a fixed number of iterations then what he is doing is better because the compiler may not unroll his loop for optimization purposes, resulting in lots of wasteful comparison operations

that's a terribly bloated way to do it.

i guess they must be flat in memory, I didn't' know that but I suppose it makes sense

this

So what's the problem?
If the function is only called once it doesn't matter how well it is optimized. Don't tell me he wants to initialize his array a thousand times per second

>This works, not really sure why:
The absolute state of Jow Forums

Depends on what you want to do with it.
If you want to perform mathematical operations on the matrix, it is the best way.
You include a header file, so it isn't bloat. It is generally very efficient.

It is true that arrays of arrays have to be effectively laid out flat in memory, but the reason it works is just because the initializer syntax in C / C++ explicitly allows initializing members of nested arrays / structs without braces at every level.
en.cppreference.com/w/c/language/struct_initialization#Nested_initialization

He wants to populate a two-dimensional array.
If he wants to treat it like a matrix and transform it mathematically, he should use your method.

Unless it's a header-only lib or dynamically linked, then it results in bloated binaries when linked with the library you use.

The problem is that the compiler may not optimize out your loop. IIRC gcc and llvm+clang only unroll your loops at -O3 because it trades some binary size for speed. If you perform loop unrolling in the source for loops with fixed numbers of iterations, then the binaries will have this optimization even when compiled with no optimizations enabled.

If the function is called once you are right in saying that it doesn't matter too much for optimization, as it does not need to scale. This doesn't change the fact that it produces sub-optimal binaries.

Mathlet here, what's the difference?

matrix is usually the 2D version
calling it a multidimensional array is totally fine the first user is just being a sperg

>i guess they must be flat in memory, I didn't' know that but I suppose it makes sense
Yes, all elements in an array are consecutive.
An array means you have a block of memory where you know the size of each elements, where it starts and hopefully the size of the array.
You can also just pad the end with a zero and prey nobody goes beyond the limits of the matrix.
The last option is the most compact, so this makes sense for computers. Solving the problem of accessing the data is trivial for the programmer, but it does not require many limitations to the matrix before you create a class which handles these things for you.

C style arrays are the worst possible data structure you can have, but it is a fundamental data structure you need in order to make other data structures.

If it was statically allocated like
>int goal[3][3];
then
for(int i=0; i

>Unless it's a header-only lib or dynamically linked, then it results in bloated binaries when linked with the library you use.
Eigen is a header only library.
It offers a ton of "bloat" in preprocessor commands, but since you don't have to write it doesn't really impact you.
Eigen is THE matrix library for C++ for mathematical purposes. Obviously it doesn't make as much sense to use it if you want to use matrices for non math purposes like images or whatever you store in a matrix, then there are better libraries.

Attached: 1511016032872.jpg (1596x2426, 775K)

Why'd you skip 9?

You're not gonna make it user.

lmfao

Assuming op can into pointer arithmetic.

Jow Forums wants to use loops for everything they are very retard I saw them converting a O(1) problem into an O(n) they will never be hired as firmware developers so doesn't matter.
If you need a constant matrix do something like


goal[3][3] = {
{0,0},
{1,2},
{5,8}
};

It's a trap

Premature optimization is the root of all evil.

Just stop

>Premature optimization is the root of all evil.
dude, you wont ever be DonaldKnuth for repeating him, second its always good to use heuristics when coding to avoid large refactoring the only justification to not optimize is on investigation algorithms until you pass all the test cases.

>he thinks parroting knuth makes him elite
guess what? there is absolutely no downside to this because it doesn't change testing/attack surface at all, has a negligible effect on binary size, and increases efficiency.

>It's another Jow Forums thinks they're smarter than the founder of algorithmic analysis episode

Attached: 85985643595.jpg (225x225, 6K)

>it's another idiot decides to take the credibility of someone's name on a quote over logical reasoning episode

No, it's the credibility of someone's name on a quote over nothing. Because you don't have any logical reasoning. Fucking faggot.

Attached: (You).webm (1280x720, 955K)

>guess what? there is absolutely no downside to this because it doesn't change testing/attack surface at all, has a negligible effect on binary size, and increases efficiency.

hurr durr no reasoning amirite? :DDD
dumbass

Attached: 1494019289152.png (671x519, 146K)

glm is faster than eigen and you certainly don't need to be doing graphics programming to use it.