Question about problem using arrays...

Hey guys, thanks a bunch for the help - I always really appreciate how you guys can take the time to help us people still learning about this amazing language. Anyway the problem goes like this:
Given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0's.
makeLast({4, 5, 6}) ?? {0, 0, 0, 0, 0, 6}
makeLast({1, 2}) ?? {0, 0, 0, 2}
makeLast({3}) ?? {0, 3}//this is basically what it's supposed to do
public int[] makeLast (int[] nums)
So I'm just wonder on how the implementation would be like. I know that I would first want to double the array length. After that, I would want to replace every element of the original array with 0's but keep the last element the same. Any suggestions guys?

So I'm just wonder on how the implementation would be like. I know that I would first want to double the array length.Well, you can't double the length per se. You can't dynamically change the size of an array. Rather, you'll need to create a new array with the size as specified by the homework assignment.
After that, I would want to replace every element of the original array with 0's but keep the last element the same.Since it's a new array, you wouldn't actually replace anything.
Any suggestions guys?Hint: the default value, in an array of numbers, is zero.

Similar Messages

  • Another question about problem using arrays

    Hey guys, if you helped me answer my other problems thanks a bunch and again thank you to all the people who try to help us learn the unique language of Java. Anyway the other problem goes like this:
    Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
    countEvens({2, 1, 2, 3, 4}) ?? 3
    countEvens({2, 2, 2}) ?? 3
    countEvens({1, 3, 5}) ?? 0
    Ok so basically I would want to count all the evens in an array and basically if I use the module operator I can figure out whether something is even or not if it returns me a 0. So I know there will be an if statement there. I'm wondering if I would have to also use a "for each" statement to check every element. I would also need a variable to keep count of the number of elements that are even. Any suggestions on how the implementation of this method should be like?

    Hey thanks a bunch actually, I just pretty much reasoned it out and ended up with this:
    public int countEvens(int[] nums) {
    int count = 0;
    for (int e:nums)
         if ((e%2)==0)
            count++;
       return count;      
    }Really appreciate the help.

  • Question about problem with  file using clipping mask

    I am using Photoshop CS6.  I purchased a digital frame with a clipping mask on Etsy.   When I go to insert the picture into the frame using the clipping mask the picture continues to show through the edges of the frame (as shown in the picture).  I have tried locking the layer but it doesn't work.  Any suggestions?

    Try this, move layer 2 below 3 don't clip them.
    With the ellipse selection tool drag out a circle that encompasses the area that the image should show
    Create a mask for the shape (make sure the shape layer is selected {now layer 2 was 3} click on the icon at the bottom of the layers panel that looks like a circle inside a square)
    You should now see your image.
    If you are stuck, capture your screen to show your progress.
    Tips for the selection tool:
    You can hold down the shift key to constrain to a circle,
    You can hold down the alt key to draw a circle selection from its center,
    You can hold down both modifier key for a perfect circle drawn from center.
    Finally you can hold down the space bar to move the selection before letting go of the mouse button.

  • Question about comparing an array of names to a hash table

    I'm still learning Powershell but feel like I have the basics now. I have a new project I'm working on and want some input as the best way to do this:
    The Problem:
    Let's say you have a list of several hundred video game titles and the dollar value of each in a text or two column CSV file.
    The example CSV file looks likes this:
    Game, Price
    Metroid, $15.00
    The Legend of Zelda!, $12.00
    Mike Tyson's Punch-Out!, $18.00
    Super Mario Bros., $16.00
    Kung Fu, $7.00
    You have another list of just the video game titles, this most likely is just a text file with each title listed on its own line.
    The example text file looks like this:
    Kung Fu
    Metroid
    Mike Tysons Punch-Out
    Legend of Zelda
    What I think would happen in the Script:
    Use import-csv and create a hash table that will contain a key = Title and the value = the price.
    Use import-csv and create an array for the title names.
    Foreach loop through each Game Title and match the value against the Hash Table, if there's a match found, put that into another array that will later add all prices of each item and give you a total sum.
    The challenge:
    So far when I try and do one line examples of comparing names against the hash table it seems to only work with exact name matches. In the above example I've purposely made the game titles slightly different because in the real world people just write things
    differently.
    With that said, I've tried using the following single line to match things up and it only seems to work if the values match exactly.
    $hash_table.ContainsKey("Game Title")
    Is there a regex I should use to change the input of the game titles before creating the hash table or doing the compare? Is there another matching operator that is better and matching with String values that have slightly different grammar. An example would
    be the game "The Legend of Zelda". Sometimes people just put "Legend of Zelda", that's close but not exact. I think using a regex to remove extra spaces and symbols would work, but what about for matching of words or letters??
    Any ideas would be very helpful and thanks!

    There's no pat answer for this.
    You can create an array from the hash table keys:
    $hashtable = @{"The Legend of Zelda" = 15.00}
    $titles = $hashtable.getenumerator() | select -ExpandProperty Name
    And then test that using a wildcard match:
    $titles -like "*Game Title*"
    and see if it returns just one match.  If it does then use that match to do your lookup in the hash table.  If it returns 0, or more than one match then you need to check the spelling or qualify the title search some more.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Question about Automator using Spaces.

    I don't think this is the right place for this question, but I figured I am probably going to need to write an applescript to do what I want, so I decided to put it here.
    I have an automation to open some Word documents on my idisk, then open Dictionary, then open Safari with some tabs. But the problem comes in with spaces. I have Word and Dictionary open on Space 1 and Safari open in Space 3. When the automation ends, I end on Space 3 and would like to know how I add a function to set the focus back to Space 1. It's not all that detrimental to the success of my Automation, but I only use Safari for research and would like to focus to end back on word. I have tried switching which apps open first, but I still end up on the 3rd Space.
    My other question is, where can I go for reference to commands in Xcode and AppleScript that deal with Spaces? I love using that function and I have had some ideas about scripts that could help me with them, but I don't even know were I would start with commands.

    There are several solutions to this.
    The first option would be to send the appropriate key command to switch to space 1. If you've left your preferences as the defaults then you can:
    <pre class=command>tell application "System Events" to keystroke "1" using {control down}</pre>
    Which will send a Ctrl-1 and switch to space 1.
    An alternative, which may be even better, is to activate the application you want to be frontmost:
    <pre class=command>tell application "Microsoft Word" to activate</pre>
    This should activate the application and switch to that application's space.

  • Question about planning using serialization point in transaction S2L

    Hello colleagues!
    Iu2019ve try to implement the supply of production line (supply area) just in sequence. R/3 received dependent requirements and backflushes of serialization point behind target Supply Area from APO.
    Then I got a few dependent demands with indicator u201CXu201D in u201Crequirement is serializedu201D field in transaction S2L. I started the automatic planning  but replenishment proposal was created to cover all dependent demands. from the selection horizon  (with ind. u201CXu201D and without ). But I need to cover dep. demands only with ind. X in u201Crequirement is serializedu201D field. I looked through SAP help and SPRO but I couldnu2019t find helpful information.
    Please, help me to find solution.
    Thanks.
    Ivan.

    Thanks for the feedback. I followed the link but I guess I just don't understand about the extended file system. Is HFS+ the same as Mac OS extended (Journaled).
    Basically, yes.
    I understand that I guess that case sensitive means just that, but what is the difference between journaled and extended? Does it mena that a log file is only saved for the journaled system?
    When a filesystem is journaled, all changes to it are written to a log.
    Do most people just format as Extended without journaled? I am about to reformat my iMac and install Leopard on there and I was wondering if I should format using the extended or the extended with journal?
    Using the journaling option slightly decreases the amount of space on the drive usable by you and increases the chance of recovering data from the drive if a problem occurred. If you want, you can use the Disk Utility to turn it on and off without erasing the drive again.
    (25598)

  • Question about very big arrays

    if i want to make a 2dimentional aray that maybe very
    large it may get up to 1000 elements or even more and its size
    may vary at runtime
    question
    1)what do i need to use[b] ?
    2)does anyone have a example code they could post[b] ?
    3)if their isnt a way to do a 2darray at runtime
    how would i do i 1dimentional array that will vary in size at runtime[b] ?
    my goal at this time (for example) is to make a 2dimentional array
    called say RTarray [] []
    during runtime it will increment the size of the array
    from [1 to 1000] ,[2] then decrement it back down to 1
    ...(each of the array elements might be set to 1 or)
    ...(anything thats not really whats important )
    thanks in advance lightwave

    You can try using a variable to set the array at runtime,
    one that varies depending on incoming data:
    protected int DATA_FIELD = IncomingData.length();
    protected int[][] array = new array[DATA_FIELD ][2];Don't know if that's what your looking for?

  • Question about forum use regarding an error I am receiving

    I am looking for an answer to the proper process I should follow when running into issues with Azure Storage. As I don't want to annoy folks with tossing questions into this forum regarding errors I'm running into that are more usage related instead
    of technical in scope.
    For instance this morning I was attempting to view some details about a particular Storage item and was greeted with the following screen in the Azure Portal.
    I was able to accomplish what I needed to (downloading some files out of this particular storage account) within the Manage.WindowsAzure.com site without issue. So this seems to be connected to the portal itself and it's interactions with Storage
    (perhaps). I now have access to the above Storage item via the portal site, but for about 15 minutes I continually received the message above. Even after logging out, clearning cache, trying different browsers etc.
    So my question(s) are; Are these forums the proper place to be posting information about issues we run into? If not what is the preferred method for reporting items like those above and getting help with remediating them in a timely manner?
    I'm happy to put them here, just want to make sure there isn't a better place as I'm new to this.

    Hi Darren,
    You can post your issue with Azure Storage in here. Just make sure that you do not give out any PII data.
    Also, the above portal which you are viewing is still in preview. You may use "manage.windowsazure.com" portal to access your resource.
    Regards,
    Manu

  • Question about problem with HP LaserJet P1102

    Hello,
    My name is Mariya. When we bought our printer I set it very good and it worked properly. The problem is sometimes I bring it to duty and use the printer there. One day I could not set the printer on my job and did something where was written to do the changes manually. The printer had worked then. The problem is when I came back at home and when I decided to print on my HP printer
    1. It became so **bleep** slow
    2. SInce then when I want to print something, before printing there is a sign said "HP printer is in an error state" and even though 10 sec. later the printer print the desired. 
    3. When the printer is pluged on every 10 sec. there is a strange sound which it comes out, I don't know what is it. 
    I hope you can hel me!
    Thank you in advance!

    Hi @mariatsvet,
    I see that you are having issues with the printer printing slow and it is making a strange sound. I will certainly do my best to help you.
    Make sure the printer is connected directly to a wall outlet. (don't use a power hub or a surge protector) This ensures the printer is receiving full power and may help this situation.
    Remove and reinsert the toner.
    Try and print a hardware self test on the printer to find out if it is a hardware or software issue.
    Printing a Configuration Page.
    Restart the Print Spooler and clear out the spool folder.
    Hold down the Windows Logo key () on the keyboard and the 'R' to open the run box, type Services.msc .
    Double-click Printer Spooler in the Services list.
    Click on the Stop button and click OK.
    Hold down the Windows Logo key () on the keyboard and the 'R' to open the run box, type"%WINDIR%\system32\spool\printers, then delete all files in this folder.
    Go back to services by holding down the Windows Logo key () on the keyboard and the 'R' to open the run box, type Services.msc again.
    Double-click Printer Spooler in the Services list.
    Click on the Start button. In the Start up Type list, make sure that Automatic is selected and click OK.
    Try printing again.
    Did the hardware test page print fast?
    Let me know how the printer connected so I can provide the proper instructions. (USB/Wireless)
    If you need further assistance, just let me know.
    Have a great day!
    Thank You.
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos Thumbs Up" on the right to say “Thanks” for helping!
    Gemini02
    I work on behalf of HP

  • Question about the use of JMS connections

    Good Morning,
    I'm a new user of JMS technology. I'm having some problems with the performance of my application. Specifically, i'm developing a 24x7 application. We are using MessageListener for all consumers.
    Doing performance and continuity tests I detected memory leaks in certain JMS classes. After passing a profiler tool, I saw a large amount of ObjectMessage and Message instances. We are not closing the session, MessageListeners, MessageConsumers or Connetion instances because we want to reuse them all the time, and we can't close them because we lose the high availability service.
    Reading, the JMS tutorial, I'm saw this paragraph in the "Connection" section:
    +"When an application completes, you need to close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider. Closing a connection also closes its sessions and their message producers and message consumers.+
    +queueConnection.close();+
    +topicConnection.close();+
    +Before your application can consume messages, you must call the connection's start method; for details, see Section 3.5, "Message Consumers." If you want to stop message delivery temporarily without closing the connection, you call the stop method."+
    Should I close the connection as the last paragraph say?. If so, I don't know how to do it without lose the service, because if I call "close" method on the connection and immediatelyi start it, i will lose all my consumers and producers for a little time.
    Any suggestion?
    Thank you in advance,
    Regards

    1002099 wrote:
    +"When an application completes, you need to close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider. Closing a connection also closes its sessions and their message producers and message consumers.+
    +queueConnection.close();+
    +topicConnection.close();+
    +Before your application can consume messages, you must call the connection's start method; for details, see Section 3.5, "Message Consumers." If you want to stop message delivery temporarily without closing the connection, you call the stop method."+
    Should I close the connection as the last paragraph say?. If so, I don't know how to do it without lose the service, because if I call "close" method on the connection and immediatelyi start it, i will lose all my consumers and producers for a little time.
    You should close a connection when you have no further use for it as this will release resources.
    You must start a connection if you want to receive messages from it.
    Most applications don't use stop(). However if your application needs to temporarily pause delivery of messages (to the application) for some reason, call stop(). When your application wants delivery of messages (to the application) to restart, call start() again.
    Nigel

  • Qosmio G40-10Z: Question about energy use in sleepmode

    When I turn my new Qosmio G40-10Z in sleeping mode, with full battery (and off the AC power), I can't start the computer next day. I can only start it up after connecting it to AC power. I get a message windows Vista wasn't closed properly and I am asked to start in normal way or safe mode.
    After starting the battery indicates only 2% power. So, what is using power in sleeping mode? Why isn't the sleep mode not transferred to hibernation state when the battery is almost empty?
    (New notebook, only vista and office installed and only television antenna is connected)

    Hello guys
    At first I just want to write a little bit about theory. Microsoft has implemented standby/Hibernation mode to offer PC users comfortable OS usage and make switch ON/OFF time much shorter.
    According to the definition standby mode should be used for short time (launch, meeting and if you must leave the notebook for few hours). Hibernation mode should be used if you leave the notebook over the night or maybe day or two. In standby mode all data will be written in RAM and to keep them notebook needs power. In Hibernation mode all data will be saved on to HDD and notebook can be switched OFF. In hibernation mode the notebook does not need permanent power supply.
    For me it is really strange that you use standby mode with battery and almost 20 hours. With this I do not want to say that described battery issue is normal but for 20 hours it is logic to use hibernation mode.
    Back to battery issue: only 20 hours under standby mode is really to short. I have tested it with my Tecra and after 5 days I was able to switch ON notebook as usual (only battery power supply.
    In your case I see only two explanations:
    1. Somehow notebook can not switch into standby mode properly and stays running (I do not believe this because you can see if something like this happen)
    2. The battery is defective
    What you should do is to contact Authorized service partner in your country. Make a call and explain the situation. Maybe you will get new battery or they should test it using your battery.
    Sorry for this long posting. I wish you luck and hope you will clarify this issue very soon.
    Bye and good luck!

  • Question about Parallels using Bootcamp partition

    I was about to install XP on bootcamp but i would mainly be using parallels to run XP from the bootcamp partition. What i was wondering is that if i was to make any changes to xp like install a software and create a new folder through parallels, would it reflect on the actual bootcamp partition when i load into the actual XP partition using bootcamp? Basically, do the changes made to windows using parallels carry over to the bootcamp partition given you are using the bootcamp partition on parallels?
    Thanks

    Thanks a lot. While i am at it, if i use the bootcamp partition with Parallels, i cannot suspend the VM right?
    Parallels will behave the same, whether it has its own copy of Windows, or is using that on the Boot Camp partition.
    The only difference is backup. If you use Parallels with Windows in a disk image file (Preferably a sparse bundle), Windows can be backed up with Time Machine. If Windows is on its own partition, Time Machine can't handle it.

  • HT2506 Questions about Preview Using in Safari after Installing Adobe Reader

    I wonder how could I reuse Preview to open PDFs on Safari, after I installed the **** stupid Adobe Reader. Now I can't read and download online PDFs just because I got the Adobe Reader, even if I uninstalled the Adobe reader. What should I do?!!!!

    I've been having this same problem with saving pdf files File - Save As from the top Toolbar on my laptop.
    I would constantly get messages - There was an error opening this document. The file is damaged and could not be repaired.
    I also downloaded the lateset version of Adobe Reader XI
    But at last I have managed to find a simple way to access pdf files from the internet.
    Firstly, without downloading the pdf, I printed the pdf file direct from the Safari page by using the Print icon on the grey lozenge that appears centrally three quarters down the screen.
    Then I discovered that if I save the pdf using the Save icon from this same grey lozenge on the Safari page I can open and print this pdf version from my desktop.
    I've tested it with a couple of different pdf files and these methods work using the grey lozenge icons but the same pdfs files don't work using File - Save As on the Tool Bar.

  • Questions about Military use

    Hey all, my wife is going on her second deployment to Kosovo and this time we were told they have WIFI in their rooms! Awesome! So through the wonders of ebay I bought us both iSight cams for our macs. They work great with iChat.
    I was wondering if there are any issues we should look for? Or maybe a different messenger service we should have as a back up?
    Anyone have any experience with this?
    Thanks in advance!
    -Stephen

    Hi,
    Like CodLBi has said it is going to be about Firewalls or at least which ports the LAN has open and the Connection speed and the Bandwidth share that are going to effect this the most. (Not to mention any "Security" concerns about apps that can send files and Pics)
    If it is the Miltary that control the LAN and Internet link then they might move on the ports (they have in the Gulf).
    As to Bandwidth that will depend on how many other people are using the LAN At them same time and what they are doing on-line.
    I presume it is not the Mac Pro she is taking
    That could run Windows XP with Boot Camp or Paralleles and would be one way round as AIM on a PC moves it's ports around and that may have provided a solution.
    See the links 5-9 on this section of my page 5.
    10:56 AM Friday; June 29, 2007

  • Some questions about the use of the relay

    I connect Relay coil  to the AC power source,but  the switch always act.Is there any suitable relay for ac power ?
    and when I replaced time delay relay in simulation, there was an error.  I don't know how to get the right parameter setting ?
    please help.
    Attachments:
    time_delay relay.png ‏5 KB

    Hi there,
    Thank you for your question. There are two ways to do this, for your origianl circuit you can connect the time delay relay in this way:
    Set the frequency of the source to 1Hz and simulate, then you can see the NC and NO switch back and forth.
    Second method: Instead of using a AC relay, we can use an AC coil which can be found at Electro_Mechanical-->COILS_RELAYS-->ENERGIZING_COIL_AC
    In addition, we can add a normally closed (NC) contact or a normally open(NO) contact spicified for this AC coil(say K1)
    Just type RELAY_CONTACT_NC or RELAY_CONTACT_NO to place the components and then place them under K1.
    Change the Controlling device reference as shown in the picture below,
    Now the AC relay can be constructed in this way.
    For time delay, we can use TIME_DELAY_NC and TIME_DELAY_NO contact
    Hope these two methods help!:-)
    Best,
    Chen_T
    National Instruments

Maybe you are looking for