I have to write a C program that simulates a client/server communication process through a FIFO (using mkfifo). The client sends a database command to the server which deciphers and processes it. The server sends a 1 back to the client if the command succeeds and a 0 if it fails. The client then prints these transactions to a log file.
Anyway, I've noticed something wrong with my FIFOs (one is for client to server communication, the other for server to client) when my program fails to exit normally.
For instance, if it seg faults, or if I kill it while running GDB, then on a following would-be successful run, it seems as if there is "junk" left in the FIFO that drastically skews both processes' output. After I remove both "junky" FIFOs from the directory, then the same program remakes them and the output is correct.
I guess the question is: is this typical/supposed to happen? Or are the FIFOs fine and there's just some other error I need to work out?
Private Mod Note
():
Rollback Post to RevisionRollBack
#define ALWAYS SOMETIMES
#define NEVER RARELY
#define ALL MANY
-=GIVE US SOMETHING TO BELIEVE IN=-
I'm nerd enough to link my WoW Armory Though I'll put it in a small font.
Since in C chars are stored as one byte integers (corresponding to ASCII codes), you can perform arithmetic operations on them to change the character itself. Capital A is 65 in ASCII, and lowercase a is 97. Thus, there is a numeric difference of 32 between capital and lowercase.
Hey, I'm having a bit of trouble with math. I can't get my head around the idea of a maximum/minimum value of a quadratic. It talks about completing the square, f(-b/2a) and if a>0 then it is a minimum and if a<0 it is a maximum. Can anyone help me?
A quadratic of the form ax^2 + bx + c is a parabola that either "opens up" or "opens down." For example, 2x^2 + 12x + 17 (a = 2, b = 12, c = 17) would open up.
If a > 0 (positive), then it opens up, looking like "u"
This type of parabola would have a minimum at its vertex, which is the lowest "dip" of the u-shape.
If a < 0 (negative), then it opens down, looking like "n"
This type of parabola would have a maximum also at its vertex, which in this case is the highest point of the n-shape.
-b / 2a will tell you the x-coordinate of the vertex. In the example above, this would be -12/(2*2) = -3. To find the y-coordinate, you simply plug -3 back into the equation to get 2*(-3)^2 + (12 * -3) + 17 = -1. The vertex (and minimum, since a > 0) is (-3, -1).
Private Mod Note
():
Rollback Post to RevisionRollBack
#define ALWAYS SOMETIMES
#define NEVER RARELY
#define ALL MANY
-=GIVE US SOMETHING TO BELIEVE IN=-
I'm nerd enough to link my WoW Armory Though I'll put it in a small font.
This isn't homework in the strictest of senses, but its close enough.
I typically try to build some sort of program that would help me with a class that I am taking. For this instance, I decided to write up a simple program to help me with my discrete probability class. These are the specifics:
The program will read commands in from a file such as the following:
The meaning of such an entry would be to calculate the expected value (Exp():) of the random variable with the following probabilities. In this case, the random variable would be named x and it would represent the value of a die toss.
The second entry would be to calculate the variance (Var():) of the random variable with the following probabilities. In this case, the random variable would again be x, and it would represent the variance of a coin toss.
The problem I am having is with my call to fscanf, and I cannot understand what the issue is:
#define EXP_ARGS 4
.
.
while((fscanf(infile,"P(%c = %f) = %f/%f",&var,&t,&num,&den)==EXP_ARGS)){
exp_val+=((num/den)*t);}
printf("The expected value of %c is %f\n",var,exp_val);
fscanf, like scanf, returns the number of values read, in this case 4. The logic is that when the loop hits the terminating character ";" (or any non-format-fitting line, really, but only I would be using this) then the loop would end. Very simple. Unfortunately, fscanf is not reading any values at all. When running the program through gdb, after the fscanf call, each variable is simply 0.
I had just written a program for class that basically does the same thing, and that one worked fine. The only difference is the format of this fscanf statement, what with the P() = and whatnot rather than simply reading in %c %f %f %f, but that shouldn't make a difference. Unless such a read is not allowed with fscanf, but I haven't been able to find anything saying such.
The entire program is mostly just a skeleton right now, as I was just working on the expected value portion, but this is it in its entirety (the program compiles; any syntax errors are just typos, as I didn't want to fire up some file sharing program to get an actual text file from my school drive to copy and paste):
#define CMD_LINE_ARGS 2
#define INFILE_ARG 1
#define OPP_NAME_SIZE 7
#define EXP_ARGS 4
/* I just started randomly including libraries just in case */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *infile;
char opp[OPP_NAME_SIZE], var;
float t, num, den, exp_val;
/* check command line */
if(argc!=CMD_LINE_ARGS){
fprintf(stderr,"Command line error.\n"); exit(1);}
/* check input file */
if((infile=fopen(argv[INFILE_ARG],"r"))==NULL){
fprintf(stderr,"Error opening file: %s\n",argv[INFILE_ARG]); exit(1);}
/* while there are commands to be read */
while((fscanf(infile,"%s",opp))==1){
/* expected value */
if(strcmp(opp,"Exp():")==0){
while((fscanf(infile,"P(%c = %f) = %f/%f",&var,&t,&num,&den)==EXP_ARGS)){
exp_val+=((num/den)*t);}
printf("The expected value of %c is %f\n",var,exp_val);}
/* variance */
else if(strcmp(opp,"Var():")==0){}
/* standard deviation */
else if(strcmp(opp,"Sig():")==0){}
/* covariance */
else if(strcmp(opp,"Cov():")==0){}
/* correlation */
else if(strcmp(opp,"Cor():")==0){}
else{ fprintf(stderr,"Invalid command.\n"); exit(1);}}
return 0;}
Edit: I found the problem. I didn't give enough space for the commands to include the newline character, which fscanf ignores, and then the second call to fscanf picked up that newline character in the char variable. Because of this, the pre-planned format was offset and nothing was read in right.
I'm having a problem with the strtok function in C. From everything that I have read, if strtok doesn't find any of the specified delimiters, then it returns null. One part of a program I have to write asks us to create a cross reference table for a MAL program. For the following line of code:
i1: .word 20 #First integer
I would have to take note that "i1" is a label, as it is terminated by the ":" character (this is just the syntax of MAL) and keep track of it (and all label names). To do so, I wrote the following C code:
if((tok=strtok(line,":"))!=NULL){
Where tok is a character pointer and line is a character array (of a defined max line size of 81).
It is my understanding that this statement means "if the string 'line' has the character ':' then point tok at the string of characters (the token) preceding that ':'
The problem is that, for whatever reason, strtok never returns NULL. If it doesn't find the delimiter ":" in "line," then tok simply points to "line" itself, rendering the if statement and what I need to use it for useless.
Private Mod Note
():
Rollback Post to RevisionRollBack
#define ALWAYS SOMETIMES
#define NEVER RARELY
#define ALL MANY
-=GIVE US SOMETHING TO BELIEVE IN=-
I'm nerd enough to link my WoW Armory Though I'll put it in a small font.
does anyone know how to find the intgeral for this? cot(x/3)csc^2(x/3)
Since the derivative of cot u = -(csc u)^2, you have both u and u' already.
Since you'll need to take into account the derivative of (x/3) due to the chain rule, you put the factor (1/3) inside the integral, and the factor 3 outside to keep the equation balanced. Since the derivative of cot u is a negative (csc u)^2, you also throw a negative on the inside as well as the outside.
Now you're ready to integrate. You simply raise u, in this case cot(x/3), by one power, and divide by that new exponent. Since you had a -3 hanging out the outside of the integral, the answer is:
(-3 cot(x/3)^2) / 2
Edit: Further, if you differentiate that, you should get the initial integral problem. To check:
1. "Slide" the exponent 2 down as a coefficient, canceling the 2 in the denominator leaving you with -3cot(x/3).
2. The first application of the chain rule nets you a -csc(x/3)^2, leaving you with -3cot(x/3)* -csc(x/3)^3
3. The second application of the chain rule nets you a 1/3. Cancel the 3 with the 1/3 and the two negatives and you get cot(x/3)csc(x/3)^2
Anyone knows how to solve this? I'm just a loser with numbers...
(n - 1)! [(n + 1)! - n!]
2 (x + 1)/4 = 7 (x - 1)/2
Well, I'll start with the second:
- Cancel the 2 in the numerator
(x + 1) / 2 = 7 (x - 1) / 2
- Multiply both sides of the equation by 2 to eliminate the fractions
(x + 1) = 7 (x - 1)
- Multiply the 7 through
x + 1 = 7x - 1
- Solve
2 = 6x
x = 1/3
As for the first problem, I really don't know what the question is asking. There's nothing to solve for, so at best you might be able to reduce it.
! is the factorial operator. In a nutshell:
4! = 4 * 3 * 2 * 1 = 24
Thus:
x! = x * (x-1) * (x-2) * (x-3) * ...
When you look at the terms of the first problem, you can see where some overlap:
C'mon this is like mental math, people! (slight exaggeration)
ln(2401)/ln(7) = log7(2401) = 4
(7^4 = 2401, which is easy enough to guess based on the size of the number)
And to simplify the above rule ln(x)/ln(y) = logy(x)
Edit: It'll make your life easier if you use the [ sup] [ /sup] tags: a7
I do believe I did say he could further simplify the answer.
Personally, unless otherwise specified, I would just leave it with the natural logs. It shows you know how the answer was derived, and if his homework is anything like mine was, the number of problems could be in the hundreds.
I also think it was a mistake to merge these two threads. Math homework is far different from many, if not all other subjects, and it would have been nice if it were to have its own thread.
Private Mod Note
():
Rollback Post to RevisionRollBack
#define ALWAYS SOMETIMES
#define NEVER RARELY
#define ALL MANY
-=GIVE US SOMETHING TO BELIEVE IN=-
I'm nerd enough to link my WoW Armory Though I'll put it in a small font.
Nope, you're right. The division thing doesn't work, but the negative thing does. So I'd go with -ln(2401)/ln(4). (I know that one works)
Well, that property is really just ln(x^y) = yln(x).
You re-write ln(1/2401) to be ln(2401^-1), and the -1 slides down as a coefficient, giving you -ln(2401).
Are there like some property's i'm missing?
ln(x^y) = yln(x)
and
x^-1 = 1/x
Since you're allowed to do practically anything to both sides of an equation, you take the natural log of both sides. Using the above property, the (5n-8) becomes multiplied by the ln(7). From there it's just solving the equation:
I'm trying to put the square root symbol but everywhere i try to copy and paste from it comes out »(
and the 2 is in the nook of the square root.
Do you mean like where the 3 is in ∛ ?
If so, then that's just a normal square root, and the two doesn't have to be (or really never is) shown there.
If that is the case, then the final answer is just:
(10a)^(7/2) * b^3
And if you want to use special characters, you need to go to Start -> All Programs -> Accessories -> System Tools -> Character Map
From there, click on the symbol you want to use, hit select, then hit copy, and then finally paste it into where you're posting. It just takes a long time, and the symbols are small on this forum.
Edit: Actually, is the problem √10a⁷b⁹ or √(10a)⁷b⁹?
If the 10a isn't in parenthesis, then you would have to re-write it as (10)^(1/2) * (a^(7))^(1/2) * (b^9)^(1/2), which would just be 10^(1/2) * a^(7/2) * b^3
It's still kind of dodgy, but you can use the properties used here even if I've interpreted the problem wrong.
I read "2(square root) 10a^7b^9" as "2 times the square root of 10a raised to the 7th times b raised to the ninth"
If this is the case, you re-write it as:
2 * ((10a^7)(b^9))^(1/2) : a square root is just something raised to 1/2
2 * (10a^(7/2)) * (b^3) : a power raised to a power is multiplication, so (a^7)^(1/2) is a^(7 * (1/2)) which is a^(7/2)
= 20a^(7/2) * 2b^3
This one is a bit tougher to write out with just text, but here we go
7^(5n-8) = 1/2401
ln(7^(5n-8)) = ln(1/2401) : take the natural log of both sides
(5n-8)*ln(7) = ln(1/2401) : ln properties say that ln(x^y) =yln(x)
5n-8 = ln(1/2401)/ln(7)
5n = ln(1/2401)/ln(7) - 8
n = (ln(1/2401)/ln(7) - 8)/5 : you can polish this up a bit if you want, but it's a messy problem to begin with, so unless your teacher specifies, you might as well make him/her read a bit more into your answer as punishment for such a needlessly messy problem =P 1/2401? Really?
Private Mod Note
():
Rollback Post to RevisionRollBack
#define ALWAYS SOMETIMES
#define NEVER RARELY
#define ALL MANY
-=GIVE US SOMETHING TO BELIEVE IN=-
I'm nerd enough to link my WoW Armory Though I'll put it in a small font.
For some reason I can't solve this early Calc I problem:
f(x) = 1 / (1 + x)
g(x) = sin(2x)
I need to find f(g(x)) and find it's domain.
f(g(x)) = sin(2x) / (1 + sin(2x)) easy enough
The domain should be all real numbers except whatever breaks the denominator and makes sin(2x) = -1, but since the sin function is periodic, that would be an infinite number of periodic values that are not in the domain.
I can't remember how to do these for the life of me. =(
Private Mod Note
():
Rollback Post to RevisionRollBack
#define ALWAYS SOMETIMES
#define NEVER RARELY
#define ALL MANY
-=GIVE US SOMETHING TO BELIEVE IN=-
I'm nerd enough to link my WoW Armory Though I'll put it in a small font.
I have to write a C program that simulates a client/server communication process through a FIFO (using mkfifo). The client sends a database command to the server which deciphers and processes it. The server sends a 1 back to the client if the command succeeds and a 0 if it fails. The client then prints these transactions to a log file.
Anyway, I've noticed something wrong with my FIFOs (one is for client to server communication, the other for server to client) when my program fails to exit normally.
For instance, if it seg faults, or if I kill it while running GDB, then on a following would-be successful run, it seems as if there is "junk" left in the FIFO that drastically skews both processes' output. After I remove both "junky" FIFOs from the directory, then the same program remakes them and the output is correct.
I guess the question is: is this typical/supposed to happen? Or are the FIFOs fine and there's just some other error I need to work out?
Though I'll put it in a small font.
Please stop hijacking my reply box.
int i = 0
char string[] = "This is a test."
while(string[i])
{
string[i] = toupper(string[i]);
i++;
}
This should work.
Edit: If toupper still doesn't work, you could always just make your own:
char my_toupper(char c)
{
if(c >= 97 && c<= 122)
return (c - 32);
else return c;
}
Since in C chars are stored as one byte integers (corresponding to ASCII codes), you can perform arithmetic operations on them to change the character itself. Capital A is 65 in ASCII, and lowercase a is 97. Thus, there is a numeric difference of 32 between capital and lowercase.
Though I'll put it in a small font.
Please stop hijacking my reply box.
A quadratic of the form ax^2 + bx + c is a parabola that either "opens up" or "opens down." For example, 2x^2 + 12x + 17 (a = 2, b = 12, c = 17) would open up.
If a > 0 (positive), then it opens up, looking like "u"
This type of parabola would have a minimum at its vertex, which is the lowest "dip" of the u-shape.
If a < 0 (negative), then it opens down, looking like "n"
This type of parabola would have a maximum also at its vertex, which in this case is the highest point of the n-shape.
-b / 2a will tell you the x-coordinate of the vertex. In the example above, this would be -12/(2*2) = -3. To find the y-coordinate, you simply plug -3 back into the equation to get 2*(-3)^2 + (12 * -3) + 17 = -1. The vertex (and minimum, since a > 0) is (-3, -1).
Though I'll put it in a small font.
Please stop hijacking my reply box.
Though I'll put it in a small font.
Please stop hijacking my reply box.
I typically try to build some sort of program that would help me with a class that I am taking. For this instance, I decided to write up a simple program to help me with my discrete probability class. These are the specifics:
The program will read commands in from a file such as the following:
The meaning of such an entry would be to calculate the expected value (Exp():) of the random variable with the following probabilities. In this case, the random variable would be named x and it would represent the value of a die toss.
The second entry would be to calculate the variance (Var():) of the random variable with the following probabilities. In this case, the random variable would again be x, and it would represent the variance of a coin toss.
The problem I am having is with my call to fscanf, and I cannot understand what the issue is:
fscanf, like scanf, returns the number of values read, in this case 4. The logic is that when the loop hits the terminating character ";" (or any non-format-fitting line, really, but only I would be using this) then the loop would end. Very simple. Unfortunately, fscanf is not reading any values at all. When running the program through gdb, after the fscanf call, each variable is simply 0.
I had just written a program for class that basically does the same thing, and that one worked fine. The only difference is the format of this fscanf statement, what with the P() = and whatnot rather than simply reading in %c %f %f %f, but that shouldn't make a difference. Unless such a read is not allowed with fscanf, but I haven't been able to find anything saying such.
The entire program is mostly just a skeleton right now, as I was just working on the expected value portion, but this is it in its entirety (the program compiles; any syntax errors are just typos, as I didn't want to fire up some file sharing program to get an actual text file from my school drive to copy and paste):
Edit: I found the problem. I didn't give enough space for the commands to include the newline character, which fscanf ignores, and then the second call to fscanf picked up that newline character in the char variable. Because of this, the pre-planned format was offset and nothing was read in right.
Though I'll put it in a small font.
Please stop hijacking my reply box.
I would have to take note that "i1" is a label, as it is terminated by the ":" character (this is just the syntax of MAL) and keep track of it (and all label names). To do so, I wrote the following C code:
Where tok is a character pointer and line is a character array (of a defined max line size of 81).
It is my understanding that this statement means "if the string 'line' has the character ':' then point tok at the string of characters (the token) preceding that ':'
The problem is that, for whatever reason, strtok never returns NULL. If it doesn't find the delimiter ":" in "line," then tok simply points to "line" itself, rendering the if statement and what I need to use it for useless.
Though I'll put it in a small font.
Please stop hijacking my reply box.
Since the derivative of cot u = -(csc u)^2, you have both u and u' already.
Since you'll need to take into account the derivative of (x/3) due to the chain rule, you put the factor (1/3) inside the integral, and the factor 3 outside to keep the equation balanced. Since the derivative of cot u is a negative (csc u)^2, you also throw a negative on the inside as well as the outside.
Now you're ready to integrate. You simply raise u, in this case cot(x/3), by one power, and divide by that new exponent. Since you had a -3 hanging out the outside of the integral, the answer is:
(-3 cot(x/3)^2) / 2
Edit: Further, if you differentiate that, you should get the initial integral problem. To check:
1. "Slide" the exponent 2 down as a coefficient, canceling the 2 in the denominator leaving you with -3cot(x/3).
2. The first application of the chain rule nets you a -csc(x/3)^2, leaving you with -3cot(x/3)* -csc(x/3)^3
3. The second application of the chain rule nets you a 1/3. Cancel the 3 with the 1/3 and the two negatives and you get cot(x/3)csc(x/3)^2
Though I'll put it in a small font.
Please stop hijacking my reply box.
Well, I'll start with the second:
- Cancel the 2 in the numerator
(x + 1) / 2 = 7 (x - 1) / 2
- Multiply both sides of the equation by 2 to eliminate the fractions
(x + 1) = 7 (x - 1)
- Multiply the 7 through
x + 1 = 7x - 1
- Solve
2 = 6x
x = 1/3
As for the first problem, I really don't know what the question is asking. There's nothing to solve for, so at best you might be able to reduce it.
! is the factorial operator. In a nutshell:
4! = 4 * 3 * 2 * 1 = 24
Thus:
x! = x * (x-1) * (x-2) * (x-3) * ...
When you look at the terms of the first problem, you can see where some overlap:
(n - 1)! = (n - 1) * (n - 2) * (n - 3) * ...
(n + 1)! = (n + 1) * (n) * (n - 1) * (n - 2) * (n - 3) * ...
n! = (n) * (n - 1) * (n - 2) * (n - 3) * ...
So, in essence, you can look at:
(n + 1)! = (n + 1) * (n) * (n - 1)!
n! = (n) * (n - 1)!
If you multiply the (n - 1)! through, you get:
[(n + 1) * (n) * ((n - 1)!)^2] - [(n) * ((n - 1)!)^2]
Unless I am missing something, that's the only thing I can see being done. I was never very good with factorials, though.
Though I'll put it in a small font.
Please stop hijacking my reply box.
Personally, unless otherwise specified, I would just leave it with the natural logs. It shows you know how the answer was derived, and if his homework is anything like mine was, the number of problems could be in the hundreds.
I also think it was a mistake to merge these two threads. Math homework is far different from many, if not all other subjects, and it would have been nice if it were to have its own thread.
Though I'll put it in a small font.
Please stop hijacking my reply box.
Well, that property is really just ln(x^y) = yln(x).
You re-write ln(1/2401) to be ln(2401^-1), and the -1 slides down as a coefficient, giving you -ln(2401).
ln(x^y) = yln(x)
and
x^-1 = 1/x
Since you're allowed to do practically anything to both sides of an equation, you take the natural log of both sides. Using the above property, the (5n-8) becomes multiplied by the ln(7). From there it's just solving the equation:
(5n-8)*ln(7) = ln(1/2401)
(5n-8)*ln(7) = -ln(2401)
5n-8 = -ln(2401)/ln(7)
5n = -ln(2401)/ln(7) + 8
n = (8 - ln(2401)/ln(7) ) /5
I don't know what answer they want here.
The arithmetic mean is the sum of the sequence divided by the number of elements in the sequence. At best, I can say (95 + ? + ? + ? + 155) / 5
Edit: well, (250 + ? + ? + ?) / 5, but it was easier to see if I left out the addition
Doublepost Merged and Warned
Though I'll put it in a small font.
Please stop hijacking my reply box.
I don't know of any such property.
calculator says:
ln(1/2401) / ln(7) = -4
-ln(343) = -5.83773045
Though I'll put it in a small font.
Please stop hijacking my reply box.
Do you mean like where the 3 is in ∛ ?
If so, then that's just a normal square root, and the two doesn't have to be (or really never is) shown there.
If that is the case, then the final answer is just:
(10a)^(7/2) * b^3
And if you want to use special characters, you need to go to Start -> All Programs -> Accessories -> System Tools -> Character Map
From there, click on the symbol you want to use, hit select, then hit copy, and then finally paste it into where you're posting. It just takes a long time, and the symbols are small on this forum.
Edit: Actually, is the problem √10a⁷b⁹ or √(10a)⁷b⁹?
If the 10a isn't in parenthesis, then you would have to re-write it as (10)^(1/2) * (a^(7))^(1/2) * (b^9)^(1/2), which would just be 10^(1/2) * a^(7/2) * b^3
Though I'll put it in a small font.
Please stop hijacking my reply box.
I read "2(square root) 10a^7b^9" as "2 times the square root of 10a raised to the 7th times b raised to the ninth"
If this is the case, you re-write it as:
2 * ((10a^7)(b^9))^(1/2) : a square root is just something raised to 1/2
2 * (10a^(7/2)) * (b^3) : a power raised to a power is multiplication, so (a^7)^(1/2) is a^(7 * (1/2)) which is a^(7/2)
= 20a^(7/2) * 2b^3
This one is a bit tougher to write out with just text, but here we go
7^(5n-8) = 1/2401
ln(7^(5n-8)) = ln(1/2401) : take the natural log of both sides
(5n-8)*ln(7) = ln(1/2401) : ln properties say that ln(x^y) =yln(x)
5n-8 = ln(1/2401)/ln(7)
5n = ln(1/2401)/ln(7) - 8
n = (ln(1/2401)/ln(7) - 8)/5 : you can polish this up a bit if you want, but it's a messy problem to begin with, so unless your teacher specifies, you might as well make him/her read a bit more into your answer as punishment for such a needlessly messy problem =P 1/2401? Really?
Though I'll put it in a small font.
Please stop hijacking my reply box.
f(x) = 1 / (1 + x)
g(x) = sin(2x)
I need to find f(g(x)) and find it's domain.
f(g(x)) = sin(2x) / (1 + sin(2x)) easy enough
The domain should be all real numbers except whatever breaks the denominator and makes sin(2x) = -1, but since the sin function is periodic, that would be an infinite number of periodic values that are not in the domain.
I can't remember how to do these for the life of me. =(
Though I'll put it in a small font.
Please stop hijacking my reply box.
(2x - 3)(x+1) = 2x^2 - x - 3
(x + 4)(x - 2) = x^2 +2x - 8
Since the powers of the numerator and denominator are the same, the horizontal asymptote is simply the quotient of their coefficients, or y = 2.
I think....
Though I'll put it in a small font.
Please stop hijacking my reply box.