is it i use the huffman coding to find out the binary number and assign it to the variable?
Eg.
0.3 = 00
0.25 = 01
0.25 = 10
0.2 = 11
Need help here.|||A fixed length code is a kind of channel coding that was used in earlier communication systems. The idea is simple: lets suppose you want to encode an alphabet {A1, A2, A3, ... Ak}.
A fixed length code C is a set of code words {C1,.... Ck} , all of the same number of bits, each of which encodes a specific element of the alphabet. Notice that this last property is essential to be able to decode the original message from an encoded version.
E.g. if the alphabet of messages is {A, B, C, D, E} then 3 bits suffice to represent these five elements, and you can choose the code
A = 000
B = 001
C = 010
D = 011
E = 100
one among the many possibilities of assigning unique 3-bit code words to each message.
Huffman coding is variable length, so by definition a Huffman code cannot be a fixed length code.
Saturday, December 17, 2011
I have a java code problem, can someone HELP?
ok, so i had to do a project of converting binary to decimal numbers, i think i made the right code but the compiler gives me an error
import javax.swing.JOptionPane;
public class BtoD
{
public static void main(String []args)
{
String testNum = JOptionPane.showInputDialog("Enter Bianry Number");
int x = testNum.length();
int[] binary= new int [x];
for(int i=0;i%26lt;x;i++)
{
binary[i]=Integer.parseInt(testNum.鈥?br>
}
int decimalNum=0;
int y=1;
for(int i=x-1;i%26gt;=0; i--);
{
decimalNum=decimalNum+(binary[i]*y)鈥?br>
y=2*y;
}
String output=" your decimal number is" + decimalNum;
JOptionPane.showMessageDialog(null,o鈥?br>
}
}
the error it gives me is: cannot find symbol
symbol : variable i
location: class BtoD
decimalNum=decimalNum+(binary[i]*y)鈥?br>
please someone help?!
|||This is a good one, took me a minute to spot the error. The problem is in this line:
for(int i=x-1;i%26gt;=0; i--);
The semi-colon at the end, ends the line so the code within the curly braces is not part of the if statement so doesn't have access to i.
import javax.swing.JOptionPane;
public class BtoD
{
public static void main(String []args)
{
String testNum = JOptionPane.showInputDialog("Enter Bianry Number");
int x = testNum.length();
int[] binary= new int [x];
for(int i=0;i%26lt;x;i++)
{
binary[i]=Integer.parseInt(testNum.鈥?br>
}
int decimalNum=0;
int y=1;
for(int i=x-1;i%26gt;=0; i--);
{
decimalNum=decimalNum+(binary[i]*y)鈥?br>
y=2*y;
}
String output=" your decimal number is" + decimalNum;
JOptionPane.showMessageDialog(null,o鈥?br>
}
}
the error it gives me is: cannot find symbol
symbol : variable i
location: class BtoD
decimalNum=decimalNum+(binary[i]*y)鈥?br>
please someone help?!
|||This is a good one, took me a minute to spot the error. The problem is in this line:
for(int i=x-1;i%26gt;=0; i--);
The semi-colon at the end, ends the line so the code within the curly braces is not part of the if statement so doesn't have access to i.
C++ inheritance, polymorphism with the overloaded<< operator?
I have base class A and subclasses B, C, and D. A has a variable in that class that B, C, and D all use. A also has another variable called type (a string) that are for B and C. D also has a variable called type (an int) that I'm trying to override polymorphically over the type variable in the base class. I have a binary search tree class that each node points to an A object For testing, I currently have one A object that was instantiated from a D object and hangs on the binary search tree as an A object with variables from D as well. I can't figure out to print the variables from D and A that it has. I can print operator%26lt;%26lt; from the subclass or superclass, but it only will print the variables from the parent class. how can I print all of my variables? Open to moving variables...|||First, member variables aren't polymorphic, functions are. D has int type;, B and C have string type;, whether A has a type or not is irrelevant. What matters is that there has to be a virtual function in A which does something different depending on run-time type of A (whether it's really B or if it's really D)
For example, that function could return printable string representation of the type:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;sstream%26gt;
class A
{
聽聽聽聽 virtual std::string gettype_as_string() const = 0;
public: virtual ~A() {}
聽聽聽聽 friend std::ostream%26amp; operator%26lt;%26lt;(std::ostream%26amp; s, const A%26amp; a)
聽聽聽聽 {
聽聽聽聽聽聽聽聽 return s %26lt;%26lt; a.gettype_as_string();
聽聽聽聽 }
};
class B : public A
{
聽聽聽聽 std::string type;
聽聽聽聽 std::string gettype_as_string() const { return type; }
public: B(const std::string%26amp; t) : type(t) {}
};
class D : public A
{
聽聽聽聽 int type;
聽聽聽聽 std::string gettype_as_string() const { std::ostringstream os; os %26lt;%26lt; type; return os.str(); }
public: D(int i) : type(i) {}
};
int main()
{
聽聽聽聽 A* d = new D(1);
聽聽聽聽 std::cout %26lt;%26lt; "A created from D(1) prints " %26lt;%26lt; *d %26lt;%26lt; '\n';
聽聽聽聽 delete d;
聽聽聽聽 A* b = new B("hi");
聽聽聽聽 std::cout %26lt;%26lt; "A created from B(\"hi\") prints " %26lt;%26lt; *b %26lt;%26lt; '\n';
聽聽聽聽 delete b;
}
For example, that function could return printable string representation of the type:
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;sstream%26gt;
class A
{
聽聽聽聽 virtual std::string gettype_as_string() const = 0;
public: virtual ~A() {}
聽聽聽聽 friend std::ostream%26amp; operator%26lt;%26lt;(std::ostream%26amp; s, const A%26amp; a)
聽聽聽聽 {
聽聽聽聽聽聽聽聽 return s %26lt;%26lt; a.gettype_as_string();
聽聽聽聽 }
};
class B : public A
{
聽聽聽聽 std::string type;
聽聽聽聽 std::string gettype_as_string() const { return type; }
public: B(const std::string%26amp; t) : type(t) {}
};
class D : public A
{
聽聽聽聽 int type;
聽聽聽聽 std::string gettype_as_string() const { std::ostringstream os; os %26lt;%26lt; type; return os.str(); }
public: D(int i) : type(i) {}
};
int main()
{
聽聽聽聽 A* d = new D(1);
聽聽聽聽 std::cout %26lt;%26lt; "A created from D(1) prints " %26lt;%26lt; *d %26lt;%26lt; '\n';
聽聽聽聽 delete d;
聽聽聽聽 A* b = new B("hi");
聽聽聽聽 std::cout %26lt;%26lt; "A created from B(\"hi\") prints " %26lt;%26lt; *b %26lt;%26lt; '\n';
聽聽聽聽 delete b;
}
Why do creationists claim gravitational time dilation makes a young earth possible?
Gravitational time dilation, if it existed on such a large scale, should be easily observable. On the contrary, we observe (from the periods of Cepheid variable stars, from orbital rates of binary stars, from supernova extinction rates, from light frequencies, etc.) that such time dilation is minor. There is some time dilation corresponding with Hubble's law (i.e., further objects have greater red shifts), but this is due to the well-understood expansion of the universe, and it is not nearly extreme enough to fit more than ten billion years into less than 10,000.
Humphreys tried to use clocks in the earth's frame of reference. But the cosmos is much older than the earth. Judging from the heavy elements in the sun and the rest of the solar system, our sun is a second-generation star at least. Billions of years must have passed for the first stars to have formed, shone, and become novas, for the gasses from those novas to have gathered into new star systems, and for the earth to form and cool in one such system. The billions of years before the earth are not accounted for in Humphreys's model.
Humphreys's theory assumes that the earth is in a huge gravity well. The evidence contradicts this assumption. If the earth were in such a gravity well, light from distant galaxies should be blue-shifted. Instead, it is red-shifted.
See Conner and Page (1998) and Conner and Ross (1999) for several other technical objections.
There is a great deal of other independent evidence that the earth is very old.
If there were any substance to Humphreys's proposal, at least some competent cosmologists would build on it and share in the Nobel Prize. Instead, they dismiss it as worthless.|||The name sounds made up in order to impress other YECs that it is true.
Rev. Neil|||Don't paint with such a wide brush ~ not every creationist believes in a young earth. Disproving the young earth theory doesn't disprove the Creator.|||Please wake me up when its over......yawn!!!!|||Because they don't understand it to the point wherein they can actually do the math and figure just what exactly gravitational time dilation would require the universe to be like physically to cause the effects they describe in the first place.
They just jump on the qualitative meaning of words and refuse to entertain the idea that just because it says X it doesn't necessarily imply "significant X for your explanatory purposes".|||They don't really try to understand the argument: anything that superficially confirms their biases is repeated without further reflection.
Unfortunately, it doesn't do much good to refute a creationist's argument because he doesn't really understand his own argument in the first place. He'll just trot out another cut-and-pasted fallacy and say, "But what about that, huh? That sounds all science-y and says evolution is wrong. Checkmate."|||I haven't heard that as much as the universe expansion rate argument (see Einsten and Hubble).
All theories assume since we can't observe. There's also a great deal of "evidence" that the earth is rather young. It all depends on the assumptions.
If we assume that there is a God for instance (even though he can't be proven by observation), then we assume it's not possible to have happened by chance. On the flip side if we assume that there is no God (again, can't be proven against by observation) then we leave no room for having a purpose here. There's always going to be assumptions, we just need to clear out as many as possible by making observations and even then we still have to assume that which we cannot observe.|||I don't claim this at all.|||I haven't heard this one before.But who cares anyway? No one takes them seriously.
Humphreys tried to use clocks in the earth's frame of reference. But the cosmos is much older than the earth. Judging from the heavy elements in the sun and the rest of the solar system, our sun is a second-generation star at least. Billions of years must have passed for the first stars to have formed, shone, and become novas, for the gasses from those novas to have gathered into new star systems, and for the earth to form and cool in one such system. The billions of years before the earth are not accounted for in Humphreys's model.
Humphreys's theory assumes that the earth is in a huge gravity well. The evidence contradicts this assumption. If the earth were in such a gravity well, light from distant galaxies should be blue-shifted. Instead, it is red-shifted.
See Conner and Page (1998) and Conner and Ross (1999) for several other technical objections.
There is a great deal of other independent evidence that the earth is very old.
If there were any substance to Humphreys's proposal, at least some competent cosmologists would build on it and share in the Nobel Prize. Instead, they dismiss it as worthless.|||The name sounds made up in order to impress other YECs that it is true.
Rev. Neil|||Don't paint with such a wide brush ~ not every creationist believes in a young earth. Disproving the young earth theory doesn't disprove the Creator.|||Please wake me up when its over......yawn!!!!|||Because they don't understand it to the point wherein they can actually do the math and figure just what exactly gravitational time dilation would require the universe to be like physically to cause the effects they describe in the first place.
They just jump on the qualitative meaning of words and refuse to entertain the idea that just because it says X it doesn't necessarily imply "significant X for your explanatory purposes".|||They don't really try to understand the argument: anything that superficially confirms their biases is repeated without further reflection.
Unfortunately, it doesn't do much good to refute a creationist's argument because he doesn't really understand his own argument in the first place. He'll just trot out another cut-and-pasted fallacy and say, "But what about that, huh? That sounds all science-y and says evolution is wrong. Checkmate."|||I haven't heard that as much as the universe expansion rate argument (see Einsten and Hubble).
All theories assume since we can't observe. There's also a great deal of "evidence" that the earth is rather young. It all depends on the assumptions.
If we assume that there is a God for instance (even though he can't be proven by observation), then we assume it's not possible to have happened by chance. On the flip side if we assume that there is no God (again, can't be proven against by observation) then we leave no room for having a purpose here. There's always going to be assumptions, we just need to clear out as many as possible by making observations and even then we still have to assume that which we cannot observe.|||I don't claim this at all.|||I haven't heard this one before.But who cares anyway? No one takes them seriously.
Somebody HELP me with this C++ program?
I dont know what is wrong it is supposed to convert decimal to binary!
Can I declare Arrays whose number of elements is variable?
How?
This program already output
the binary equivalent but it has extra numbers
#include %26lt;iostream.h%26gt;
void main ()
{
int num,xnum,dgts,counter;
cout%26lt;%26lt;"Enter Number"%26lt;%26lt;endl;
cin%26gt;%26gt;num;
if(num%26lt;=1)
cout%26lt;%26lt;endl%26lt;%26lt;num;
else
{
xnum=num;
dgts=0;
while(xnum%26gt;=1)
{
xnum=xnum/2;
dgts+=1;
}
int bnr[50];
for (counter=0;counter%26lt;dgts;counter++)
{
bnr[counter]=num%2;
num=num/2;
}
cout%26lt;%26lt;endl%26lt;%26lt;"The number entered converted to binary is ";
while(dgts%26gt;=0)
{
dgts=dgts-1;
cout%26lt;%26lt;bnr [dgts];
}
}
}|||Everything is right in the program. Only at the end, while printing the number, you are making a mistake. The while loop should be
"while (dgts%26gt;0)" instead of while(dgts%26gt;=0).
Whats happening is at the end, when dgts = 0;
dgts = dgts -1, will make dgts = -1;
and then you are trying to access bnr[-1], which is illegal.
And certainly you can use a dynamic array, instead of static. Using dynamic arrays you can have whose number of elements is variable.
In above example instead of int bnr[50], you can simply declare int * bnr;
And then when you know how mnay elements you need, you can allocate memory as :
bnr = (int *)malloc(dgts * sizeof(int));
The rest of the program will be as it is.
Here is the entire right program, with dynamic memory.
#include %26lt;iostream.h%26gt;
void main ()
{
int num,xnum,dgts,counter;
cout%26lt;%26lt;"Enter Number"%26lt;%26lt;endl;
cin%26gt;%26gt;num;
if(num%26lt;=1)
cout%26lt;%26lt;endl%26lt;%26lt;num;
else
{
xnum=num;
dgts=0;
while(xnum%26gt;=1)
{
xnum=xnum/2;
dgts+=1;
}
int *bnr;
bnr = (int *)malloc(dgts * sizeof(int));
for (counter=0;counter%26lt;dgts;counte++)
{
bnr[counter]=num%2;
num=num/2;
}
cout%26lt;%26lt;endl%26lt;%26lt;"The number entered converted to binary is ";
while(dgts%26gt;0)
{
dgts=dgts-1;
cout%26lt;%26lt;bnr [dgts];
}
}
}|||OK, I haven't compiled your given code in my compiler, but looking at the code, and assuming that you are getting the binary equivalent, with extra digits means that your array bnr[ ] is printing out ALL the elements in it.
I suggest you first initialize the array before assigning it any value. Then, you can assign a NULL character to index after that last element of the array. This should solve the problem.
Good Luck!
Can I declare Arrays whose number of elements is variable?
How?
This program already output
the binary equivalent but it has extra numbers
#include %26lt;iostream.h%26gt;
void main ()
{
int num,xnum,dgts,counter;
cout%26lt;%26lt;"Enter Number"%26lt;%26lt;endl;
cin%26gt;%26gt;num;
if(num%26lt;=1)
cout%26lt;%26lt;endl%26lt;%26lt;num;
else
{
xnum=num;
dgts=0;
while(xnum%26gt;=1)
{
xnum=xnum/2;
dgts+=1;
}
int bnr[50];
for (counter=0;counter%26lt;dgts;counter++)
{
bnr[counter]=num%2;
num=num/2;
}
cout%26lt;%26lt;endl%26lt;%26lt;"The number entered converted to binary is ";
while(dgts%26gt;=0)
{
dgts=dgts-1;
cout%26lt;%26lt;bnr [dgts];
}
}
}|||Everything is right in the program. Only at the end, while printing the number, you are making a mistake. The while loop should be
"while (dgts%26gt;0)" instead of while(dgts%26gt;=0).
Whats happening is at the end, when dgts = 0;
dgts = dgts -1, will make dgts = -1;
and then you are trying to access bnr[-1], which is illegal.
And certainly you can use a dynamic array, instead of static. Using dynamic arrays you can have whose number of elements is variable.
In above example instead of int bnr[50], you can simply declare int * bnr;
And then when you know how mnay elements you need, you can allocate memory as :
bnr = (int *)malloc(dgts * sizeof(int));
The rest of the program will be as it is.
Here is the entire right program, with dynamic memory.
#include %26lt;iostream.h%26gt;
void main ()
{
int num,xnum,dgts,counter;
cout%26lt;%26lt;"Enter Number"%26lt;%26lt;endl;
cin%26gt;%26gt;num;
if(num%26lt;=1)
cout%26lt;%26lt;endl%26lt;%26lt;num;
else
{
xnum=num;
dgts=0;
while(xnum%26gt;=1)
{
xnum=xnum/2;
dgts+=1;
}
int *bnr;
bnr = (int *)malloc(dgts * sizeof(int));
for (counter=0;counter%26lt;dgts;counte++)
{
bnr[counter]=num%2;
num=num/2;
}
cout%26lt;%26lt;endl%26lt;%26lt;"The number entered converted to binary is ";
while(dgts%26gt;0)
{
dgts=dgts-1;
cout%26lt;%26lt;bnr [dgts];
}
}
}|||OK, I haven't compiled your given code in my compiler, but looking at the code, and assuming that you are getting the binary equivalent, with extra digits means that your array bnr[ ] is printing out ALL the elements in it.
I suggest you first initialize the array before assigning it any value. Then, you can assign a NULL character to index after that last element of the array. This should solve the problem.
Good Luck!
PROBABILITY THAT HEADS WILLE UP WHEN a FAIR COIN IS TOSSED?
Let 惟 = {w1,w2,w3...wn} , wi is an outcome in the sample space and
X be a discrete random variable over S with probability mass function
m(w):s--%26gt;[0,1]
then 危 (wi) | i=1..n =1. where m(w3)=...m(wn)=0.
The moment generating function of X is M_X(t)=EXP(e^(tX)), t E R
=危 (wi)
Let 胃 = { }. Then w1 /\ w2 = $theta;
Corillary: EXP(X) for a random variable X over a probabilitically symmetrical, binary and uniform S.
= Pr(X=wi) for each i.
=%26gt;危 e^(tw)*m(w) , w E Z= M_X(t)
where dM(t)/dt|t=0 =EXP(X). therefore
危 w*m(w) , w E Z = 1/2*1+0*1/2=1/2
Answer = 1/2
??????????????|||Erm... right. A coin has two sides. The probability that any one side will come up is 1/2 (the required side/the number of sides), so for n tosses, the probability that it will come up the same side every time is 1/(2^n).|||You are an idiot dude. you went thru all that and you kno there are 2 sides. That means you have a 50 percent chance of gettiing 1 side.
X be a discrete random variable over S with probability mass function
m(w):s--%26gt;[0,1]
then 危 (wi) | i=1..n =1. where m(w3)=...m(wn)=0.
The moment generating function of X is M_X(t)=EXP(e^(tX)), t E R
=危 (wi)
Let 胃 = { }. Then w1 /\ w2 = $theta;
Corillary: EXP(X) for a random variable X over a probabilitically symmetrical, binary and uniform S.
= Pr(X=wi) for each i.
=%26gt;危 e^(tw)*m(w) , w E Z= M_X(t)
where dM(t)/dt|t=0 =EXP(X). therefore
危 w*m(w) , w E Z = 1/2*1+0*1/2=1/2
Answer = 1/2
??????????????|||Erm... right. A coin has two sides. The probability that any one side will come up is 1/2 (the required side/the number of sides), so for n tosses, the probability that it will come up the same side every time is 1/(2^n).|||You are an idiot dude. you went thru all that and you kno there are 2 sides. That means you have a 50 percent chance of gettiing 1 side.
Truth tables and input variables?
What would be the truth table of a function with 4 input variables, A,B,C,D which is 1 if the binary value of the binary number represented by the bits AB = the value of the binary number represented by the bits CD.
For example: if AB = 10 and CD = 01 then the output is 0 since 2 is greater than 1.|||A B C D R
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 0 1 1 0
0 1 0 0 0
0 1 0 1 1
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 0
1 0 1 0 1
1 0 1 1 0
1 1 0 0 0
1 1 0 1 0
1 1 1 1 1
A+B = C+D
For example: if AB = 10 and CD = 01 then the output is 0 since 2 is greater than 1.|||A B C D R
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 0 1 1 0
0 1 0 0 0
0 1 0 1 1
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 0
1 0 1 0 1
1 0 1 1 0
1 1 0 0 0
1 1 0 1 0
1 1 1 1 1
A+B = C+D
Subscribe to:
Posts (Atom)