Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I: Create and p rint out the two arrays : (Be sure to do this first) You ar

ID: 3688604 • Letter: P

Question

Part I:

Create and print out the two arrays: (Be sure to do this first)  You are allowed to hard code these arrays into your program. You can also put the data into a file and read the information into the program. The data is as follows:

Anne130

Bob250

Ralph305

Tim225

Barbara85

Jane160

Steve180

Tom200

Mike165

Shirley155

Pam125

Frank120

Part II:  The elevators in our building have an 1100 lb. load limit. Determine which people in the list above get on the elevator. Print their names, weights, total weight, and how many got on.

Part III:  Rearrange these people in descendingsequence by weight and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Part IV:  Rearrange these people in ascendingsequence by name (USE A DIFFERENT SORTALGORITHM THAN THE ONE YOU USED IN PART III) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Part V:  Have the program determine which method allowed the most people to get on the elevator.  The program should compare the three different counts of how many people got on the elevator.

This program should include:

Make sure all methods are writen to handle n elements, not just 12. You can pass 12 from main to n in the method/function.

Turn in: The assignment sheet, algorithm for main, source code, output and Academic Honor Promise.

EXTRA CREDIT: Read the data from a file and put the results on an output file. Print the output file using Notepad or some other editor.

Explanation / Answer

Hi below i have writen the sample code in Java using knapsack methodology which helps you to understand and solve the problem, Logic remains the same and you can easily convert the syntax to C++ for your reference.

Java Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

1100 12

1 30

1 150

1 305

1 225

1 135

1 160

1 80

1 200

1 165

1 90

1 100

1 120

The first line contains the capacity of the elevator and the number of persons. The following lines are the values of the persons and their weight. I saved this file in c:/tmp/data.txt

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Knapsack {

    private Item[] items;

    private int nofItems, capacity;

    private void initialize() throws FileNotFoundException {

        Scanner input= new Scanner(new File("c:/tmp/data.txt"));

        capacity= input.nextInt();

        nofItems= input.nextInt();

        items= new Item[nofItems];

        for (int i= 0; i < nofItems; i++)

            items[i]= new Item(input.nextInt(), input.nextInt());

        total= 0L;

        cut= 0L;

    }

    private Solution solve(int allowed, int cap) {

        Solution take, dontTake;

        if (allowed < 0)

            return new Solution(nofItems);

        total++;

        dontTake= solve(allowed-1, cap);

        if (items[allowed].weight > cap) {

            cut++;

            return dontTake;

        }

        take= solve(allowed-1, cap-items[allowed].weight).take(items, allowed);

        return (take.value > dontTake.value)?take:dontTake;

    }

    private long total;

    private long cut;

     

    public static void main(String[] args) throws FileNotFoundException {

        Knapsack k= new Knapsack();

        k.initialize();

        Solution b = k.solve(k.nofItems-1, k.capacity);

        System.out.println("Total Value: " + b.value);

        System.out.println("Total Steps: " + k.total);

        System.out.println("Total Cuts : " + k.cut);

        int w= 0;

        for (int i= 0; i < k.nofItems; i++) {

            if (b.taken[i]) {

                System.out.println(i + ": " + k.items[i]);

                w+= k.items[i].weight;

            }

        }

        System.out.println("Total weight: "+w);

    }

}

class Solution {

    boolean[] taken;

    int value;

    Solution(int nofItems) {

        this.taken= new boolean[nofItems];

    }

    Solution take(Item[] items, int rank) {

        this.value+= items[rank].value;

        this.taken[rank]= true;

        return this;

    }

}

class Item {

    int value, weight;

    public Item(int value, int weight) {

        this.value= value;

        this.weight= weight;

    }

    public String toString() {

        return value + " " + weight;

    }

}

solution:

Java Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

Total Value: 9

Total Steps: 3526

Total Cuts : 342

0: 1 30

1: 1 150

3: 1 225

4: 1 135

5: 1 160

6: 1 80

9: 1 90

10: 1 100

11: 1 120

Total weight: 1090

The elevator is nearly full

1

2

3

4

5

6

7

8

9

10

11

12

13

1100 12

1 30

1 150

1 305

1 225

1 135

1 160

1 80

1 200

1 165

1 90

1 100

1 120

The first line contains the capacity of the elevator and the number of persons. The following lines are the values of the persons and their weight. I saved this file in c:/tmp/data.txt

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Knapsack {

    private Item[] items;

    private int nofItems, capacity;

    private void initialize() throws FileNotFoundException {

        Scanner input= new Scanner(new File("c:/tmp/data.txt"));

        capacity= input.nextInt();

        nofItems= input.nextInt();

        items= new Item[nofItems];

        for (int i= 0; i < nofItems; i++)

            items[i]= new Item(input.nextInt(), input.nextInt());

        total= 0L;

        cut= 0L;

    }

    private Solution solve(int allowed, int cap) {

        Solution take, dontTake;

        if (allowed < 0)

            return new Solution(nofItems);

        total++;

        dontTake= solve(allowed-1, cap);

        if (items[allowed].weight > cap) {

            cut++;

            return dontTake;

        }

        take= solve(allowed-1, cap-items[allowed].weight).take(items, allowed);

        return (take.value > dontTake.value)?take:dontTake;

    }

    private long total;

    private long cut;

     

    public static void main(String[] args) throws FileNotFoundException {

        Knapsack k= new Knapsack();

        k.initialize();

        Solution b = k.solve(k.nofItems-1, k.capacity);

        System.out.println("Total Value: " + b.value);

        System.out.println("Total Steps: " + k.total);

        System.out.println("Total Cuts : " + k.cut);

        int w= 0;

        for (int i= 0; i < k.nofItems; i++) {

            if (b.taken[i]) {

                System.out.println(i + ": " + k.items[i]);

                w+= k.items[i].weight;

            }

        }

        System.out.println("Total weight: "+w);

    }

}

class Solution {

    boolean[] taken;

    int value;

    Solution(int nofItems) {

        this.taken= new boolean[nofItems];

    }

    Solution take(Item[] items, int rank) {

        this.value+= items[rank].value;

        this.taken[rank]= true;

        return this;

    }

}

class Item {

    int value, weight;

    public Item(int value, int weight) {

        this.value= value;

        this.weight= weight;

    }

    public String toString() {

        return value + " " + weight;

    }

}

solution:

Java Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

Total Value: 9

Total Steps: 3526

Total Cuts : 342

0: 1 30

1: 1 150

3: 1 225

4: 1 135

5: 1 160

6: 1 80

9: 1 90

10: 1 100

11: 1 120

Total weight: 1090

The elevator is nearly full

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote