3

I have a practice problem I am trying to solve. I have three arrays accepted into a method. Names, ages, and salaries. I am to return using string.format that shows the salary, name, and the age

The arrays are the same length and the corresponding elements in each array are related. I am supposed to find the smallest salary and use that to get the corresponding name and age of the person.

I am able to find the smallest salary, but I am confused on how to get the corresponding name and age that goes along with that.

Thank you in advance

Edit: If I found the smallest salary through this code, what is the function to grab this index and use that index to get the corresponding elements:

public static String getSmallestSalaryString(String[] names, int[] ages, double[] salaries ) {

double smallSal = Integer.MAX_VALUE;    

for(int i = 0; i < salaries.length; i++) {

    if(smallSal > salaries[i]) {
        smallSal = salaries[i];
    }       
}
3
  • 1
    Just use the same index to access the other arrays. Commented Jan 17, 2018 at 3:19
  • Please post your current (working) code and any attempt at the other parts would be good as well. Commented Jan 17, 2018 at 3:19
  • 2
    Just FYI, this approach is called "parallel arrays" and was outdated by the 1970s with the advent of languages that had actual data structures (i.e. pretty much everything after Dartmouth BASIC). I hope the instructor is going to introduce classes immediately after this and rewrite the same program "correctly". Commented Jan 17, 2018 at 3:54

4 Answers 4

3

You somehow knew the smallest salary lets say it is smallestSalary.

// Find the index corresponding to the smallest salary
int index = -1;
for(int i=0; i<salaryArray.length; i++){
    if(salaryArray[i] == smallestSalary){
        index = i;
    }
}

Now you have the index. so just return/use names, ages.

System.out.println(nameArray[index]);
System.out.println(ageArray[index]);

But having data like can cause data inconsistency. You can have a class to group each person related info. Person class can be as simple as this.

class Person{
   int age;
   String name;
   int salary;
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can return the index of salary array and use same index to get name and age from another arrays.

public static int getSmallestSalaryIndex(double[] salaries) {

double smallSal = salaries[0];    
int index =0;
for(int i = 1; i < salaries.length; i++) {

    if(smallSal > salaries[i]) {
        smallSal = salaries[i];
        index =i;
    }       
}
   return index;
}

You should write implementation as per method name, don't include everything inside same method. You don't need to pass all the array (age, name) to getSmallestSalaryIndex.

int idx = getSmallestSalaryIndex(salaries);
System.out.printf("The person name %s is %d has salary %f", names[idx] ,ages[idx],salaries[idx]);

4 Comments

@HuyVo tell me how ?
@HuyVo practically, that is not going to happen, but yes. I will consider this scenario. and changed the code.
@HuyVo No. you have very basic mistake, don't let me point out. :P Yes index = 0 can be avoided.
This helped a lot. Thank you. And yes, I know, that it is better to write helper methods, I will format it near the end.
2
double smallSal = salaries[0];
int index = 0;

for(int i = 1; i < salaries.length; i++) {

    if(smallSal > salaries[i]) {
        smallSal = salaries[i];
        index = i;
    }       
}

This saves you one less trip as you assume the smallest value is the first index.

Then, use the index to access the names and age of that person.

Don't forget to check trivial things such as if the arrays are null, etc.

5 Comments

if you set the int i = 1, would that not mess up the actual index since index values start at zero?
No. You guess the first element in the array is the smallest. Since that is the assumption, you can skip when i=0.
Try to run the algorithm on a piece of paper when the list is {3,2,1 and {1,2,3}
int index = smallSal[0]; will never compile
@Ravi what do you mean?
1

Assuming you have arrays a[], b[], c[] - a being salary

  1. You don't change the sequence of the array a[] when you check the lowest index.

Taking the lowest index to be "pos"

Just access b[pos], c[pos]

  1. You change order of values in array a[] while finding out the lowest index

Change the values of b[], c[] as well while changing values of a[] and access b[pos], c[pos]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.