|
You're forgetting to reset maxSoFar, and Bad Things happen when i == 0
|
|
|
|
|
Can you explain how to "reset" maxSoFar? I thought i just gotta swap the index into temp and then change the small to that index and then the temp to the max so far?
What will happen when i reaches 0?
|
|
|
|
|
bmnot wrote: I thought i just gotta swap the index into temp and then change the small to that index and then the temp to the max so far?
Yes, that is right.
But what you're doing now is remember the index of the global max of the entire array, instead of just the part that you're supposed to be looking at. The next iteration you will need the max of a smaller part of the array, not the global max.
The Bad Things that happen at i == 0 are also partially a result of not resetting maxSoFar, it will not search for a new max (since 0 is not smaller than 0), and then swap them anyway. If you found the correct max (maxSoFar = 0 be cause it's the only one you can still scan) then swapping 0 with 0 has no effect, but you'd still have the old max, so strange things happen.
|
|
|
|
|
Okay, I understand the maxSoFar thing now, I'm a bit of a n00b to programming and REALLY appreciate this help. I'm doing it in college and have only been at it about 6 weeks.
Also, how do I get around this i = 0 problem? Cause I've noticed that when I input the strings, they end up in an order that is most definatly not alphabetically!
|
|
|
|
|
Well you could change the ending condition of the outer loop to i > 0 , that should work
|
|
|
|
|
But then would that not stop before the 1st array (list[0])?
I've done this and the list is completly messed up! I've re-upped the code, here is the link.[Source Code]
Could you compile and test it please and see if you can find what the problem is?
Also, as far as programming goes, is this code "messy"?
Thanks,
Niall
|
|
|
|
|
Now you're resetting maxSoFar too often
Oh this is not messy.. just wait until you have to implement things like chordal graph colouring...
About the first element - yes, it would stop before it. But you wouldn't move it even if you did not stop before it. The only candidate for swapping would be itself, and that's not very useful. Also you could see it like this, all following items are already correctly sorted (because of the selection-sort invariant which says that the "last" portion of the array is already perfect), and you only have 1 item left, it must already be in the right place, since it can not go anywhere else. It's not a problem if list[0] was not the smallest element in the beginning - it will be the smallest now (so apparently it got swapped already)
|
|
|
|
|
god i'm confused now!! Where should the maxSoFar go then?
haha. Can you give me some examples maybe with some code to show me? I'd much appreciate it.
Thanks,
Niall
|
|
|
|
|
Maybe so, but then I'd pretty much give you the answer wouldn't I
Seems like cheating..
On the other hand, it's on wikipedia anyway
Here is the code that does it the other way around (starting at i = 0 and finding the smallest element every time)
http://en.wikipedia.org/wiki/Selection_sort#Code[^]
|
|
|
|
|
So what's up, did you solve the problem?
|
|
|
|
|
fixing the former could fix the latter, even when nothing needs to be done for i==0
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Can a java application call a WCF service?If Yes can any one help me how to do it?It is really urgent.
I have created a WCF service in .net .
I have simple java application.
I want to use WCF service in Java application..
|
|
|
|
|
|
You posted this question in three other forums. Please use one forum only for your queries.
MVP 2010 - are they mad?
|
|
|
|
|
Hello..
I am trying to write a code in socket programming using java that would transfer audio files from 1 destination to another.
I am able to read the audio file successfully using AudioInputStream and AudioSystem methods.
However, i am unable to write back the same file to the destination. The client side does not receive the audio file properly.
Please can u help me?
I was able to successfully transfer a simple text file..but it just doesn't seem to work for audio files..
Thanks n regards..
here is my source code..
Server code
public class UdpSend1
{
private DatagramSocket socket = null;
private DatagramPacket recvPacket, sendPacket;
private int hostPort;
public static void main(String args[])
{
DatagramSocket socket = null;
DatagramPacket recvPacket, sendPacket;
try
{
socket = new DatagramSocket();
InetAddress hostAddress = InetAddress.getLocalHost();
File fr = new File("C:\\Taal_Theme.wav");
AudioInputStream aio = AudioSystem.getAudioInputStream(fr);
int totalFramesRead = 0;
while (socket != null)
{
int bytesPerFrame = aio.getFormat().getFrameSize();
if (bytesPerFrame == AudioSystem.NOT_SPECIFIED)
{
}
int numBytes = 1024 * bytesPerFrame;
byte[] audioBytes = new byte[numBytes];
try
{
int numBytesRead = 0;
int numFramesRead = 0;
while ((numBytesRead = aio.read(audioBytes)) != -1)
{
numFramesRead = numBytesRead / bytesPerFrame;
totalFramesRead += numFramesRead;
sendPacket = new DatagramPacket(audioBytes, numFramesRead, hostAddress, numFramesRead);
socket.send( sendPacket );
recvPacket= new DatagramPacket(new byte[512], 512, hostAddress, 0);
socket.receive(recvPacket);
System.out.write(recvPacket.getData(), 0,recvPacket.getLength());
System.out.print("\n");
}
}
catch (SocketException se)
{
System.out.println("Error in SimpleDatagramClient: " + se);
}
}
}
catch (IOException ioe)
{
System.out.println("Error in SimpleDatagramClient: " + ioe);
}
catch (UnsupportedAudioFileException e)
{
System.out.println("Error!!! "+e);
e.printStackTrace();
}
}
}
The Client code
public class UdpReceive1
{
public static void main(String args[])
{
DatagramSocket socket = null;
DatagramPacket recvPacket, sendPacket;
try
{
InetAddress hostAddress = InetAddress.getLocalHost();
FileOutputStream fout = new FileOutputStream("C:\\f.wav");
AudioInputStream aio;
socket = new DatagramSocket(4545);
while (socket != null)
{
System.out.println("HELLO!!!");
recvPacket= new DatagramPacket(new byte[512], 512,
hostAddress, 0);
socket.receive(recvPacket);
System.out.println("HELLO!!!");
byte[] str = new byte[512];
str = recvPacket.getData();
System.out.println(str);
ByteArrayInputStream bis = new ByteArrayInputStream(str);
System.out.println(bis);
aio = new AudioInputStream(bis, null, 512);
AudioSystem.write(aio, AudioFileFormat.Type.WAVE, fout);
0,recvPacket.getLength());
System.out.print("\n");
sendPacket = new DatagramPacket(recvPacket.getData(),
recvPacket.getLength(),recvPacket.getAddress(),recvPacket.getPort());
socket.send( sendPacket );
}
}
catch (SocketException se)
{
System.out.println("Error in DatagramClient: " + se);
}
catch (IOException ioe)
{
System.out.println("Error in DatagramServer: " + ioe);
}
}
}
modified on Monday, January 25, 2010 9:58 AM
|
|
|
|
|
vidzdas wrote: However, i am unable to write back the same file to the destination. The client side does not receive the audio file properly.
Perhaps you could explain exactly what happens when you try this. There is no real difference in sending a text file or an audio file from one computer to another; a file is just a stream of bytes. It may be that you should read the file as a simple BufferedInputStream rather than using the AudioSystem object.
MVP 2010 - are they mad?
|
|
|
|
|
Hey thanks..but i already tried that.
A file is created on the other computer but only garbage is displayed. The audio comes as only bytes.
Can u please suggest something else.??
|
|
|
|
|
vidzdas wrote: Hey thanks..but i already tried that.
Uh, tried what?
vidzdas wrote: A file is created on the other computer but only garbage is displayed. The audio is comes as only bytes.
I don't understand, there is nothing to display from an audio file.
MVP 2010 - are they mad?
|
|
|
|
|
|
4277480 wrote: Try this
That's what I was suggesting. I wish I had a £ for every time I have tried to get people to understand that a file is merely a stream of bytes, and transferring from one place to another without the need to understand the content is what keeps the entire world running these days.
MVP 2010 - are they mad?
|
|
|
|
|
haha I know but the thought of sending stream of garbage which is supposed to be an audio file really is an interesting theory
|
|
|
|
|
4277480 wrote: the thought of sending stream of garbage which is supposed to be an audio file really is an interesting theory
It fits the model of so many issues posted here these days!
btw welcome back, haven't seen input from you for some time.
MVP 2010 - are they mad?
|
|
|
|
|
Thank you, now I am back to idle mode until the next project, so from time to time I will be referencing here.
|
|
|
|
|
hey
try to use ObjectInputStream and ObjectOutputStream
am always use it to transfer any type of files.
/*********************************************
* simple FTP(Client Side/upload) *
* Written By : Salah I. Qasem *
* E-mail: tormentor701@hotmail.com *
*********************************************/
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String args[])
{
try{
InputStream file = new FileInputStream("fileName.xxx");
Socket socket = new Socket("127.0.0.1" , 2008);
byte[] bfrArray = new byte[file.available()];
file.read(bfrArray);
ObjectOutputStream obj = new ObjectOutputStream(socket.getOutputStream());
obj.writeObject(bfrArray);
}
catch(Exception ex){System.err.println(ex);}
}
}
/*********************************************
* simple FTP(Server Side/receive) *
* Written By : Salah I. Qasem *
* E-mail: tormentor701@hotmail.com *
*********************************************/
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])
{
try{
ServerSocket serverSocket = new ServerSocket(2008);
Socket socket= serverSocket.accept();
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
byte[] bfrArray = (byte[])input.readObject();
OutputStream file = new FileOutputStream("newFileName.xxx");
DataOutputStream writer = new DataOutputStream(file);
writer.write(bfrArray);
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
|
|
|
|
|
Hi,
I have to create ssh window.And i've mindterm.jar and following code
<APPLET CODE="com.mindbright.application.MindTerm.class" ARCHIVE="mindterm312.jar?r1" WIDTH=800 HEIGHT=600>
<PARAM NAME="line-space-delta" value="0">
<PARAM NAME="local-echo" value="false">
<PARAM NAME="rev-video" value="false">
<PARAM NAME="ignore-null" value="true">
<PARAM NAME="crlf" value="false">
<PARAM NAME="font-size" value="12">
<PARAM NAME="autowrap" value="true">
<PARAM NAME="delete-send" value="bs">
<PARAM NAME="ascii-line" value="false">
<PARAM NAME="local-pgkeys" value="false">
<PARAM NAME="font-name" value="Monospaced">
<PARAM NAME="visual-bell" value="false">
<PARAM NAME="geometry" value="80x24">
<PARAM NAME="backspace-send" value="del">
<PARAM NAME="paste-button" value="middle">
<PARAM NAME="rev-autowrap" value="false">
<PARAM NAME="passthru-prn-enable" value="true">
<PARAM NAME="copy-select" value="true">
<PARAM NAME="save-lines" value="512">
<PARAM NAME="term-type" value="xterm">
<PARAM NAME="key-mappings" value="">
<PARAM NAME="insert-mode" value="false">
<PARAM NAME="username" value="">
<PARAM NAME="copy-crnl" value="false">
<PARAM NAME="bg-color" value="white">
<PARAM NAME="server" value="apps0.toronto.edu">
<PARAM NAME="print-font-size" value="8">
<PARAM NAME="input-charset" value="none">
<PARAM NAME="repos-output" value="true">
<PARAM NAME="resizable" value="true">
<PARAM NAME="repos-input" value="true">
<PARAM NAME="map-ctrl-space" value="true">
<PARAM NAME="print-font-name" value="Courier">
<PARAM NAME="encoding" value="iso-8859-1">
<PARAM NAME="virtual-kbd" value="">
<PARAM NAME="cursor-color" value="i_blue">
<PARAM NAME="scrollbar" value="right">
<PARAM NAME="auto-linefeed" value="false">
<PARAM NAME="select-delim" value="\"\"">
<PARAM NAME="visible-cursor" value="true">
<PARAM NAME="fg-color" value="black">
</APPLET>
When i run this html, i got ssh window but this following error will occur.
Error connecting to apps0.toronto.edu, reason:
-> access denied (java.net.SocketPermission apps0.toronto.edu resolve)
Is there any bug in applet code? please help me.........
|
|
|
|