Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing out an array in order without methods

I'm a new coder, and my class has recently gone about learning arrays. I've been slaving over this one program assignment for the whole day. It's not due for a few days, but I'm incredibly frustrated over it.

We have to print out a list of test scores and the pertinent ID that is associated with it. We have to order the test scores from highest to lowest, but without modifying the array whatsoever--so no Array sorting, or any of the like. The "suggested solution" says:

  1. Find the largest score, keeping track of its subscript.

  2. Print this score along with the corresponding ID.

  3. Set that score equal to its opposite (That makes it the smallest score!).

  4. Repeat steps 1-3 until all 21 scores have been processed.

  5. Stop and print an appropriate message.

I've been trying to adhere to these instructions, because there are other programs for the rest of the week which will rely on this. However, my approach is flawed, because my code prints that the maximum score is 110, 238 for 26 iterations. I'm having awful trouble with this code, it's really frustrating me. This is what I have so far:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Prog408a {

    static Scanner inFile = null;
    static Scanner inFile2 = null;

    public static void main (String[]args) {

        try {

            // create scanner to read file
            inFile = new Scanner(new File ("prog408a.dat"));

        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            System.exit(0);
        }

        int [] ids = new int[26];
        int [] scores = new int[26];

        int first = 0;
        int second = 0;

        int counter = 0;

        int max, maxid, supermax;

        do {

            ids [first] = inFile.nextInt();
            scores [second] = inFile.nextInt();

            first++;
            second++;

        } while (inFile.hasNextInt());


        /* initial values of max, maxid */



        System.out.println("ID\t\tScore");

        maxid = ids[0];
        supermax = scores[0];
        max = scores[0];
        int index=0;

        while (counter < scores.length) {

            for (int x = 0; x < scores.length - 1; x++) { // scroll through the indexes.

                if (scores[x] > max) {

                    max = scores[x];
                    index = x; // keep the index of the biggest number.

                } 

                max = -1 * (scores[index]);

            }

            System.out.println("The maximum score is " + ids[index] + ", " + scores[index]);
            counter++;



        }




    }

}

This is what is in the data file.

365 265
222 223
306 262
003 559
004 560
203 224
113 243
208 242
213 229
115 257
302 242
223 230
001 599
311 256
323 245
321 245
123 253
104 239
002 513
112 239
207 228
325 246
116 246
218 243
110 238

Excuse me if there are any formatting errors in the post.

like image 829
Nikolas Avatar asked Apr 28 '26 01:04

Nikolas


1 Answers

while (counter < scores.length - 1) {
        for (int x = 0; x < scores.length - 1; x++) { // scroll through the indexes.
            if (scores[x] > max) {
                max = scores[x];
                index = x; // keep the index of the biggest number.
            } 
        }
        System.out.println("The maximum score is " + ids[index] + ", " + scores[index]);
        max = -1 * (scores[index]);
        scores[index] = -1 * (scores[index]); // change the value in the original array so it won't find the same max again
        counter++;
}

Output:

ID      Score
The maximum score is 1, 599
The maximum score is 4, 560
The maximum score is 3, 559
The maximum score is 2, 513
The maximum score is 365, 265
The maximum score is 306, 262
The maximum score is 115, 257
The maximum score is 311, 256
The maximum score is 123, 253
The maximum score is 325, 246
The maximum score is 116, 246
The maximum score is 323, 245
The maximum score is 321, 245
The maximum score is 113, 243
The maximum score is 218, 243
The maximum score is 208, 242
The maximum score is 302, 242
The maximum score is 104, 239
The maximum score is 112, 239
The maximum score is 110, 238
The maximum score is 223, 230
The maximum score is 213, 229
The maximum score is 207, 228
The maximum score is 203, 224
The maximum score is 222, 223

You might want to add a little function that will print the id < 100 with additional zeros at start.

like image 199
Tony Babarino Avatar answered Apr 30 '26 13:04

Tony Babarino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!