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
Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
thank you for the advice and patience, its working now

Adbot
ADBOT LOVES YOU

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
nvm

Soldat fucked around with this message at 03:16 on May 9, 2008

Professor Science
Mar 8, 2006
diplodocus + mortarboard = party
if (c == ' ') wouldn't work?

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
I edited the code to show what I have now, and no it still doesn't work. something like (55 5) still comes back true.

TheSleeper
Feb 20, 2003
Uh, do you want one of us to just do the whole project for you?

Do a mental trace of that loop with a space in different parts of the input and see what happens and when it doesn't work right.

Edit: And for the love of God get acquainted with your debugger. Even if you somehow can't work it out by hand, stepping through with a debugger once will make it REALLY obvious what's wrong.

Edit2: Also, why are you comparing to ASCII values and using 3 different bools and what do their names mean?

TheSleeper fucked around with this message at 02:49 on May 9, 2008

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
Look man, I'm entry level. I didn't think I was asking anyone to do the whole project for me, I was just asking for some help on functions that are written mostly correct, but have small errors. I'm sure I'll get acquainted with the debugger when I am TAUGHT to do so. I probably don't have the time to figure out how to use GNU (as easy as it may be) and even if I could, we're not supposed to use any debuggers for this project (due to it being relatively basic, but I'm still having problems, which is why I'm posting here.)

That being said, I appreciate any help I can get from these threads, as other people have helped prod me along. I'm comparing ascii values to make sure the first input char is a (, and then the following are numbers. In the first case I'm checking the input in the form of '(0-9 value)', and in the second the number can be more than one digit. At this point the names are arbitrary, I'm changing them later. Still returning true when input is something like (5 5).

Soldat fucked around with this message at 03:09 on May 9, 2008

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
double

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
Question omitted, got it.

TheSleeper
Feb 20, 2003
edit: whatever.

Paniolo
Oct 9, 2007

Heads will roll.
Soldat there are so many things wrong with that code you posted I'm not sure where to begin. You should spend a bit more time thinking about the problem you're trying to solve instead of just diving in and writing code. Since you're new at C++ maybe try writing out a solution in pseudocode or another language you know better and then translating it. Also, learn to use the loving debugger, it is NOT optional, it is NOT "cheating", and the thirty minutes you spend learning how to step through code will save you years in the long run.

But I wouldn't just give you poo poo and not help you out, so here's the function you need:

code:
#include <regex>

bool nums(const char* str) {
   static std::tr1::regex r("^\([0-9]+\)$");
   return std::tr1::regex_match(str, r);
}

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
Well, we've learned scheme and writing interpreters for it all semester and spent the last week or so in my intro class getting acquainted with c++. I suppose I didn't realize the level of coding that people expect questions to be asked about in this thread. I suppose I can take it to the yahoo! message boards or something. This is about my 8th program or so, if you include the several hello world! type ones that we did. I'm attempting the extra credit parts of a project where we haven't been introduced to classes yet, so thanks anyways for the code, but I can't use that. As I said, I'm a newbie. Thanks to those that were helpful.

Mikey-San
Nov 3, 2005

I'm Edith Head!

crazypenguin posted:

strcmp returns 0 when the strings are the same.

so basically, stick a ! before those strcmps

The strcmp() function isn't "does this match or not"; it's not a yes-or-no operation. Rather, strcmp() is a lexicographical comparison function that determines the ordering of two strings which can be identical, sorted A>B, or A<B. As a result (no pun), the return value of strcmp() is a signed integer that is equal to, greater than, or less than 0. You should perform strcmp() checks like this:

code:
if (strcmp(stringA, stringB) == 0) // or another comparison operator
{
...
}
At least, this is how I understand strcmp() to work. If anyone knows better, please tell me. :)

Mikey-San fucked around with this message at 22:21 on May 9, 2008

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

Mikey-San posted:

The strcmp() function isn't "does this match or not"; it's not a yes-or-no operation. Rather, strcmp() is a lexicographical comparison function that determines the ordering of two strings which can be identical, sorted A>B, or A<B. As a result (no pun), the return value of strcmp() is a signed integer that is equal to, greater than, or less than 0. You should perform strcmp() checks like this:

code:
if (strcmp(stringA, stringB) == 0)
{
...
}
At least, this is how I understand strcmp() to work. If anyone knows better, please tell me. :)

You're correct, but using !strcmp(A,B) as an equality test is a fairly common idiom. It works of course because anything nonzero is true, and strcmp(A,B) evaluates to false (0) if and only if A and B are the same.

It's a non-intuitive and inelegant syntax, but if you've been coding for any length of time you should be familiar with what it means, so there's nothing terribly wrong with using it unless you're trying to teach someone else.

Incoherence
May 22, 2004

POYO AND TEAR

Soldat posted:

Well, we've learned scheme and writing interpreters for it all semester and spent the last week or so in my intro class getting acquainted with c++. I suppose I didn't realize the level of coding that people expect questions to be asked about in this thread. I suppose I can take it to the yahoo! message boards or something. This is about my 8th program or so, if you include the several hello world! type ones that we did. I'm attempting the extra credit parts of a project where we haven't been introduced to classes yet, so thanks anyways for the code, but I can't use that. As I said, I'm a newbie. Thanks to those that were helpful.
For the record, until this post you were giving off the strong impression that you weren't a newbie. The people on this forum are more than capable of tailoring their advice to their perception of your skill level, but not if you aren't clear about what that is.

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
I'm doing an assignment involving signals/semaphores/forks in POSIX/unix and I need a function that will suspend execution until a certain signal is sent. I was under the impression that the function I want is sigpause, sigwait or sigsuspend, but I can't get the fuckers to work.

Am I meant to be using a few functions together? I'm after something that will do all this atomically: enable signal X, sleep until signal X, disable signal X, return.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

vanjalolz posted:

I'm doing an assignment involving signals/semaphores/forks in POSIX/unix and I need a function that will suspend execution until a certain signal is sent. I was under the impression that the function I want is sigpause, sigwait or sigsuspend, but I can't get the fuckers to work.

Am I meant to be using a few functions together? I'm after something that will do all this atomically: enable signal X, sleep until signal X, disable signal X, return.

It's probably simplest to just use pause(2). That will wait for any signal you have a handler for; if you want to wait for a particular signal, just set a variable in that signal's handler to false, and pause in a loop as long as that variable is true.

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
Thats what I was thinking, but the unit is all about being atomic or something, like what happens if i have code like:

enablesignals()
// here.
pause()
disablesignals()

And theres a thread switch just after enable signals. The signal will be handled, then I will pause, and won't awake until the next signal.

edit:
I'm definitely doing something wrong. I think I'm meant to signal with semaphores.

Back to the drawing board! :science:

vanjalolz fucked around with this message at 09:58 on May 11, 2008

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
oops

Standish
May 21, 2001

vanjalolz posted:

Thats what I was thinking, but the unit is all about being atomic or something, like what happens if i have code like:

enablesignals()
// here.
pause()
disablesignals()

And theres a thread switch just after enable signals. The signal will be handled, then I will pause, and won't awake until the next signal.

edit:
I'm definitely doing something wrong. I think I'm meant to signal with semaphores.
Yeah, try doing sem_wait() instead of sleep(), and calling sem_post() in your signal handler (and set a variable as well if you need to know which specific signal it was):

code:
int s;
sem_t sem;
...
enablesignals();
sem_wait(&sem);
disablesignals();

...
void sighandler(int signo)
{
   s |= (1<<signo);
   sem_post(&sem);
}
Note that in general it's not safe to call arbitrary library functions (most notably printf) from within a signal handler, however sem_post plus a few dozen others are guaranteed safe under the POSIX standard. Best practice is to limit your signal handler to poking a semaphore and setting a global variable, then do all the actual signal-processing work in your main program code. Debugging signal-related deadlocks is not fun.

Standish fucked around with this message at 10:24 on May 11, 2008

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
Thank you, I have decided to not use signals and I came here to ask how to inc/dec a semaphore, but you just answered my question :D

will sem_wait decrement the value of the semaphore?

edit:
What's this? s |= (1<<signo);

Paniolo
Oct 9, 2007

Heads will roll.

vanjalolz posted:

What's this? s |= (1<<signo);

Well, if you just wrote "s = signo" and multiple signals were fired before you checked the value of s, you'd miss one of the signals. The above code treats each bit in s as a flag for a signal type. You can check if a signal was raised like this:

code:
if(s & (1<<SIGQUIT))
   // handle a sigquit
if(s & (1<<SIGINT))
   // handle a sigint
// etc.

vanjalolz
Oct 31, 2006

Ha Ha Ha HaHa Ha
Oh okay thanks.

I think my latest problem involves manipulating the semaphores. My little lift program will fill the request buffer, but the lift processes will only run once (they dont stop and wait at the start) and then they wait for ever after their first loop iteration.

http://codepad.org/bxg5QhsC

lmao zebong
Nov 25, 2006

NBA All-Injury First Team
I just have a quick question about this programming assignment I've been working on for the last couple days. I'm still entry level, so bare with me if my error is something glaringly obvious.

I'll quote the assignment from my professor's website:

quote:

The birthday paradox is that there is a surprisingly high probability that two people in the same room happen to share the same birthday. By birthday, we mean the same day of the year (ignoring leap years), but not the exact birthday including the birth year or time of day. Write a program that approximates the probability that two people in the same room have the same birthday, for 2 to 50 people in the room.

The program should use simulation to approximate the answer. Over many trials (say, 10,000), randomly assign birthdays to everyone in the room. Count up the number of times at least two people have the same birthday on a trial, and then divide by the number of trials to get an estimated probability that two people share the same birthday for a given room size.

Hint: Try writing a function that can search through an array for a target value, but only search up to a specified number of people.

And here is my code: http://pastebin.com/m40ca99c4
I'm almost positive that my main function is right, and something is going wrong in SameBirthday.

Here is an example of the output I'm getting:
code:
For 2 people, the probability of two birthdays is about 0.0052
For 3 people, the probability of two birthdays is about 0.0168
For 4 people, the probability of two birthdays is about 0.0496
For 5 people, the probability of two birthdays is about 0.0445
For 6 people, the probability of two birthdays is about 0.0756
For 7 people, the probability of two birthdays is about 0.1148
For 8 people, the probability of two birthdays is about 0.1688
For 9 people, the probability of two birthdays is about 0.1944
For 10 people, the probability of two birthdays is about 0.239
For 11 people, the probability of two birthdays is about 0.2717
For 12 people, the probability of two birthdays is about 0.3336
For 13 people, the probability of two birthdays is about 0.4498
For 14 people, the probability of two birthdays is about 0.4858
For 15 people, the probability of two birthdays is about 0.5385
For 16 people, the probability of two birthdays is about 0.6576
For 17 people, the probability of two birthdays is about 0.8024
For 18 people, the probability of two birthdays is about 0.8226
For 19 people, the probability of two birthdays is about 0.912
For 20 people, the probability of two birthdays is about 0.962
For 21 people, the probability of two birthdays is about 1.1088
For 22 people, the probability of two birthdays is about 1.2232
For 23 people, the probability of two birthdays is about 1.4007
For 24 people, the probability of two birthdays is about 1.4928
For 25 people, the probability of two birthdays is about 1.5575
For 26 people, the probability of two birthdays is about 1.8096
For 27 people, the probability of two birthdays is about 1.8414
For 28 people, the probability of two birthdays is about 2.044
For 29 people, the probability of two birthdays is about 2.0619
For 30 people, the probability of two birthdays is about 2.322
For 31 people, the probability of two birthdays is about 2.4707
For 32 people, the probability of two birthdays is about 2.5824
For 33 people, the probability of two birthdays is about 2.805
For 34 people, the probability of two birthdays is about 2.9954
For 35 people, the probability of two birthdays is about 3.248
For 36 people, the probability of two birthdays is about 3.1032
For 37 people, the probability of two birthdays is about 3.2782
For 38 people, the probability of two birthdays is about 3.6556
For 39 people, the probability of two birthdays is about 3.9741
For 40 people, the probability of two birthdays is about 3.948
For 41 people, the probability of two birthdays is about 4.2148
For 42 people, the probability of two birthdays is about 4.4772
For 43 people, the probability of two birthdays is about 4.6354
For 44 people, the probability of two birthdays is about 5.1172
For 45 people, the probability of two birthdays is about 5.229
For 46 people, the probability of two birthdays is about 5.566
For 47 people, the probability of two birthdays is about 5.5695
For 48 people, the probability of two birthdays is about 5.6208
For 49 people, the probability of two birthdays is about 5.6742
For 50 people, the probability of two birthdays is about 6.235
Now, the probability shouldn't get above one at all. Why is it getting up to around 6? I'm stumped and have been staring at my code for a while trying to figure out what is going wrong.

Thanks in advance.

csammis
Aug 26, 2003

Mental Institution

Sarah Sherman posted:

Now, the probability shouldn't get above one at all. Why is it getting up to around 6?

The problem's in the loops in main(). You're incrementing num_matches multiple times per trial. It only needs to be incremented once per trial, because once two people share the same birthday, the trial is a match and you can stop checking.

haveblue
Aug 15, 2005



Toilet Rascal
Looks like numMatches is not getting reset properly either. So it just adds the match onto all the matches from all previous trials.

csammis
Aug 26, 2003

Mental Institution

HB posted:

Looks like numMatches is not getting reset properly either. So it just adds the match onto all the matches from all previous trials.

That part looks fine (numMatches = 0 in the outermost loop), the formatting's kind of bad though.

edit:

quote:

The if statement on line 72 doesn't have brackets, so only the first statement following it is affected by the if. You've got two lines indented there, so I'm not sure if you meant for both of those lines to be in the if's body.

Seriously, fix the formatting and always use braces :eng101:

ColdPie
Jun 9, 2006

Looks like the posters above fixed you, but I'll mention a couple things I noticed.

On line 44 you're using the value "10000" instead of your constant NUM_TRIALS. Not an error, but could be if you changed the constant and forgot to update that number.

The if statement on line 72 doesn't have brackets, so only the first statement following it is affected by the if. You've got two lines indented there, so I'm not sure if you meant for both of those lines to be in the if's body.

Scaevolus
Apr 16, 2007

Sarah Sherman posted:

Birthday Paradox
Instead of having int birthdays[50], and then doing a linear search to see if a birthday is a repeat, why not have an array int days[365], and increment cells when a day is chosen?

lmao zebong
Nov 25, 2006

NBA All-Injury First Team
Thanks for the help guys. I realized I'm missing a couple braces, which is throwing off the whole thing.

csammis posted:

The problem's in the loops in main(). You're incrementing num_matches multiple times per trial. It only needs to be incremented once per trial, because once two people share the same birthday, the trial is a match and you can stop checking.
I'm not really getting at what you're saying. Am I formatting my loops wrong? I realized I was missing a bracket in the inner loop that calls SameBirthday, and now I have this:

code:
// Run trials to see if people have the same birthday
// reset number of matches
   for (people=2; people<=50; people++)
   {
   numMatches = 0;
  
	for(trial = 0; trial <= NUM_TRIALS; trial++)
	{
	  for(i = 0; i < people; i++) 
	       birthdays[i] = (rand( ) % 365) + 1;
			
	  for(int y = 0; y < people; y++)
	  {
		if(SameBirthday(birthdays, people))
		++numMatches;
	  }
	}

    cout << "For " << people << " people, the probability of two " <<
        "birthdays is about " <<
        static_cast<double>(numMatches) / NUM_TRIALS << endl;
   }
}
Shouldn't that only increase numMatches when SameBirthday returns true? Now my output isn't going above one, it's hovering around 0.1.
Sorry for needing all this help guys. I blame me having all this trouble on final stress and overworking myself on classes that have nothing to do with my major. Hooray General Ed!

ZorbaTHut
May 5, 2005

wake me when the world is saved

Sarah Sherman posted:

I'm not really getting at what you're saying. Am I formatting my loops wrong? I realized I was missing a bracket in the inner loop that calls SameBirthday, and now I have this:

Shouldn't that only increase numMatches when SameBirthday returns true? Now my output isn't going above one, it's hovering around 0.1.
Sorry for needing all this help guys. I blame me having all this trouble on final stress and overworking myself on classes that have nothing to do with my major. Hooray General Ed!

Let's say there's one day per year, and you have 6 people in a room. What's the chance that two of them share the same birthday?

Obviously it's 1, if there's only one day per year then any two people must share the same birthday. However, that loop of yours iterates through people, and increments each time any person shares a birthday with someone else. That code would output 6 - the number of people who share a birthday with someone else - not 1.

You should increment numMatches when SameBirthday returns true, but you should only increment it once per trial.

lmao zebong
Nov 25, 2006

NBA All-Injury First Team
Ah I see! That makes sense, and now it seems that my program is working correctly. Thanks a lot for the help guys.

Nitis
Mar 22, 2003

Amused? I think not.
I'm hoping this is the correct forum to post a question about some C++ code I'm putting together. I am very new to programming, so any help would be appreciated.

I've uploaded the file here. (BTW, if someone knows a good online file uploader, I'd love to hear about it)

I've outlined the question in "/* */" comments.

Essentially, when attempting to call a function, using a set variable, I end up with a "too many arguments" compile error. I am only attempting to call the function with one variable, and have the function return one value.

If possible, would someone review the section of code I have marked, and point out what I am missing?

Thanks in advance.

Avenging Dentist
Oct 1, 2005

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

Nitis posted:

I've uploaded the file here. (BTW, if someone knows a good online file uploader, I'd love to hear about it)

I'm downloading the file (well, waiting to), but you should probably use something like Pastebin to upload code samples.


EDIT: You're declaring your function prototypes wrong. They need the same signatures as the definitions you have below. Right now you're declaring a function that's never defined, and then defining a completely different function.

Avenging Dentist fucked around with this message at 17:07 on May 12, 2008

6174
Dec 4, 2004

Nitis posted:

I've uploaded the file here. (BTW, if someone knows a good online file uploader, I'd love to hear about it)

As Avenging Dentist said, Pastebin works pretty well for code. I've put your file here http://pastebin.com/m70aea19c

edit: To clarify Avenging Dentist's answer, look at lines 48, 81, and 162.

6174 fucked around with this message at 17:11 on May 12, 2008

TheSleeper
Feb 20, 2003
It's also usually considered bad form to use all caps for anything that isn't a constant. Likes like:
code:
BET_AMOUNT = getBetAmount( moneyRemaining )
Will make people cringe.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Oh also, static does not mean what you think it means. In the context of a function, it's a variable that is initialized on the first function call and reused on all subsequent calls. If you had a class (you don't), it could be declared in the class definition to be a "global" variable.

6174
Dec 4, 2004
I've got a problem I'd like to solve that is easily transformed into an instance of a shortest path of a digraph. Being lazy, I'd prefer to use a library to save me some time. So I've been looking at the Boost Graph Library and have a question. The problem is the natural transformation of my problem into a graph puts the weights onto the vertices instead of the edges. The BGL seems to only solve the problem when the weights are on edges. Of course the graph can be transformed to put the weights on the edges (by making the out-edges carry the weight of the vertex), but since the digraph I'm going to apply this to is roughly 3-regular and has about 64000 vertices, that adds about 128000 additional integers to be stored that don't need to be. Is there some simple way to adapt the BGL into this situation or am I just better off writing a more specialized implementation to solve this problem?

edit: I'm retarded and it is only about 6400 vertices, so it isn't as crazy to put weights on all the edges, but I'd still prefer to use the weights on vertices instead.

edit2: I'm not fixated upon BGL and would consider other libraries.

6174 fucked around with this message at 18:47 on May 12, 2008

Nitis
Mar 22, 2003

Amused? I think not.

Avenging Dentist posted:

EDIT: You're declaring your function prototypes wrong. They need the same signatures as the definitions you have below. Right now you're declaring a function that's never defined, and then defining a completely different function.

I'm not sure I entirely understand what you mean.

Does the function prototype need to look exactly like the function call? The spelling for the function is the same, but the parameter in the prototype is empty, does that make a difference?

Reviewing TheSleeper's suggestion, lines 48, 81, and 162 look like this:

48: int getBetAmount(); <- Function prototype
81: betAmount = getBetAmount( moneyRemaining ); <- Function call
162: int getBetAmount( moneyRemaining ) <- Function code

Looking at this, I'm still not sure where I went wrong.

(I also changed the variable name to reflect it's non-constant status)

Or, am I missing the point completely. I've been working with this for a few days, so I could just be missing the trees because of the forest.

6174
Dec 4, 2004

Nitis posted:

Looking at this, I'm still not sure where I went wrong.

The code that defines the function says that the function getBetAmount should take a parameter (line 162). The function prototype does not mention any parameters (line 48). You call the function with a parameter (line 81). These three things must match.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

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

Nitis posted:

Does the function prototype need to look exactly like the function call? The spelling for the function is the same, but the parameter in the prototype is empty, does that make a difference?

Yes. To the compiler, the function's "name" isn't just the name of the function, but the types of all the parameters. In essence you're declaring (prototyping) a function named getBetAmount_void and defining a function named getBetAmount_int. When you call getBetAmount, the int version doesn't exist yet, so it falls back to getBetAmount_void and says, "Hey wait a minute, this function expects zero arguments, but I'm passing in one. Abort!"

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