[QUOTE=CommanderPT;40294629]
public class binarysearch { public static void printValues(int low, int high, int mid){ System.out.print("Low: " + low + "\n Mid: " + mid + "\n High: " + high + "\n"); } public static void main(String[] args) { int elements[] = {10, 11, 12, 16, 20, 25, 32, 36, 40, 44, 50, 55, 64, 77, 81, 90}; int low = 0; int high = elements.length -1; int mid = 0; int target = 90; //Can be changed. The number we are looking for. int p = 0; int i = 0; System.out.println("Target is: " + target + "\n"); while(i == 0 && low <= high){ if(low > high){ p = -1; }else{ mid = (low + high) / 2; if(target == elements[mid]){ p = mid; System.out.println("Found target at index: " + p); i = 1; }else if(target < elements[mid]){ high = mid; p = mid; printValues(low,high,mid); }else if(target > elements[mid]){ low = mid; p = mid; printValues(low,high,mid); }else{ printValues(low,high,mid); } } } }}
I've made this rather messy binary search algorithm. It works but if I want to search for 90, as in the last spot of the array it fails and falls into an endless loop. I've tried to fix it but I've ran out of ideas.[/QUOTE]
Your logic for recursively calling the binary search is faulty here:
[CODE]...}else if (target < elements[mid]){ high = mid; p = mid; printValues(low,high,mid);}...[/CODE]
For binary search, you need to recursively call the search in the following fashion. If the mid element (in your case elements[mid]) is lower than the searched-for value, you recursively search the left half of the array. However, you need to set the new high value to the previous middle [I]minus one[/I], or you might get an infinite loop as you describe. Similarly, if the searched-for value is greater than the middle value, you recursively search the right half of the array, with the new low value set to the previous middle value plus one.
It appears that you are taking an iterative approach to the binary search rather than recursive, but the same logic should apply. When you compute your new low/high values, add and subtract one, respectively. Integer division will take care of the fractional parts.
[QUOTE=thirty9th;40296063]Your logic for recursively calling the binary search is faulty here:
[CODE]...}else if (target < elements[mid]){ high = mid; p = mid; printValues(low,high,mid);}...[/CODE]
For binary search, you need to recursively call the search in the following fashion. If the mid element (in your case elements[mid]) is lower than the searched-for value, you recursively search the left half of the array. However, you need to set the new high value to the previous middle [I]minus one[/I], or you might get an infinite loop as you describe. Similarly, if the searched-for value is greater than the middle value, you recursively search the right half of the array, with the new low value set to the previous middle value plus one.
It appears that you are taking an iterative approach to the binary search rather than recursive, but the same logic should apply. When you compute your new low/high values, add and subtract one, respectively. Integer division will take care of the fractional parts.[/QUOTE]
Oh yeah, sorry for not mentioning it. I have to make an iterative and recursive solution. The one I posted is the iterative one. I'll take a closer look at it and see if I can solve it with your help. Thanks.
I have a question. For my little project I am making, I am using a local sql database (.sdf) to store data on users and nodes. Right now I have it setup so that on runtime, it opens a connection to the local database and dumps the data into datatables in memory, and then closes the connection. All work is then done on the local copy. So any time I have to search for a user or interact with the node, the data for those is stored in the local table. I have come to the point where I want to be able to dynamically create users and nodes inside of the application, so I need a method to save these changes. Now I plan on making it an internal command, so I could type "savedatabase" and the system checks if I am an admin and if I am it will take the tables in memory and update the local database with them. The question I have is how I can actually do this. By the time I call save database, the connection has been closed. I want this to be able to be called at any time while the program is running.
Here is my code for setting up the database, it's no doubt ugly and messy. (novice programmer)
[code]
public void setupDatabase()
{
SimNetDataSet dataSet = new SimNetDataSet();
DataTable UserTable = new DataTable("Users");
DataTable NodeTable = new DataTable("Nodes");
SqlCeConnection conn = new SqlCeConnection(@"Data Source=|DataDirectory|\SimNet.sdf; password=simnet");
try //to open a connection to the database
{
conn.Open();
}
catch (SqlCeException e)
{
Console.WriteLine(e); //Print any problems
}
Console.WriteLine("Opened.");
SqlCeDataAdapter dsDataAdapter = new SqlCeDataAdapter("SELECT * FROM Users", conn); //gonna steal it all
SqlCeDataAdapter nsDataAdapter = new SqlCeDataAdapter("SELECT * FROM Nodes", conn);
dsDataAdapter.Fill(UserTable); //throw it in a table
nsDataAdapter.Fill(NodeTable);
Console.WriteLine("Data Captured Successfully");
conn.Close(); //were done here
dsDataAdapter.Dispose(); //thank you for you noble service, now get out
nsDataAdapter.Dispose();
Console.WriteLine("Verifying data capture: ");
foreach (DataRow row in UserTable.Rows) //this is mainly for debug to see whats there, later it can be removed or toggled
{
string username = row["Username"].ToString();
string password = row["Password"].ToString();
int perm = (int)row["Permissions"];
int id = (int)row["ID"];
Console.WriteLine("Username: " + username);
Console.WriteLine("Password: " + password);
Console.WriteLine("Permissions: " + perm);
Console.WriteLine("ID: " + id);
}
Console.WriteLine("Data Dump completed.");
admin = new User();
admin.Username = "admin";
admin.Password = "admin";
admin.ID = -1;
admin.Permissions = -1;
setupUsers(UserTable); //sneaky eh?
nodeMan.setupNodeManager(NodeTable, this); //Start up node manager
}
[/code]
And this would be a crude example of what I would like for the save database to do:
[code]
public bool saveDatabases(DataTable usertable, DataTable nodetable)
{
sqlupdate(usertable, nodetable); //This function would totally make it so that changes in the local tables compared to the database's tables are saved, but not rewriting everything because if the tables are big it would be way too slow.
return true;
}
[/code]
Very pseudocode-y.
I know there is an update command for the adapters, but would that work if it is not right after a fill and edit? The fill and edits and updates would all take place at different times, and the table isn't the same one as its been moved to a local copy in memory and been potentially modified.
Most of it probably isn't important to you guys, but I included the whole function just in case. Let me know if you have any questions so I can try to explain. (Oh and this is c# in case you can't tell, I didn't actually mention it before :v:)
Quick question when I uncomment one of the following lines (any of them) even with the opengl mayor / minor version turned down to what this laptop supports it crashes on my work laptop but not on my computer.
[code]
//glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
//glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 2 );
//glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
[/code]
Any idea why ?
What do you guys recommend for 3D game development in C#? I'm looking at using OpenTK or Tao but would like to see some opinions.
Or do you not recommend C#? Because I can do other languages, I just prefer C#.
[QUOTE=reevezy67;40300914]What do you guys recommend for 3D game development in C#? I'm looking at using OpenTK or Tao but would like to see some opinions.
Or do you not recommend C#? Because I can do other languages, I just prefer C#.[/QUOTE]
MonoGame? Unity 3D (if you can afford the premium version or you don't mind missing key features)?
So I'm trying to write a simple function to word wrap an input string. (In this case, a movie synopsis). Problem is, everything outside of the loop isn't doing what it should and I don't know why.
[cpp]
void WordWrap(string plot)
{
unsigned const int MAX_LENGTH = 72;
unsigned int count; //both unsigned so no error when compared to plot
int index;
string outWordStr;
string outLineStr; //both strings used to determine wrap length
index = 0; //initialize
for(count = 0; count <= plot.length(); count++)
{
if(plot[index] != ' ')
{
outWordStr = outWordStr + plot[index];
index++;
}
else
{
if(outWordStr.length() + outLineStr.length() > MAX_LENGTH)
{
cout << outLineStr << endl;
outLineStr.clear();
}
outLineStr = outLineStr + outWordStr + ' ';
outWordStr.clear();
index++;
}
}//end FOR
//now deal with remaining in outWordStr or outLineStr
if(outWordStr.length() > 0 || outLineStr.length() > 0) //if anything remaining in either
{
outWordStr = outWordStr + plot[index];
index++;
}
else
{
if(outWordStr.length() + outLineStr.length() > MAX_LENGTH)
{
cout << outLineStr << endl;
outLineStr.clear();
}
outLineStr = outLineStr + outWordStr + ' ';
outWordStr.clear();
index++;
}
}[/cpp]
the loop works fine, but i have a feeling something's wrong in the remainder statement...should that be in a loop too? As it is it just doesn't output the last line PERIOD. I'm not sure why. (also it's really annoying to try and format in this without being able to tab spaces).
[editline]16th April 2013[/editline]
and yeah i know the index++ shouldn't be at the end but i just copied it from inside the loop.
which is probably one reason it doesn't work.
[QUOTE=Gnomical;40298303]I have a question. For my little project I am making, I am using a local sql database (.sdf) to store data on users and nodes. Right now I have it setup so that on runtime, it opens a connection to the local database and dumps the data into datatables in memory, and then closes the connection. All work is then done on the local copy. So any time I have to search for a user or interact with the node, the data for those is stored in the local table. I have come to the point where I want to be able to dynamically create users and nodes inside of the application, so I need a method to save these changes. Now I plan on making it an internal command, so I could type "savedatabase" and the system checks if I am an admin and if I am it will take the tables in memory and update the local database with them. The question I have is how I can actually do this. By the time I call save database, the connection has been closed. I want this to be able to be called at any time while the program is running.
Here is my code for setting up the database, it's no doubt ugly and messy. (novice programmer)
...
[code]
public bool saveDatabases(DataTable usertable, DataTable nodetable)
{
sqlupdate(usertable, nodetable); //This function would totally make it so that changes in the local tables compared to the database's tables are saved, but not rewriting everything because if the tables are big it would be way too slow.
return true;
}
[/code]
...[/QUOTE]
I don't know much about C#, so I could be misinterpeting this, but it looks like you need another layer of abstraction. I.e. you should code it so that you have a db_load function and a db_save function and outside of that you don't have [I]any[/I] SQL or table-related code. db_load would read the tables into something like a dictionary/map and db_save would turn that dictionary back into a SQL table for dumping.
aww man, I had an assignment in CS251 to format paragraphs. I did with with an 11 line or so recursive function. It was so fucking sexy.
i fixed it nevermind
What's the best way of doing think/draw cycles?
Put them on the same thread or make a seperate think thread that fires only x times per seconds?
[editline]17th April 2013[/editline]
or this?
[code]void draw() {
//stuff
}
void think(float gameTime) {
//stuff
}[/code]
[QUOTE=quincy18;40299783]Quick question when I uncomment one of the following lines (any of them) even with the opengl mayor / minor version turned down to what this laptop supports it crashes on my work laptop but not on my computer.
[code]
//glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
//glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 2 );
//glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
[/code]
Any idea why ?[/QUOTE]
It may be problems with tool you're using, if you're using Microsoft Visual Studio 2010 on laptop upgrade it to 2012, upgrade all of tools you are using and it might be fine, sometimes old stuffs make problems like this which i had with uTorrent before, the error is sending something null to your GPU and GPU can't read it, so it restarts ram and computer with blue screen, it happens for null stuffs like when you open an icon, i got uTorrent fixed by downloading newer versions of it :)
Can someone help me out with ArrayList Iterator in Java?
:|
[QUOTE=pyschomc;40317479]Can someone help me out with ArrayList Iterator in Java?
:|[/QUOTE]
[code]
for (Object o : arrayList) {
// do something with o
}
[/code]
?
[QUOTE=pyschomc;40317479]Can someone help me out with ArrayList Iterator in Java?
:|[/QUOTE]
What exactly is your issue with it?
[editline]18th April 2013[/editline]
[QUOTE=Goz3rr;40317701][code]
for (Object o : arrayList) {
// do something with o
}
[/code]
?[/QUOTE]
That's iterating with a for loop, not iterating with an iterator.
[editline]18th April 2013[/editline]
[code]
Arraylist<Object> list = new ArrayList<Object>();
Iterator<Object> it = list.iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
[/code]
That is the basic usage of an iterator.
-snip- jesus i posted like 2 questions because this was getting hard and really pissing me off, but i ended up figuring it out :) (wasn't really that hard lmao)
I'm making a tip calculator for android for a class project
In C# when a class is public are it's functions too? I'm new to C# so I'm trying to grasp the concept here
[QUOTE=jrj996;40328386]In C# when a class is public are it's functions too? I'm new to C# so I'm trying to grasp the concept here[/QUOTE]
No, visibility is determined per method / variable. Also think about this:
If you create a private class, how do you expect to use it? You can't access it, it's private.
[editline]18th April 2013[/editline]
Unless you create the class inside the scope of another class of course.
[QUOTE=mobrockers2;40328487]No, visibility is determined per method / variable. Also think about this:
If you create a private class, how do you expect to use it? You can't access it, it's private.
[editline]18th April 2013[/editline]
Unless you create the class inside the scope of another class of course.[/QUOTE]
Ah alright I see, thanks!
[QUOTE=!!!WARLOCK!!!;40317198]It may be problems with tool you're using, if you're using Microsoft Visual Studio 2010 on laptop upgrade it to 2012, upgrade all of tools you are using and it might be fine, sometimes old stuffs make problems like this which i had with uTorrent before, the error is sending something null to your GPU and GPU can't read it, so it restarts ram and computer with blue screen, it happens for null stuffs like when you open an icon, i got uTorrent fixed by downloading newer versions of it :)[/QUOTE]
I have visual studio 2012 proffesional registered on both machines, the only difference is that the laptop is using windows 8.
Say you had a useful function in C++ that will be used by other classes you have, but does not necessarily fit in any of those classes (a function that loads glsl shaders from files, in this example). Is there any standard way to go about this, as in integrating it in your project?
[QUOTE=Meatpuppet;40331046]Say you had a useful function in C++ that will be used by other classes you have, but does not necessarily fit in any of those classes (a function that loads glsl shaders from files, in this example). Is there any standard way to go about this, as in integrating it in your project?[/QUOTE]
Make their visibility public.
[QUOTE=mobrockers2;40331263]Make their visibility public.[/QUOTE]
I'm not sure you understand what I'm saying. The function will be used by most of my classes, but I'm not sure if I should put the function in a separate file, or what.
[QUOTE=Meatpuppet;40331574]I'm not sure you understand what I'm saying. The function will be used by most of my classes, but I'm not sure if I should put the function in a separate file, or what.[/QUOTE]
If the function is public you can call it from other classes? Indeed I do not understand what the problem is, clarify please.
[QUOTE=mobrockers2;40331593]If the function is public you can call it from other classes? Indeed I do not understand what the problem is, clarify please.[/QUOTE]
I have a single function, loadShader(file arguments)
That function will be used in lots of classes.
Where do I put the function?
[QUOTE=Meatpuppet;40331995]I have a single function, loadShader(file arguments)
That function will be used in lots of classes.
Where do I put the function?[/QUOTE]
I'd say where it makes most sense but i suppose that doesn't help you much.
[QUOTE=mobrockers2;40332162]I'd say where it makes most sense but i suppose that doesn't help you much.[/QUOTE]
I don't want to include classes I don't need to just for one function. Is there any way I can?
[QUOTE=Meatpuppet;40332170]I don't want to include classes I don't need to just for one function. Is there any way I can?[/QUOTE]
Code duplication. Wouldn't advice it though.
[QUOTE=mobrockers2;40332193]Code duplication. Wouldn't advice it though.[/QUOTE]
Then what can I do?
[QUOTE=Meatpuppet;40332202]Then what can I do?[/QUOTE]
What is your problem with including? That's what is for.
Sorry, you need to Log In to post a reply to this thread.