简体   繁体   中英

Compare two arrays and return an array of integers which values are result of comparison

My not woring version Write a method which takes two integer arrays of the same length. Method should return an array of integers which values are result of comparison of input arrays using the following rules:

a[i] > b[i], then 1
a[i] == b[i], then 0
a[i] < b[i], then -1

Possible method signature:

static int[] CompareArrays(int[] a, int[] b) { ... }

Sample input:

a = [1, 3, 9]

b = [-2, 6, 9]

Expected output:

[1, -1, 0]

Use for loop to iterate both input arrays and assign values to output arrays.

looks like homework but following should get you started...

static int[] CompareArrays(int[] a, int[] b) {
  //add checks to make sure a.Length == b.Length;

  //allocate space for output array
  int[] retVal = new int[a.Length];

  //iterate through all the items in the input arrays... checking length of any one of the input arrays is just fine since we assume both should be same length
  for(int i = 0; i < a.Length; ++i) {
    retVal[i] = 0;

    //add your rules here e.g. a[i] > b[i] then 1 etc
    if(a[i] > b[i]) {
      retVal[i] = 1;
    }
  }

  return retVal;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM