|
thank you for the advice and patience, its working now
|
# ? May 8, 2008 23:39 |
|
|
# ? Jun 8, 2024 22:51 |
|
nvm
Soldat fucked around with this message at 03:16 on May 9, 2008 |
# ? May 9, 2008 01:44 |
|
if (c == ' ') wouldn't work?
|
# ? May 9, 2008 02:07 |
|
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.
|
# ? May 9, 2008 02:30 |
|
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 |
# ? May 9, 2008 02:39 |
|
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 |
# ? May 9, 2008 03:02 |
|
double
|
# ? May 9, 2008 03:03 |
|
Question omitted, got it.
|
# ? May 9, 2008 03:15 |
|
edit: whatever.
|
# ? May 9, 2008 03:26 |
|
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:
|
# ? May 9, 2008 03:33 |
|
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.
|
# ? May 9, 2008 03:43 |
|
crazypenguin posted:strcmp returns 0 when the strings are the same. 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:
Mikey-San fucked around with this message at 22:21 on May 9, 2008 |
# ? May 9, 2008 22:17 |
|
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: 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.
|
# ? May 9, 2008 22:22 |
|
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.
|
# ? May 10, 2008 19:34 |
|
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.
|
# ? May 11, 2008 09:37 |
|
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. 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.
|
# ? May 11, 2008 09:43 |
|
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! vanjalolz fucked around with this message at 09:58 on May 11, 2008 |
# ? May 11, 2008 09:50 |
|
oops
|
# ? May 11, 2008 09:57 |
|
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: code:
Standish fucked around with this message at 10:24 on May 11, 2008 |
# ? May 11, 2008 10:16 |
|
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 will sem_wait decrement the value of the semaphore? edit: What's this? s |= (1<<signo);
|
# ? May 11, 2008 10:41 |
|
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:
|
# ? May 11, 2008 11:28 |
|
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
|
# ? May 11, 2008 11:47 |
|
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. 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:
Thanks in advance.
|
# ? May 12, 2008 02:32 |
|
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.
|
# ? May 12, 2008 03:17 |
|
Looks like numMatches is not getting reset properly either. So it just adds the match onto all the matches from all previous trials.
|
# ? May 12, 2008 03:18 |
|
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
|
# ? May 12, 2008 03:23 |
|
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.
|
# ? May 12, 2008 03:23 |
|
Sarah Sherman posted:Birthday Paradox
|
# ? May 12, 2008 04:08 |
|
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. code:
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!
|
# ? May 12, 2008 08:46 |
|
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: 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.
|
# ? May 12, 2008 09:08 |
|
Ah I see! That makes sense, and now it seems that my program is working correctly. Thanks a lot for the help guys.
|
# ? May 12, 2008 09:15 |
|
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.
|
# ? May 12, 2008 16:58 |
|
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 |
# ? May 12, 2008 17:03 |
|
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 |
# ? May 12, 2008 17:09 |
|
It's also usually considered bad form to use all caps for anything that isn't a constant. Likes like:code:
|
# ? May 12, 2008 17:25 |
|
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.
|
# ? May 12, 2008 17:57 |
|
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 |
# ? May 12, 2008 18:11 |
|
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.
|
# ? May 12, 2008 19:05 |
|
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.
|
# ? May 12, 2008 19:10 |
|
|
# ? Jun 8, 2024 22:51 |
|
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!"
|
# ? May 12, 2008 19:14 |