The k largest elements of M, I'm stuck with it (rookie)!

Hello!
I have to write a program, which first reads an int k, than reads a row of ints M, until 0 is entered. Than the program must show the k largest elements of that row M. For example: I enter 4 for k, and enter 3, 6, 12, 8, 16, 17, 9, and in the end 0 to stop the input. Finally the 4 largest elements 9,12,16 and 17 should be shown. I have to write this program for homework and I am stick with it since yesterday. I am trying to create an array in which the numbers should be stored, and than another array with the same components, where they will be sorted. Than the last k sorted numbers should be displayed. I used couple of books and lectures in which the sort function was explained and I came up with this code:
public class Max extends MiniJava {     
public static int[] sort (int[] a) {
          int n = a.length;
          int[] b = new int[n];
          for (int i = 0; i<n; ++i)
          insert (b, a, i);
          return b;
     public static void insert (int[] b, int x, int i) {
          int j = locate (b,x,i);
          shift (b,j,i);
          b[j] = x;
     public static int locate (int[] b, int x, int i) {
          int j = 0;
          while (j < i && x > b[j]) ++j;
          return j;
     public static void shift (int[] b, int j, int i) {
          for (int k = i-1; k >= j; --k)
          b[k+1] = b[k];
This is just the "sorting" part I have to make it show the k largest elements and I just can't do it. We are currently using the MiniJava class, because we are in the very very beginning and it should be easier with it. Here is a link to it http://savefile.com/files/255814
If anyone can help, I will be very grateful. Tnx in advance!

The input and sorting didn't actually worked, so I decided to try with a different approach. Here is the code now:
public class test extends MiniJava {
public static void main(String[] args) {
          int[] a;
          int k=read();
          a = new int[k]; // a = k groessten Zahlen, Anlegen des Felds
          int x; // x - unsere Zahl;
while (x != 0){
x = read();
public void store (int x, int[] a){
     int b = 0; // der kleinste Zahlindex
int m; //die kleinste Zahl
for(int i = 0; i < 0; i++)
int c = a;
if(m >= c)
m = c;
b = i;
if(x >= m)
a[b] = x;
     public void print (int x, int[] a){
for (int i=0; i <a.length; i++)
write (a[i]);
The problem is that when I try to compile it i get 3 errors:
variable x might not have been initialized while (x != 0) {
variable m might not have been initialized if (m>=c)
variable m might not have been initialized if (x >= m)
Where is the mistake? Is it in the declaration or is it in the store function?

Similar Messages

  • Trying to download the trial version of acrobat pro dc.The installer's says downloading but it's stuck with 3% for the last 30 minutes. What to do?

    trying to download the trial version of acrobat pro dc.The installer's says downloading but it's stuck with 3% for the last 30 minutes. What to do?

    Try downloading from below link :
    https://helpx.adobe.com/acrobat/kb/acrobat-downloads.html
    Click on Acrobat pro DC option and then click on the option : File 1 of 1 (544 MB)

  • HT2204 I have recently immigrated to the US but my apple ID is still stuck with my previous country. How do I change the country? Preferably without using a credit card

    I have recently immigrated to the US but my apple ID is still stuck with my previous country. How do I change the country? Preferably without using a credit cardI've only been here in the US 2 months and still have no US credit card.

    Your credit or debit card credentials must be associated with the same country where you reside.
    "Although you can browse the iTunes Store in any country without being signed in, you can only purchase content from the iTunes Store for your own country. This is enforced via the billing address associated with your credit card or other payment method that you use with the iTunes Store, rather than your actual geographic location."
    From here >  The Complete Guide to Using the iTunes Store | iLounge Article

  • Converted the id3 tags of my entire library, now stuck with duplicates of everything. can i sort by id3 tag version or something to easily delete duplicates?

    help!!!
    i have about 6000 tracks that just became 12000

    Hold down the SHIFT key and then select File > Display Exact Duplicates. This gives a list of all tracks where more than one track has essentially the same metadata, name, track no., artist, album, etc. Actually, if you know everything has been doubled then I guess you skip that step...
    If the duplicates have been recently added into your library as a job lot then you can sort the list on the date added column, select a range of tracks with SHIFT-click and delete all the recent entries. Please check however that there really are two copies of each file before deciding whether to delete the files as well as the entries from the database - get it wrong and you could delete your entire library!
    For more thoughts on this topic see this recent post.
    tt2

  • I just signed up for Verizon 45 days ago and am unhappy with my service? I have decided to switch to another service provider. Are there any other cancellation fees on top of the $350 per line fee? Am I stuck with the phone?

    I just signed up with Verizon 45 days ago and am unhappy with my service. I know about the ETF fee of $350/ per line but are there any other fees I will need to pay for the actual phone.

    There are no other fees. You are not stuck with the phone, you can do whatever you would like to do with it.

  • HT201210 I was doing an update on my iphone and it stopped short of getting it all the way done. Now my phone is stuck with an itunes icon on the screen, and will not allow me to do anything with it. I tried shutting it down and restarting it...same icon

    I tried to update my phone. The update didnt go all the way through, and ended up freezing the screen on my iphone with an itunes icon on it. I tried to reboot phone with no success...the icon stays on there with no other function of the phone available. Anybody know of a fix?

    Put your iphone in dfu mode by doing this. Turn your iphone off. When its off, hold home and power button together for exactly 10 seconds, then release the power button and keep holding home button for another 10 seconds. Then open up itunes on your pc and plug your iphone to your pc. Itunes should recognize your iphone in recovery mode and it will tell you to restore it. Click restore, and your iphone will restore. Remember that, if you do this you will lose everything that was on your iphone. You can restore from backup later on. Hope I helped. If you have any questions or problems with this tutarial, you can contact me personaly on my email [email protected]   . Hope I helped. Cheers

  • I used the internet data recovery because my mac is stuck with the grey screen + apple logo at startup. When I tried to reinstall the OS, it prompted: you can't upgrade to this newer version of mac os x is installed. What do I do? x

    I have no backup of my files because my mac suddenly stopped working.
    I'm using a macbook air 11" mid-2012 mac os x 10.8.5.
    Please help

    Try using Internt Recovery.
    http://osxdaily.com/2014/12/14/reinstall-os-x-mac-internet-recovery/
    When you visit the site page click the 3½ bars gray Reader icon left of the url in the address bar for easy viewing.

  • While downloading the latest download for my iphone it is stuck with a plug in and itune logo on the screen-help[!

    White downloading  ios 8.1.1 the screen frozen on a picture of a plugin device and the itune logo.  Iphone 4s

    You phone is in Recovery Mode.
    Here is the apple support page to help you out.
    If you can't update or restore your iPhone, iPad, or iPod touch - Apple Support
    PJRS

  • Finding the 25th largest number in an array

    How would I say find the 25th largest number in an array?
    Or nth largest number?
    Chris

    I find most of these answers correct, but without previous knowledge of the size of the array, I would do things differently.
    In the enclosed VI, a cluster is formed of all array values and their index. When the array is then sorted, the first cluster element is used as the first sort criterium, then the second cluster element. This allows a single array operation (Sorting) to take care of any searching, whilst the cluster retains the index of the original array.
    Coupling this method with an unitialised shift register, you can also cache the sorted array to allow for more rapid processing if you require more than a single indexed value.
    Another way to achieve this is (Assuming you`re seeking the 25 largest) to simply get the max and min at t
    he very beginning, feed the array into a loop with a shift register, set the value at the index returned for max each run to the initial min value -1, and repeat another 24 times.
    Regarding memory use and execution speed, I`m not sure if searching an array 25 times is quicker than a single sort or not (No new array is created), but both versions are viable.
    Shane.
    Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
    Attachments:
    Get_Nth_largest-smallest_(6.1).vi ‏31 KB
    Get_Nth_largest-smallest_(6.1)_-_version_2.vi ‏42 KB

  • When I bootup the icon for Elements shows up in orange.  It changes to green only when I've gone to Finder and clicked on Elelments under the Devices list.  This is a recent development.  The icon used to be green upon bootup.  Does anyone know why ?

    When I bootup the icon for Elements shows up in orange.  It changes to green only when I've gone to Finder and clicked on Elelments under the Devices list.  This is a recent development.  The icon used to be green upon bootup.  Does anyone know why ?

    I'm sorry to hear that.
    I'm not affiliated w/ the developer, just a happy user that gave up fighting the apple podcast app a while ago.  I used to have a bunch of smart playlists in itunes for my podcasts, and come home every day and pathologically synced my phone as soon as I walked in the door, and again before I walked out the door in the morning.
    Since my wife was doing this too, we were fighting over who's turn it was to sync their phone.
    Since I've switched to Downcast, I no longer worry about syncing my phone to itunes at all.  I can go weeks between syncs.
    Setup a "playlist" in downcast (ex., "Commute") and add podcasts to that playlist.  Add another playlist ("walk" or "workout") and add different podcasts to that one. 
    Set podcast priorities on a per-feed basis (ex., high priority for some daily news feeds, medium priority for some favorite podcasts, lower priority for other stuff).  Downcast will play the things in the priority you specify, and within that priority, it will play in date order (oldest to newest).
    Allegedly, it will also sync your play status to other devices, although that is not a feature I currently use and can't vouch for.  It uses apple's iCloud APIs, so to some extent may be limited by what Apple's APIs can do.

  • Can you use more than one computer to edit the same Premiere Elements Project?

    I am using Premiere Elements 10.  I have built a new project on my desktop computer because the desktop has a firewire port to capture video from my camcorder and my laptop doesn't have a firewire port.  The project is stored on my home network.  I seem to be able to do editing fine using the desktop computer that the project was created on, but it won't load up right on the laptop for editing.  Premiere Elements is configured the same on both computers.  I guess my question is - does Premiere Elements store something on on the computer that the project was created on that is not being stored on the network drive?    How can I fix the problem so that I can edit on my laptop? The laptop is a top of the line Dell XPS with an i7 processor that I use for other Premiere projects, so the laptops ability is not an issue.
    Thanks
    Bill

    Yes, having files stored on a HDD, but having the letter change between machines is a problem. The Project file (PREL) is only an XML database, with links to the Assets. Those links are Absolute Paths, so if anything changes, from drive letter, to file name, the link is broken.
    I have over a dozen externals, that I use to migrate Projects between my main two computers. Each has had its drive letter set in OS of each computer. I started with Z:\ and worked down. Each drive has a lable on it with the designated drive letter. Unfortunately, I ran out of letters, as the workstation has a ton of HDD's and burners, so I had to start over. That does mean that I cannot have both Z:\ drives plugged in at the same time, but that has never been an issue. I wish that Windows would allow for double drive letters, say ZZ:\, but still, I have a handful of letters to use - just too many externals for those letters...
    Happy editing,
    Hunt

  • How can i sort out the N lowiest elements from an 1D array faster than using the build in sort function (1D-array) in Labview?

    I need an algorithm that is based on the same sorting-algorithm that Labview uses (or one that is just as fast) but it only needs to sort out the N lowiest elements (and the N-element output array don't need to be sorted).
    /Jonas

    I want all three zeros in the output. You can se the algorithm that I'm looking for as a soft version of Labviews own sort algorithm that only gives you the N (always less then the length of the array) lowiest values as output.If two elements contain the same small value both should be sorted out.
    /Jonas

  • How to get the number of elements in a tilelist

    I'm writing test cases for a flex application using FlexSelenium and I need to get the number of elements displayed in the tilelist.
    I know that that the tilelist is fed by an array. but i can't access that array from my test case.
    Can anyone tell me how I can get the number of elements in a tilelist on the application? which property of the tilelist can provide that number?

    try tileList.dataProvider.length;

  • How to get the number of elements in DB

    Could you tell me how to get the number of elements in DB??
    My code to get the number is below, but I think it is not efficient
    DB->cursor(DB, NULL, &cursorp, 0);
    while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0)
    count++;
    --------------------------------------------------------------------------------------------------------------

    Hi,
    The most efficient way to get a count from the database is using the DB->stat API (http://docs.oracle.com/cd/E17076_02/html/api_reference/C/dbstat.html)
    The code will be something like:
            DB *dbp;
            DB_BTREE_STAT *statp;
            int ret;
            /* Print out the number of records in the database. */
            if ((ret = dbp->stat(dbp, NULL, &statp, 0)) != 0) {
                    dbp->err(dbp, ret, "DB->stat");
                    goto err1;
            printf("%s: database contains %lu records\n",
                progname, (u_long)statp->bt_ndata);
            free(statp);The code comes from the example included in the distribution at examples/c/ex_btrec.c
    If the database isn't a btree, you should update to the appropriate statistics structure and field.
    It's worth noting that retrieving the count is never a "cheap" operation. The count is not stored in the database - since doing so introduced a single point of contention that creates a bottle neck.
    Regards,
    Alex Gorrod
    Oracle Berkeley DB

  • Using the value "Image/*" for the accept attribute of the HTML input Element, how can I add .pdf to the array of preconfigured file types (.jpe, .jpg, .jpeg, .?

    On a form, using the value "image/*" for the accept attribute of the HTML input Element, how can I add .pdf to the array of pre-configured file types (.jpe, .jpg, .jpeg, .gif, .png, .bmp, .ico, .svg, .svgz, .tif, .tiff, .ai, .drw, .pct, .psp, .xcf, .psd, .raw)?
    Say I wanted to add .gif, .jfif or .ico. I find this array limited, how can I add types to image?
    <input type="file" name="file" accept="image/*" id="file" />
    mimeTypes.rdf does not seem to allow this.

    ''mimeTypes.rdf'' has nothing to do with web development. It's a file that stores your file handling preferences (e.g. if you want ZIP files automatically saved or opened).
    You can't change the file types of the pre-defined content specifiers (audio/*, video/*, image/*), but you can specify additional MIME types. To add PDF to your above example,
    <pre><nowiki><input type="file" name="file" accept="image/*,application/pdf" id="file" /></nowiki></pre>
    For details, see
    * [https://developer.mozilla.org/En/HTML/Element/Input developer.mozilla.org/En/HTML/Element/Input]

Maybe you are looking for

  • Follow up activities after SAP (CU&UC) upgrade from SAP 4.6c to ECC6 EHP4

    Dear Friends We have completed SAP (CU&UC) upgrade from SAP 4.6c to ECC6 EHP4. We refereed the upgrade guide for follow activities and we required confirmation before performing the activities. Could you please help us what needs to be done immediate

  • How do I use an "Other" e-mail account on the iPhone?

    I have a Bellsouth/AT&T e-mail account, and I want to use that as my e-mail, but I don't really understand how. Please help me out.

  • Safari and firefox crashes and others

    I got my Macbook pro last week I have been using safari and it crashes randomly or gets stuck then I tried using firefox and its the same. I was using speech recognition and it crashed Why is this happening and how can I fix it, do you think its a sy

  • Costing Log in production order creation

    Dear All, I'm having a material with a component which we do not cost. Component material has marked 'do not cost' tick box in costing 1 view in material master. Costing for the header material is done using CK40N after attaching BOM and routing. How

  • Powerbook Duo 2300c not booting after installing 7.5.3 from Apple

    I downloaded the 7.5.3 software from Apple and installed it on my 2300c. But on restart it says "This startup disk will not work on this Macintosh model. Use the latest Installer to update this disk for this model.". According to the knowledge base 7