Question

JAVA Array and Loop Question. No DUPLICATES! I saw a similar question but it is not...

JAVA Array and Loop Question. No DUPLICATES! I saw a similar question but it is not the same as this.

There are two arrays, one holds time and one holds amount, each corresponding to each other.

timeArray= [ 0, 2, 3, 0, 2]

amountArray= [500, 100, 400, 650, 50]

time = 4 sec

From these I must find the maximum sum of amountArray with correspondence of timeArray being the urgency #. There is a given time limit and before that time is finished I must find out the maximum amount I can get from amountArray.

So for example from the arrays above: from timeArray there are two 0s. I must pick the one with the highest amount, in this case 650. Then the next is there are two 2s, and pick the max amount which is 100, and 3 is just one. From this my max sum should be 1150. What would be the loop behind this algorithm, tried to do it but got very confused.

The trick is I can only pick one amount at a time and with each loop iteration, timeArray elements must decrease by 1. so for i =1, timeArray= [ 0, 1, 2, 1, 0], if i=2 timeArray= [0, 0, 1, 0, 0], etc... Once the time, in this case 4 iteration, is up, then it must give the max amount from the array.

It would be nice if you could help me with this. Thank you, will give thumbs up for correct code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It's tricky somewhat. However, we can use "TWO LOOPS" to solve this one.

Follow the code below. Please follow the comments to better understand the program.

====================

SOURCE CODE

===================

class Program {
   public static void main (String[] args) {
       int[] timeArray= { 0, 2, 3, 0, 2};
int[] amountArray= {500, 100, 400, 650, 50};
int sum=0;
//Take the length of the array
int length=timeArray.length;
for(int i=0;i<length;i++)
{
int time=timeArray[i];
//If the time is -1, that means we have already processed that time. So, just continue with the next time.
if(time==-1)
continue;
else
{
int max=amountArray[i];
//We simpy need to see the element with same time RIGHT of "index i"
//So, take i+1
for(int j=i+1;j<length;j++)
{
//If the times are same, then make it -1,
// Just to remember that we have already seen this time
if(time==timeArray[j])
{
timeArray[j]=-1;
//Update max amount
if(max<amountArray[j])
{
max=amountArray[j];
}
}
}
//Add the maximum to the final sum
sum+=max;
System.out.println("Index="+i+" : Maximum Number= "+max);
}
}
System.out.println("The total sum is: "+sum);
   }
}

==========================================================

SCREENSHOT FOR THE CODE:

=============

I HOPE THIS HELPS A LOT..!!

Please comment down here in the comment box if you have any doubts.

Please hit the LIKE Button if you liked the answer.

==============

Add a comment
Know the answer?
Add Answer to:
JAVA Array and Loop Question. No DUPLICATES! I saw a similar question but it is not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Assume L is an array, length(L) returns the number of records in the array, and qsort(L,i,j)...

    Assume L is an array, length(L) returns the number of records in the array, and qsort(L,i,j) sorts the records of L from i toj (leaving the records sorted in L) using the Quicksort algorithm. What is the average-case complexity for the following code fragment? for (i = @; i<length(L); i++) asort(1, 0, 1); Consider the time for each pass of the for loop, and sum them all together. Prove the upper and lower bound, then argue for an average case....

  • (b) Suppose you are designing a search aggregator, which for a given query fetches search results from two different se...

    (b) Suppose you are designing a search aggregator, which for a given query fetches search results from two different search engines and presents an intersection of the two search results. Here is a simplified version of this problem: Given two sorted integer arrays of lengths m and n, return a new array with elements that are present in both input arrays. The input array may contain duplicates, but there should be no duplicates in the output array. For example, if...

  • Question 2 In this question, you will read two data files that include integers into two...

    Question 2 In this question, you will read two data files that include integers into two different arrays – the same way we did in class (but we are doing to arrays here). Duplicates are ok. 1- After you read the data into the array (use one function that takes an int array and a dsize by reference just like we did in class, and call that from main to fill both arrays). 2- Include a printArray function so that...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Write a method that takes in a two-dimensional array of integers, do some calculation for each...

    Write a method that takes in a two-dimensional array of integers, do some calculation for each row in the two-dimensional array, and returns a one-dimensional array of sums for each row. Your code must work any array dimensions, including jagged arrays. The calculations are: • Sum of each row • Maximum value of each row • Minimum value of each row • Average of each row Here is an example an array passed and returns an array of sums for...

  • C++ Question 14 10 Assume Lis an array, length(L) returns the number of records in the...

    C++ Question 14 10 Assume Lis an array, length(L) returns the number of records in the array, and qsort(L, 1. j) sorts the records of L from i toj (leaving the records sorted in L) using the Quicksort algorithm. What is the average-case complexity for the following code fragment? for (i = 0; i<length(); i++) asort(L, 0, 1); Consider the time for each pass of the for loop, and sum them all together. Prove the upper and lower bound, then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT