Sort by

recency

|

1121 Discussions

|

  • + 0 comments

    Whats C++11 for??

  • + 0 comments

    Code in Python

    def computeDifference(self):
            self.maximumDifference = max(self.__elements) - min(self.__elements)
    
  • + 0 comments

    This is the code in java

    Difference(int[] elements){
        this.elements = elements;
    
    }
    
    public void  computeDifference(){
       int smallest = 101;
       int largest = 0;
    
        for(int i = 0; i < elements.length; i++){
            if(elements[i] > largest){
                largest = elements[i];
            }
            if(elements[i] < smallest){
                smallest = elements[i];
            }
        }
        maximumDifference = largest - smallest;
    
    
    }
    
  • + 1 comment

    Can someone explain to me while the following code only returns zeroes (JAVA):

    public int computeDifference(){ Arrays.sort(elements); int len = elements.length-1;

        int maximumDifference = Math.abs(elements[0] - elements[len]);
        return maximumDifference;
    }
    
    • + 0 comments

      I suppose I know the issue in this. Remove the declaration int for maximumDifference because doing so, it'll create a local variable although it was already declared in the class and during the final output the class-level declared variable is accessed, not the local one.

      Scope matters!! 😃

  • + 0 comments

    C#

        public Difference(int[] elements)
        {
            this.elements = elements;
            this.maximumDifference = 0;
        }
    
        public int computeDifference()
        {
            Array.Sort(elements);
            
            //getting max and min values and getting the abs difference of them
            maximumDifference = Math.Abs(elements[0] - elements[elements.Length-1]);
            
            return maximumDifference;
        }