C/C++ >> Looping question
Posted by Mintegra on 19:06:00 02-04-2002
I have an array, int deck[52]. I want to make the values:

deck[0] = 1;
deck[1] = 1;
deck[2] = 1;
deck[3] = 1;
deck[4] = 2;
deck[5] = 2;
deck[6] = 2;
deck[7] = 2;
deck[8] = 3;

And so on, every four increment by one. I have tried several times and cannot get the idea in my head. Can someone show me the proper way to do this?
Posted by Govtcheez on 19:23:00 02-04-2002
Just banging this out quick, but...

The deck[0] part's cuz I can't recall how smth%0 works...

int i,j;
i=1;
j=1;
deck[0] = j;
for(i;i

int i;

for(i = 0; i < 52; i++) {
deck[i] = i / 4 + 1;
}

The + 1 is because you wanted it to start with 1, not 0
Posted by Flikm on 21:21:00 02-04-2002
can you do something like:

array = {1,1,1,1,2,2,2,2,3};

? i think thats how you do it. thats how it works in perl at least
Posted by KaGez on 08:49:00 02-05-2002
Flikm: and how do you fill it?

I think Govt's solution is the best and simple one
[addsig]
Posted by robost86 on 09:34:00 02-05-2002
Better solution:

int i,j=0;

for(i=0;i[ This Message was edited by: robost86 on 2002-02-05 09:37 ]
Posted by Govtcheez on 12:56:00 02-05-2002
--> I think Govt's solution is the best and simple one

Hehe - good enough for coder of the month, then?
Posted by Peter on 15:00:00 02-05-2002
muhahaha... =P
Posted by Mintegra on 21:46:00 02-05-2002
Thanks, both of you
Posted by robost86 on 13:32:00 02-10-2002
Govtcheez: but my solution is faster
Posted by robost86 on 13:36:00 02-10-2002
btw, his solution is _not_ the best, it's not even good in fact. Everyone using division/modulo in a loop like that should read his C book again
Posted by KaGez on 14:02:00 02-10-2002
robert: we don't use stone-aged PCs like you anymore

[addsig]
Posted by robost86 on 14:30:00 02-10-2002
That's good for you, because "What your brain can't do, you PC will have to do"
Posted by robost86 on 14:32:00 02-10-2002
A Pentium 200MHz with MMX (tm) technology is not from the stone age, by the way.
Posted by KaGez on 16:13:00 02-10-2002
hey, stop posting 2 posts in a line!
ok, it might be not from the stone-age, but it's old enough to care about performance, yes
I love to care about performance tho, it's challenging, and also funny. No, I mean it. Guess why I'm progging OpenGL stuff... you always need maximum performance for the rendering, so you should keep the main code as fast as possible
I really love that =D
[addsig]
Posted by MoX on 10:38:00 02-12-2002
AFAIK loops are the most perfomance-influencing (is there a better way to put it?) parts of the code. Making them PERFECT is therefor essential for every program and should be done even in small progs, no matter how many TeraHertz your box has...
Posted by robost86 on 17:43:00 02-12-2002
Yes, of course the most used code in a program (i.e. inside loops) is the most important to improve. If you code your loops like some people in this thread have, your program will get a bit slow
Posted by Govtcheez on 17:51:00 02-12-2002
I'm not gonna deny that maybe yours is better, robost, but why is it? What's so wrong with using mod in a loop?
Posted by fsvara on 18:43:00 02-12-2002
robert said it's too slow when i asked him on irc...
Posted by Govtcheez on 18:47:00 02-12-2002
hehehe - I can't believe a question this little's generating so much discussion...
Posted by MoX on 20:12:00 02-12-2002
Hey, I even thought this little question interesting enough to start coding an own solution instantly...

More problems like this please
Posted by IbYdI on 20:39:00 02-12-2002
ill agree with mox and robert.
and give you an example.
few months ago i coded a catalogue programm that manages 20.000+ mp3 ...
now when performing search my first algo blocked the computer when you give him only two letters, and for artis name (eg. Deep Purple) it took round 10 sec to display.
after 15 min revision of the algo it 1. doesn't block 2.takes 1-2 secs for the deep purple search
Posted by justin on 03:24:00 02-19-2002
mod is actually int(x / y) * y
yes, that is two operations, therefore it takes 2x as long (assuming * and / take the same amount of time, not always, if I remeber correctly), so / is faster...
Posted by Govtcheez on 15:04:00 02-19-2002
--> mod is actually int(x / y) * y

You sure?

I jsut plugged in 5%3 into that equation on a compiler and got 3...
Posted by IbYdI on 15:19:00 02-19-2002
...hmmm
modulus?
put short way , it gives the reminder of a division.
Posted by Yjo on 18:38:00 04-06-2002
Quote:
The deck[0] part's cuz I can't recall how smth%0 works...

0 mod *smth* is always 0
Posted by KaGez on 03:35:00 04-07-2002
almost all mathematical opreations you do with 0 are 0 in the end ...
[addsig]
Posted by Yjo on 12:25:00 04-07-2002
0+1!=0
Posted by KaGez on 11:03:00 04-08-2002
heh...
_almost_ all .... did you see it?
[addsig]
Posted by fsvara on 19:37:00 04-08-2002
i bet i could come up with an infinite number of operations involving zero that do not equal zero!! haha, i love showing off my m4d maths sk1llz, lol
Posted by Yjo on 00:37:00 04-09-2002
actually, multiplication, and division operations (LHS only) are the only ones I can think of.

apart from that, there're:
division (RHS)
addition
subtraction
indices+roots (RHS)
factorials
which don't evaluate to 0