Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
honeymustard
Dec 19, 2008

Shut up cunt.
Just print it, so yeah you're right. I'm stupid and didn't realise that. Thanks ;)

Adbot
ADBOT LOVES YOU

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane
You shouldn't seed the random number generator every turn, only once at the beginning of your program.

Jo
Jan 24, 2005

:allears:
Soiled Meat

PT6A posted:

You shouldn't seed the random number generator every turn, only once at the beginning of your program.

:pseudo: Seeding more makes it more random! :downs:

floWenoL
Oct 23, 2002

hexadecimal posted:

code:
...
               int rand_choice = rand()%1;
...
This will give you a semi random list.

Are you sure?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

floWenoL posted:

Are you sure?
well it's random so there's no way to be sure :pseudo:

hexadecimal
Nov 23, 2008

by Fragmaster

floWenoL posted:

Are you sure?

Oh... lol you got me there. %2 would do what i meant to do. I was just thinking to generate number 0 or 1, and forgot that %1 is meaningless. Sorry guys.

hexadecimal fucked around with this message at 03:28 on Jan 10, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

hexadecimal posted:

Oh... lol you got me there. %2 would do what i meant to do.
I'm curious, why did you suggest this way rather than permuting an ordered list of the pairs? (to be clear: this isn't meant as a jab at his coding, but is there a facet to the problem that makes the push_front/push_back procedure better?)

hexadecimal
Nov 23, 2008

by Fragmaster

Dijkstracula posted:

I'm curious, why did you suggest this way rather than permuting an ordered list of the pairs?

Can you show me how to do this?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

hexadecimal posted:

Can you show me how to do this?

code:
int[] ret;

for i=0..9 do
  ret[i] = i;
end


for i=0..9 do
  int j = rand() mod n;
  int k = rand() mod n;

  pair temp = ret[j];
  ret[j] = ret[k];
  ret[k] = temp;
end
edit: added array generation just to make it clear, and changed a variable name that I used twice.

Dijkstracula fucked around with this message at 03:40 on Jan 10, 2009

floWenoL
Oct 23, 2002

hexadecimal posted:

Can you show me how to do this?

Maybe you'll learn it in grad school!

POKEMAN SAM
Jul 8, 2004

Dijkstracula posted:

code:
int[] ret;

for i=0..9 do
  ret[i] = i;
end


for i=0..9 do
  int j = rand() mod n;
  int k = rand() mod n;

  pair temp = ret[j];
  ret[j] = ret[k];
  ret[k] = temp;
end
edit: added array generation just to make it clear, and changed a variable name that I used twice.

Ugh, this still isn't a properly shuffled array.

hexadecimal
Nov 23, 2008

by Fragmaster

Dijkstracula posted:

code:
int[] ret;

for i=0..k do
  ret[i] = i;
end


for i=0..k do
  int j = rand() mod n;
  int k = rand() mod n;

  pair temp = ret[j];
  ret[k] = ret[j];
  ret[j] = temp;
end
edit: added array generation just to make it clear.

The reason I didn't do this is because to get index in linked list is linear time, and to swap in array is more costly than linked list. So I thought the way I did it would be fastest, even if it doesn't really generate true random permutation.

Perhaps if one had a custom linked list implementation you could store references to all 9 nodes in array, then do your method, and swap pointers in the node references. This would probably be the fastest way?

Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

Ugg boots posted:

Ugh, this still isn't a properly shuffled array.

Can we just elide this discussion by pointing him here: http://en.wikipedia.org/wiki/Knuth_shuffle ?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Ugg boots posted:

Ugh, this still isn't a properly shuffled array.
What's missing? Is n swaps too few for an array of size n?


vvv Kluth Shuffle is linear, so I don't think that's it.

Dijkstracula fucked around with this message at 03:49 on Jan 10, 2009

Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

Dijkstracula posted:

What's missing? Is n swaps too few for an array of size n?

If you're not avoiding swaping the same two numbers twice it is.

Edit: This is kind of out of my rear end, but I seem to remember that if you swap without avoiding duplication you need n log n swaps.

Edit2: Upon further thought I'm pretty sure that's right because it would seem to follow from Fisher-Yates + Coupon Collector

Smackbilly fucked around with this message at 03:49 on Jan 10, 2009

floWenoL
Oct 23, 2002

^^^^ wrong (Edit: actually nevermind, i misinterpreted what you mean by "swap without avoiding duplication")

Dijkstracula posted:

What's missing? Is n swaps too few for an array of size n?

code:
rand() mod n => i + rand() mod n - i
An easy way to see that your method is wrong is that n^n (number of possibilities in your method) isn't evenly divisible by n! (number of shuffles of an array).

floWenoL fucked around with this message at 04:03 on Jan 10, 2009

floWenoL
Oct 23, 2002

hexadecimal posted:

to swap in array is more costly than linked list.

Not true unless the elements are large.

quote:

Perhaps if one had a custom linked list implementation you could store references to all 9 nodes in array, then do your method, and swap pointers in the node references. This would probably be the fastest way?

Nope.

*puts in hexadecimal.txt*

floWenoL fucked around with this message at 03:54 on Jan 10, 2009

Victor
Jun 18, 2004

floWenoL posted:

An easy way to see that your method is wrong is that n^n (number of possibilities in your method) isn't evenly divisible by n! (number of shuffles of an array).
Genius! I suppose this is a standard way of reasoning if you have formal CS or maths training, but I don't. I had seen this idea before, but I think you just locked it in my slow brain.

Victor fucked around with this message at 04:46 on Jan 10, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Victor posted:

Genius! I suppose this is a standard way of reasoning if you have formal CS or maths training, but i don't. I had seen this idea before, but I think you just locked it in my slow brain.
Hell, I do have CS/math training and that simple counting argument didn't occur to me. Thanks, floWenoL.

edit: yep, I'm still embarassed.

Dijkstracula fucked around with this message at 03:56 on Jan 10, 2009

honeymustard
Dec 19, 2008

Shut up cunt.
Fixed all the tic tac toe issues and that's working nicely now. I'm on another small program and have come across an annoying bug:

code:
		int n;
		char value1, value2, value3, value4, value5;
		for (n = 0; n < numberofcards; n++) {
			cout << "\nPlease enter the value of card number " << n+1 << "\n";
			if (n = 0) {
				cin >> value1;
				if (n = 1) {
					cin >> value2;
					if (n = 2) {
						cin >> value3;
						if (n = 3) {
							cin >> value4;
							if (n = 4) {
								cin >> value5;
							}
						}
					}
				}
			}
		}
This loop is supposed to get the value of the 2-5 cards from the user. It seems get stuck in an infinite loop, however. I'm sure it's something small with the branching if statements (which I'm not sure are exactly necessary anyway). Hope that makes sense :)

Staggy
Mar 20, 2008

Said little bitch, you can't fuck with me if you wanted to
These expensive
These is red bottoms
These is bloody shoes


I'm not sure about an infinite loop, but those branching if-statements are definitely screwed.

Example:
code:
if (n = 0) {
       cin >> value 1;
       if (n = 1) {
...
                  }
            }
Firstly, (n = 1). That's saying (please don't let me get this wrong) that n is 1. It's assigning the value 1 to n.
What you need is (n == 1). That use of the == checks whether or not the two sides are equal, rather than assigning one to the other.

Secondly, your for loop will only do anything the first time through. Every if-statement after 0 is contained inside the 0 if-statement - so it'll work the first time, when n == 0, but that's it.

I think you need something like:
code:
if (n == 0) {
        cin >> value1;
            }
if (n == 1) {
        cin >> value2;
            }
and so on. Although, with only one line for each if-statement, you don't even need the brackets.
code:
if (n == 0)
    cin >> value1;
if (n == 1)
    cin >> value2;
Alternatively you could use a Switch-Case.
code:
switch (n){
   case 0:
    cin >> value1;
    break;
   case 1:
    cin >> value2;
    break;
}
I'm guessing you're working on some sort of card game - it looks interesting.

honeymustard
Dec 19, 2008

Shut up cunt.
Ahhh thank you. I always forget to use two = rather than just one. I guess I'm used to using Visual Basic, rather than C++. It's an annoying jump, due to the syntax.

The card game isn't much, it just scores a hand of Blackjack :)

digibawb
Dec 15, 2004
got moo?

Staggy posted:

Alternatively you could use a Switch-Case.
code:
switch (n){
   case 0:
    cin >> value1;
    break;
   case 1:
    cin >> value2;
    break;
}

Yes, let's teach people the for-case paradigm.



You'd be better off writing a function which deals with printing the statement, and fetching the input, then calling it 5 times, either via a loop and an array of ints, or just by calling it 5 times explicitly.

EDIT: or, since it's a relatively small amount of logic, just shoving it inline with the array

digibawb fucked around with this message at 20:57 on Jan 11, 2009

honeymustard
Dec 19, 2008

Shut up cunt.

digibawb posted:

Yes, let's teach people the for-case paradigm.

Tell me more? Our programming lecturer guy seems to encourage switch-cases.

digibawb
Dec 15, 2004
got moo?

honeymustard posted:

Tell me more? Our programming lecturer guy seems to encourage switch-cases.

There's nothing wrong with switch-case, it's awesome.

Doing it inside a loop based on the iterator is just silly however (in general anyway). With a small case like this it might not look all that crazy, but once you go down the road, you get to places like http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx

honeymustard
Dec 19, 2008

Shut up cunt.
Hmm, I think I understand that. What I originally wanted was one line within the loop, like:

cin >> cardvalue[n]

So each time it would increment the variable name itself, but I couldn't figure out how to use that, or if it is even possible.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

honeymustard posted:

Hmm, I think I understand that. What I originally wanted was one line within the loop, like:

cin >> cardvalue[n]

So each time it would increment the variable name itself, but I couldn't figure out how to use that, or if it is even possible.
While you can't increment based on the name of the variable, there's nothing stopping you from having an array of chars and iterating over that.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

honeymustard posted:

Tell me more? Our programming lecturer guy seems to encourage switch-cases.

Yeah, for something like that, you definitely want to use a switch, because with a series of if statements, it needlessly runs every single statement; whereas with a switch, it jumps right into the case derived from the argument, and only runs that case. Just keep in mind that you can only use either ints or chars as an argument in a switch.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Anunnaki posted:

Yeah, for something like that, you definitely want to use a switch, because with a series of if statements, it needlessly runs every single statement; whereas with a switch, it jumps right into the case derived from the argument, and only runs that case. Just keep in mind that you can only use either ints or chars as an argument in a switch.

"Premature optimization is the root of all evil." While switch statements can usually be implemented as jump tables (which are faster), 99% of the time, you won't notice the speed difference anyway. Always, always write code in the clearest way possible first, then optimize later if you need to.

That said, I think switch statements are usually more readable so I use them anyway.

hexadecimal
Nov 23, 2008

by Fragmaster

Avenging Dentist posted:

"Premature optimization is the root of all evil." While switch statements can usually be implemented as jump tables (which are faster), 99% of the time, you won't notice the speed difference anyway. Always, always write code in the clearest way possible first, then optimize later if you need to.

I agree with you, but why are switch statements less clear then if statements? I mean if you follow a relatively sane coding style then switch statement should be about as readable as an alternative if one.

EDIT: Didn't see your edit.

sklnd
Nov 26, 2007

NOT A TRACTOR

honeymustard posted:

Ahhh thank you. I always forget to use two = rather than just one. I guess I'm used to using Visual Basic, rather than C++. It's an annoying jump, due to the syntax.
If you have trouble with == versus = (and I've seen old grizzled coders check in bugs related to that), place the constant portion of the comparison on the left. It might not be exactly how you would describe the logic if you were reading it, but it makes the compiler catch a dumb error.

honeymustard
Dec 19, 2008

Shut up cunt.
Thanks, got that part sorted. One other thing I was wondering, when using a case-switch statement, is it possible to have more than one case on the same line? I thought it'd look like:

case ('2' || '3'):

but that doesn't seem to work.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

honeymustard posted:

Thanks, got that part sorted. One other thing I was wondering, when using a case-switch statement, is it possible to have more than one case on the same line? I thought it'd look like:

case ('2' || '3'):

but that doesn't seem to work.

code:
switch(foo)
{
case '2':
case '3':
    butts();
    break;
}

honeymustard
Dec 19, 2008

Shut up cunt.
Wonderful, works great, cheers :)

floWenoL
Oct 23, 2002

sklnd posted:

If you have trouble with == versus = (and I've seen old grizzled coders check in bugs related to that), place the constant portion of the comparison on the left. It might not be exactly how you would describe the logic if you were reading it, but it makes the compiler catch a dumb error.

That is dumb and horrible. If you have trouble with == versus = and your compiler doesn't tell you you need to turn warnings on (and are probably missing a load of other stuff, too).

Edit:
You really shouldn't be using a for loop and the switch statement.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

floWenoL posted:

That is dumb and horrible. If you have trouble with == versus = and your compiler doesn't tell you you need to turn warnings on (and are probably missing a load of other stuff, too).

Example: http://codepad.org/gIhxXXaN

That Turkey Story
Mar 30, 2003

sklnd posted:

If you have trouble with == versus = (and I've seen old grizzled coders check in bugs related to that), place the constant portion of the comparison on the left. It might not be exactly how you would describe the logic if you were reading it, but it makes the compiler catch a dumb error.
I bring this up every time someone mentions it, but I really don't understand how this is supposed to help -- if your problem is that you are forgetting to use == instead of =, I don't see how putting constants on the left is any easier to remember. In other words, if you are writing your comparison and you have the sense to say to yourself "I'd better put the constant on the left so if I forget to use the == operator it'll produce a compile error," why not at that point in time just make sure to use == instead of =, after all you just remembered to put the constant on left, why not just be sure to use == instead. Also what about times where neither operand is constant, do you find yourself using the wrong operator there? Probably not.

The entire rationale just doesn't make sense. It makes more sense to be aware when you are using the wrong operator rather than to write your code in a way that may be unintuitive to yourself or a reader for little [or in my opinion, no] gain.

That Turkey Story fucked around with this message at 02:27 on Jan 12, 2009

sklnd
Nov 26, 2007

NOT A TRACTOR

That Turkey Story posted:

I bring this up every time someone mentions it, but I really don't understand how this is supposed to help -- if your problem is that you are forgetting to use == instead of =, I don't see how putting constants on the left is any easier to remember. In other words, if you are writing your comparison and you have the sense to say to yourself "I'd better put the constant on the left so if I forget to use the == operator it'll produce a compile error," why not at that point in time just make sure to use == instead of =, after all you just remembered to put the constant on left, why not just be sure to use == instead. Also what about times where neither operand is constant, do you find yourself using the wrong operator there? Probably not.

The entire rationale just doesn't make sense. It makes more sense to be aware when you are using the wrong operator rather than to write your code in a way that may be unintuitive to yourself or a reader for little [or in my opinion, no] gain.

It doesn't help when both values are variables, but instances where doing that is habit would catch bleary-eyed mistakes of just forgetting the second =, or just when a VB person comes along and played in a C-like language.

As pointed out in #cobol, -Wall is probably a better option.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
Hmm, I was bored and thought I'd try to figure out how to make a program that computes every single permutation of letters in the alphabet. After I got it working initially, I realized my math was horribly off. I figured if you moved "A" to the last position, then "B" to the second to last, "C" to the third to last, etc., the math would basically come out to be "25 + 24 + 23 + .. + 1", but realized it's actually multiplied by instead of added by, which makes it "26!", which is freaking huge.

Is it feasible to come up with an algorithm that generates every possible permutation? Because we're talking 4.03 x 10^26 possibilities.

Adbot
ADBOT LOVES YOU

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Anunnaki posted:

Is it feasible to come up with an algorithm that generates every possible permutation? Because we're talking 4.03 x 10^26 possibilities.

That's at least about as hard as incrementing an 89 bit counter through its range, so no, it's not computationally tractable.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply