• What Do You Need Help With? V6
    7,544 replies, posted
[IMG]http://puu.sh/9rdcI/e8a3bd158c.jpg[/IMG] Can someone please explain to me how to figure this out?
You need to simplify the above and figure out what it's actually doing. Each if statement checks if the first letter is less than the second letter. If this is true, then we swap the two letters. Do note that the statements are evaluated sequentially, not all at once, so each letter may not have the same value in one if statement as it does in the next. The swap works by using a temporary variable because otherwise once you set the first letter to equal the second letter, both your letters are the same and therefore you can no longer swap the two letters. You can also swap using only two variables with bit level hacks, but that's another thing entirely, and it's efficiency is questionable at best! Here's the following pseudo code. Hopefully you can figure out the rest. [code] if letter1 < letter2 then swap letter1 and letter2 if letter2 < letter3 then swap letter2 and letter3 if letter1 < letter2 then swap letter1 and letter2 [/code]
[QUOTE=Bladezor;45086256]I'll do it for $30/hr.[/QUOTE] Hahahahaah, you're hilarious :'D
[QUOTE=RedCause;45091877]Hahahahaah, you're hilarious :'D[/QUOTE] he is dead serious. pay him $30/hour.
[QUOTE=G4MB!T;45092012]he is dead serious. pay him $30/hour.[/QUOTE] Or not get help?
[QUOTE=rookwood101;45092498]Or not get help?[/QUOTE] Or not get a full program written. The questions answered here are usually a few minutes of work at most, making the whole thing may take more than an hour.
In Java [CODE] private static void sumOfTrianglesIterative(int[][] rowsArray) { int[] sum = new int[rowsArray.length]; int total = 0; for (int i = 0; i < rowsArray.length; i++) { int maxRow = rowsArray[i][0]; for (int j = 0; j < rowsArray[i].length; j++) { if (maxRow < rowsArray[i][j]) { maxRow = rowsArray[i][j]; } } sum[i] = maxRow; } System.out.println("Max from each row " + Arrays.toString(sum)); for (int i = 0; i < sum.length; i++) { total += sum[i]; } System.out.println("Total: " + total); } private static int sumOfTrianglesRecursive(int[][] rowsArray, int row, int col) { int total; int rowChecker; int rowColChecker; if (row >= rowsArray.length) { return 0; } else { rowChecker = sumOfTrianglesRecursive(rowsArray,row+1, col); rowColChecker = sumOfTrianglesRecursive(rowsArray,row+1, col+1); total = Math.max(rowChecker, rowColChecker) + rowsArray[row][col]; return total; } } }[/CODE] So I have this 2D array that looks like a triangle. [QUOTE] 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5 3 4 5 1 2 3[/QUOTE] Max from each row [1, 2, 3, 4, 5, 5] Iterative: 20 Recursion: 16 I can get the iterative one to work just fine. It's the recursive one that is giving me weird outputs. Where am I going wrong?
[QUOTE=RedCause;45091877]Hahahahaah, you're hilarious :'D[/QUOTE] No one is going to do all the work for you, this isn't charity programming. You're asking us to spend [i]our[/i] time writing something entirely for [i]your[/i] "friends" benefit and frankly it's insulting to even ask. I don't understand what it is with people and having this sense of entitlement from software developers. While I was at college I had several business-related majors ask me to write them [i]the next facebook[/i] or some other unrealistic crap for no compensation. The internet is full of a plethora of resources and I'm more than positive someone's already made something similar if not identical to what you're asking. If you're not willing to put in any work then there's a fee for our work. If you have specific issues and some sample code, sure we might be able to help you with that. Otherwise, $30.00/hr is a more than reasonable rate. [editline]13th June 2014[/editline] [QUOTE=blacksam;45092731]So I have this 2D array that looks like a triangle. Max from each row [1, 2, 3, 4, 5, 5] Iterative: 20 Recursion: 16 I can get the iterative one to work just fine. It's the recursive one that is giving me weird outputs. Where am I going wrong?[/QUOTE] This looks very familiar, what's the expected pattern, is it a form of Pascal's triangle?
[QUOTE=Bladezor;45093293]This looks very familiar, what's the expected pattern, is it a form of Pascal's triangle?[/QUOTE] It could be used to produce Pascal's triangle that's for sure. The way I see it is that it traverses the triangle recursively finding the highest values from each row then adding those values to return a sum. What I don't understand is that I can have triangles of many different lengths and get some values to be equal to the iterative and others not equal. [QUOTE]run: Number of Triangles: 4 1 1 2 1 2 3 Max from each row [1, 2, 3] Total: 6 Result from recursion: 6 1 1 3 3 2 1 2 1 3 4 3 5 2 4 1 Max from each row [1, 3, 3, 4, 5] Total: 16 Result from recursion: 13 3 Max from each row [3] Total: 3 Result from recursion: 3 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5 3 4 5 1 2 3 Max from each row [1, 2, 3, 4, 5, 5] Total: 20 Result from recursion: 16 BUILD SUCCESSFUL (total time: 0 seconds) [/QUOTE]
[QUOTE=blacksam;45093440]It could be used to produce Pascal's triangle that's for sure. The way I see it is that it traverses the triangle recursively finding the highest values from each row then adding those values to return a sum. What I don't understand is that I can have triangles of many different lengths and get some values to be equal to the iterative and others not equal.[/QUOTE] Hmm, if I were doing it recursively I might take a different approach. I'd add an additional argument for rowMax, that way you have the previous rows value, which you can just add to the overall total. There would be two sets of recursive branches: One to increment the row count (this is done when the max for that row is found), and another to increment the column until the max is found. Poorly written Pseudo code [code] def totalTriangle (int[][] array, int row, int col, int rowMax) { int total = 0; if(row < array.RowCount) { if(col < array[row].Count) rowMax = Max(array[row][col], totalTriangle(array, row, col + 1)) else if(col == array.[row].Count) total += totalTriangle(array, row+1, 0, 0) + rowMax; } return total; } [/code] There's probably quite a bit of logic missing there but I hope that helps portray that idea.
[QUOTE=Bladezor;45093555]Hmm, if I were doing it recursively I might take a different approach. I'd add an additional argument for rowMax, that way you have the previous rows value, which you can just add to the overall total. There would be two sets of recursive branches: One to increment the row count (this is done when the max for that row is found), and another to increment the column until the max is found. Poorly written Pseudo code [code] def totalTriangle (int[][] array, int row, int col, int rowMax) { int total = 0; if(row < array.RowCount) { if(col < array[row].Count) rowMax = Max(array[row][col], totalTriangle(array, row, col + 1)) else if(col == array.[row].Count) total += totalTriangle(array, row+1, 0, 0) + rowMax; } return total; } [/code] There's probably quite a bit of logic missing there but I hope that helps portray that idea.[/QUOTE] Maybe I'm lost on the pseudocode logic or the idea in general. But I kept getting larger numbers than I was supposed to. I tried just returning rowMax instead and that put me back where I was before.
[QUOTE=blacksam;45095226]Maybe I'm lost on the pseudocode logic or the idea in general. But I kept getting larger numbers than I was supposed to. I tried just returning rowMax instead and that put me back where I was before.[/QUOTE] I'm at work right now so I just punched that out without testing it at all, but when I get home I'll give it a go real quick.
Hello all. For my Computer Science final (sophomore in high school) we have to make Simon Says. I have the basics down, but I'm lost on how to make "Simon" do his pattern. The only help my teacher gave me on this was that I must use a list, and I've never used a list before. Here's the code I have already. It's XNA C#. [code] using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace Simon_Says { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Rectangle s1, s2, s3, s4; Texture2D sT; SpriteFont font; bool rClick, gClick, bClick, yClick; Random random; int number; int score; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); s1= new Rectangle(175,25,200,200); s2 = new Rectangle(400,25,200,200); s3 = new Rectangle(175,250,200,200); s4 = new Rectangle(400, 250, 200, 200); sT = this.Content.Load<Texture2D>("PIXEL"); font = this.Content.Load<SpriteFont>("SpriteFont1"); random = new Random(); } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); IsMouseVisible = true; //The cursor is always visible. MouseState mouseState = Mouse.GetState(); score = 0; //Score starts at 0 number = random.Next(4); var mousePosition = new Point(mouseState.X, mouseState.Y); if (s1.Contains(mousePosition)) //If the mouse is over/on the green square { if (mouseState.LeftButton == ButtonState.Pressed) { rClick = true; //When the left mouse button is clicked, the square turns black. (rClick is set to true) } if (mouseState.LeftButton == ButtonState.Released) { rClick = false; } } else { rClick = false; //When the left mouse button is released, the square turns red. (rClick is set to false) } if (s2.Contains(mousePosition)) //If the mouse is over/on the green square. { if (mouseState.LeftButton == ButtonState.Pressed) { gClick = true; //When the left mouse button is clicked, the square turns black. (gClick is set to true) } if (mouseState.LeftButton == ButtonState.Released) { gClick = false; } } else { gClick = false; //When the left mouse button is released, the square turns green. (gClick is set to false) } if (s3.Contains(mousePosition)) //If the mouse is over/on the blue square. { if (mouseState.LeftButton == ButtonState.Pressed) { bClick = true; //When the left mouse button is clicked, the square turns black. (bClick is set to true) } if (mouseState.LeftButton == ButtonState.Released) { bClick = false; } } else { bClick = false; //When the left mouse button is released, the square turns blue. (rClick is set to false) } if (s4.Contains(mousePosition)) //If the mouse is over/on the yellow square. { if (mouseState.LeftButton == ButtonState.Pressed) { yClick = true; //When the left mouse button is clicked, the square turns black. (yClick is set to true) } if (mouseState.LeftButton == ButtonState.Released) { yClick = false; } } else { yClick = false; //When the left mouse button is released, the square turns yellow. (rClick is set to false) } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); if (rClick == false) { spriteBatch.Draw(sT, s1, Color.DarkRed); } else { spriteBatch.Draw(sT, s1, Color.Red); } if (gClick == false) { spriteBatch.Draw(sT, s2, Color.DarkGreen); } else { spriteBatch.Draw(sT, s2, Color.LawnGreen); } if (bClick == false) { spriteBatch.Draw(sT, s3, Color.DarkBlue); } else { spriteBatch.Draw(sT, s3, Color.Blue); } if (yClick == false) { spriteBatch.Draw(sT, s4, Color.DarkGoldenrod); } else { spriteBatch.Draw(sT, s4, Color.Yellow); } spriteBatch.DrawString(font, "Score:", new Vector2(0, 0), Color.White); spriteBatch.DrawString(font, score.ToString(), new Vector2(110,0), Color.White); spriteBatch.End(); base.Draw(gameTime); } } } [/code] [editline]13th June 2014[/editline] Ignore my horrendous commenting.
We're working on the Huffman's compression implementation. Everything works fine and all, but the decompression gives me a headache. It's working, but it's damn slow for files other than .txt. [code] while(binbuf.length() > 0) { a = 0; pakiecik = binbuf.substr(0, iter); while( ( ( (tablica[a].kod).compare(pakiecik) ) != 0 ) && (a < n) )a++; if( (tablica[a].kod).compare(pakiecik) ) { plikwy << znaktabl[a]; binbuf.erase(0, iter); iter = cmin; } else iter++; } [/code] Basically what I'm doing - load up the entire file into a string and then load some of it into the 'pakiecik' string. The minimal size of loaded information is the length of the shortest code in the code table, if there was no match, we load another character and repeat the search. The znaktabl is a table of 256 ascii characters, so I don't have to cast the index of the 'tablica' table as a character in the loop. It gave me a slight faster run, but still it's not what I want. The 'binbuf' is the main buffer which contains the whole file. I don't like how the whole file sits inside the buffer, but I couldn't come up with something better. We're not talking about compressing files > 10 mb of course, but even for 500kb .jpg picture, the decompression takes about 5 minutes? Could someone help me out, how can I improve this in terms of speed? I just can't come up with a better algorithm :<
[QUOTE=Failure;45100922]We're working on the Huffman's compression implementation. Everything works fine and all, but the decompression gives me a headache. It's working, but it's damn slow for files other than .txt. [code] while(binbuf.length() > 0) { a = 0; pakiecik = binbuf.substr(0, iter); while( ( ( (tablica[a].kod).compare(pakiecik) ) != 0 ) && (a < n) )a++; if( (tablica[a].kod).compare(pakiecik) ) { plikwy << znaktabl[a]; binbuf.erase(0, iter); iter = cmin; } else iter++; } [/code] Basically what I'm doing - load up the entire file into a string and then load some of it into the 'pakiecik' string. The minimal size of loaded information is the length of the shortest code in the code table, if there was no match, we load another character and repeat the search. The znaktabl is a table of 256 ascii characters, so I don't have to cast the index of the 'tablica' table as a character in the loop. It gave me a slight faster run, but still it's not what I want. The 'binbuf' is the main buffer which contains the whole file. I don't like how the whole file sits inside the buffer, but I couldn't come up with something better. We're not talking about compressing files > 10 mb of course, but even for 500kb .jpg picture, the decompression takes about 5 minutes? Could someone help me out, how can I improve this in terms of speed? I just can't come up with a better algorithm :<[/QUOTE] I cannot comment on the algorithm itself, but the two expensive calls you have in there are substr() and erase(). They both cause a lot of reallocation of memory, which can be quite a bottleneck. Maybe you can use two iterators for the start and end of the section you are currently working on instead.
[QUOTE=Exigent;45097448]Hello all. For my Computer Science final (sophomore in high school) we have to make Simon Says. I have the basics down, but I'm lost on how to make "Simon" do his pattern. The only help my teacher gave me on this was that I must use a list, and I've never used a list before. [/QUOTE] Can no one help me with this?
[QUOTE=Exigent;45103487]Can no one help me with this?[/QUOTE] System.Collections.Generic.List<type> :P It's basically an expanding array. What more info do you need on it.
[QUOTE=Exigent;45103487]Can no one help me with this?[/QUOTE] I don't want to give away too much, it's your excercise after all, however understand that everytime you add something to a List<>, you expand it. So a list is something you could loop through, and when you're done looping, you could add one, and loop again, and add one, and loop again. You could create a list by doing [code] using System.Collections.Generic; [/code] and then inside a class [code] List<YourSimonSaysMoveClassType> SimonSaysMoves = new List<YourSimonSaysMoveClassType>(); SimonSaysMoves.Add(AMove); [/code]
[QUOTE=Exigent;45103487]Can no one help me with this?[/QUOTE] You have a list of colours pressed. Every turn you push a new colour to the end of that list, iterate over the list to show the sequence of colours. Then allow the player to interact with one of those 4 colours, every press you move up one in the list, check the pressed colour with the current step in the list. Shouldn't be much more to it?
[IMG]http://i.gyazo.com/824e7b65fb0ce39ab843fef71108b46d.png[/IMG] Why is camera.m_scaleX not evaluating to 1?
[QUOTE=Bladezor;45095951]I'm at work right now so I just punched that out without testing it at all, but when I get home I'll give it a go real quick.[/QUOTE] Hey! Just wanted to let you know my solution works, I just misread what the problem was saying. It's looking for the maximum path for the triangle. So the iterative solution is false and the recursive solution gets the right path. For reference: [url]http://projecteuler.net/problem=67[/url] [url]http://projecteuler.net/problem=18[/url]
Hey, I got a small conceptual problem that I can't figure out due to my inexperience with advanced level programming, I guess. My current project is in UE4, thus the language is in C++. I have a method that monitors for a specific type of user input. The method works by constantly checking for the input condition until it is found with a while loop. The problem is that as soon as it starts checking, the entire engine halts until that loop finishes. I know there is some sort of a proper way of dealing with this, because otherwise game engines would freeze waiting for any other conditions like keystrokes, waiting for trigger boxes to get pressed, etc. How would I better write a method so that it checks for it's condition without freezing the engine? I feel like I need my own type of custom listener or something. Essentially it's just a method with a while loop that performs a function and breaks out when a specific condition is met
[QUOTE=Talkbox;45104035]Why is camera.m_scaleX not evaluating to 1?[/QUOTE] It probably has something to do with floating point numbers and not evaluating to exactly to 1 whereas your printed value is truncated.
[QUOTE=Asgard;45103851]I don't want to give away too much, it's your excercise after all, however understand that everytime you add something to a List<>, you expand it. So a list is something you could loop through, and when you're done looping, you could add one, and loop again, and add one, and loop again. You could create a list by doing [code] using System.Collections.Generic; [/code] and then inside a class [code] List<YourSimonSaysMoveClassType> SimonSaysMoves = new List<YourSimonSaysMoveClassType>(); SimonSaysMoves.Add(AMove); [/code][/QUOTE] I don't know if you're familiar with XNA, but if you are, hopefully you can answer this question as well: I haven't made any classes. Everything I've programmed has been in the "Game1" tab. Do you need a separate class to create a list?
[QUOTE=Exigent;45104514]I don't know if you're familiar with XNA, but if you are, hopefully you can answer this question as well: I haven't made any classes. Everything I've programmed has been in the "Game1" tab. Do you need a separate class to create a list?[/QUOTE] I don't want to sound rude, but has your teacher taught you the purpose of classes?
I'm learning python and having a crack at the Euler problems: For problem 3 where you have to find the prime factors of a number I've got this: [CODE]def prime(x): if x == 0 or x == 1: return False for i in range(x/2,1,-1): if x % i == 0: return False return True i = 131950000 prime_fact = [] for x in range (i/2,1,-1): if i % x == 0 and prime(x): prime_fact.append(x) print prime_fact [/CODE] which works fine for small numbers, but I assume the sort of brute force iterating through every value method I'm using falls apart for bigger values like the one they want you to use. When I tried to get the answer for 600851475143 my computer locked up. Any hints on what direction to go?
[QUOTE=Dienes;45101899]I cannot comment on the algorithm itself, but the two expensive calls you have in there are substr() and erase(). They both cause a lot of reallocation of memory, which can be quite a bottleneck. Maybe you can use two iterators for the start and end of the section you are currently working on instead.[/QUOTE] Thank You! I've fiddled around the iterators a bit and now it's really, really fast. Here's the code if anyone's interested [code] int paklen = 0; int front_iter = 0; int back_iter = 1; while(back_iter != binbuf.npos && back_iter <= binbuf.length()) { a = 0; pakiecik.assign(binbuf.begin()+ front_iter, binbuf.begin() + back_iter); paklen = pakiecik.length(); while( (tablica[a].kod != pakiecik) && (tablica[a].dlugosc <= paklen) && (a < n) )a++; if( tablica[a].kod == pakiecik ) { plikwy << static_cast<unsigned char>(tablica[a].indeks); //binbuf.erase(0, back_iter); front_iter = back_iter; back_iter = back_iter+1; } else back_iter++; } [/code] Once again, thanks :3
[QUOTE=Asgard;45104523]I don't want to sound rude, but has your teacher taught you the purpose of classes?[/QUOTE] Believe me, you don't sound rude. My teacher barely teaches us, I can't remember the last actual lesson he's taught us. All he makes us do is constantly make games. When I ask him the questions that usually winds up here, he tells me "figure it out".
[QUOTE=WTF Nuke;45104301]It probably has something to do with floating point numbers and not evaluating to exactly to 1 whereas your printed value is truncated.[/QUOTE] What the fuck lua... [IMG]http://i.gyazo.com/3776baad708ec09530ed385c5297a7b8.png[/IMG] This works but is there a much better way to do validation like this? Edit: my original post was 1 decimal place accuracy but I changed it to 3 and it works perfectly for what I want it to do...
Instead of formatting string you could do [code] if (N < 0.9999) or (N > 1.0001) then ... end [/code]
Sorry, you need to Log In to post a reply to this thread.