I expected the program to update the original array but it seems to update the array that has been sorted. i tried to copy array but it does not seem to help me. how do i make sure sure that the array that is updated is the original array.

Desired outpur
if my original array is 1,8,9,7,9
I would like all the other functions to work on other arrays
the updated array shouls be 1,8,0,7,0
where all the 9's have been replaced with 0's.
this is my code

package Number1;
public class Main
{
    public static void main(String[] args)
    {
        //Declarations
        int key = 9;
        int Length = 5;
        int []array = new int[Length];
        int UpdatingValue = 0;//new value to update by
        int ValueToUpdate = 9;//value that is to be updated

        //create and print
        insert(array);
        System.out.println("The original array is:");
        print(array);
        int[] CopyOFOriginalArray1 = array;
        int[] CopyOFOriginalArray2 = array;
        int[] CopyOFOriginalArray3 = array;
        Separator();


        //maximum and minimum
        max(CopyOFOriginalArray1);
        min(CopyOFOriginalArray2);
        Separator();

        //mean
        mean(CopyOFOriginalArray3);
        Separator();
        
        //Update
        Update(ValueToUpdate,UpdatingValue,CopyOFOriginalArray1);
        Separator();

        //Sort
        BubbleSort(array);
        print(array);
        Separator();
        SelectionSort(array);
        print(array);
        Separator();

        //Search
        System.out.println("The key searched in the array is " + key);
        LinearSearch(key,array);
        BinarySearch(key,array);
        Separator();

        //Std Deviation
        std_Dev(array);
        Separator();
    }//end main

    //*************************************************************************
    //This method will insert some randomly generated numbers in to the array
    public static int[] insert(int array[])
    {
        for (int i = 0; i < array.length; i++)
        {
            array[i] = (int) (Math.random() * 10);
        }

        return array;

    }//end insert
   //**************************************************************************


    //**************************************************************************
   //These methods will sort the elements od the array
    public static int[] SelectionSort(int array[])
    {
        System.out.println("The array sorted via a Selection Sort is :");
        for(int i = (array.length - 1 ); i >= 1; i--)
        {
            int max = array[0];
            int MaxIndex = 0;
            for(int j = 0; j <= i; j++)
            {
                if(max < array[j])
                {
                    max = array[j];
                    MaxIndex = j;
                }//end if
            }//end for
            if(max != i)
            {
                array[MaxIndex] = array[i];
                array[i] = max;
            }//end if
        }//end for

        return array;
    }//end SelectionSort

    public static int[] BubbleSort(int array[])
    {
        System.out.println("The array sorted via a Bubble Sort is :");
        for (int i = 0; i < (array.length - 1); i++)
        {
            for(int k = 0; k < (array.length - 1); k++)
            {
               int temp;
               if (array[k] > array[k + 1])
               {
                  temp = array[k];
                  array[k] = array[k + 1];
                  array[k + 1] = temp;
               }
            }
        }
        return array;
     }//end BubbleSort
    //*************************************************************************


    //*************************************************************************
    //These methods will search for a particular element in the array
    public static  int LinearSearch(int key,int array[])
    {
       System.out.println("The result of the search via a linear search is:");
        for(int i = 0; i < array.length; i++)
        {
            if (key == array[i])
            {
              System.out.println("The key is in position " + i);
              continue;
            }

           else
            {
                //System.out.println("The key is not in the array:");
                continue;
            }
        }
        return 1;
    }
    public static int BinarySearch(int key, int array[])
    {
        System.out.println("The result of the search via a binary search is:");
        int low = 0;
        int high = array.length;
        while (high > low)
        {
            int mid = ((high + low) / 2);
            if (key < array[mid])
            {
                high = (mid - 1);
            }
            if (key == array[mid])
            {
                System.out.println("The key is in position " + mid);
                break;
            }
            else
            {
                low = (mid + 1);
            }
        }
        return 1;
    }
    //*************************************************************************

   //*************************************************************************
   //This method is used to update one value with another
   public static int[] Update(int ValueToUpdate, int UpdatingValue, int array[])
   {
        boolean changed = false;
        for(int i = 0; i < array.length; i++)
        {

            if ( ValueToUpdate == array[i])
            {
                array[i] = UpdatingValue;
                changed = true;
                continue;
            }

         }
        if (changed == true)
            {
                System.out.println("The updated array is");
                print(array);
            }
        else
        {
            System.out.println("The value to update is not in the array.");
        }
        return array;
    }//end update
    
    //*************************************************************************


   //************************************************************************
    //This method will find and print the largest number in the array;
   public static int max(int array[])
    {
        int max = array[0];
        for(int i = 1; i < array.length; i++)
        {
            if (array[i] > max)
            {
               max = array[i];
            }
        }
        System.out.println("The maximum number in the array is " + max);
        return max;
    } //end  max
   //**************************************************************************

   //**************************************************************************
   //This method will find and print the minimun value in the array
   public static int min(int array[])
    {
        int min = array[0];
        for (int i = 1; i < array.length;i++)
        {
            if (array[i] < min)
            {
                min = array[i];
            }
        }
        System.out.println("The minimum number in the array is " + min);
        return min;
    }//end min
    //*************************************************************************

   //*************************************************************************
   //This method will calculate and print out the std deviation
   public static void std_Dev(int [] array)
    {
        double sumOfSquares = 0;
        for (int i = 0; i < array.length; i++)
        {
            sumOfSquares = sumOfSquares + (Math.pow(array[i],2));
        }
   // System.out.println(sumOfSquares);

        double sum = 0;
        for(int i = 0; i < array.length; i++)
        {
           sum = sum + array[i];
        }
        double sumSquared = Math.pow(sum,2);
        double sumSquaredPer_n = (sumSquared / array.length);
        double temp1 = (sumOfSquares - sumSquaredPer_n);
        double temp2 = (temp1 / ((array.length) - 1));
        double deviation = Math.sqrt(temp2);
        System.out.println("The deviation is  " + deviation);


    }//end deviation
   //**************************************************************************

   //**************************************************************************
   //This method will print the array

    public static int[] print(int array[])
    {
        for (int i = 0; i < array.length; i++)
        {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        return array;
    }//end print
    //*************************************************************************

    //*************************************************************************
    //This method will calculate and print the mean
    public static int[] mean(int[] array)
    {
       double sum = 0;
       for (int i = 0; i < array.length; i++)
       {
           sum = sum + array[i];
       }
       double average = (sum/array.length);
       System.out.println("The mean is  " + average);
       return array;
    }//end mean
    //*************************************************************************

    //*************************************************************************
    //This method will separate the output for clarity
    public static void Separator()
    {
        for (int i = 0; i < 30; i++)
        {
            System.out.print("==");
        }
        System.out.println();
    }//end Separaror
    //*************************************************************************


}

Edited 5 Years Ago by liphoso: bad formating

I expected the program to update the original array but it seems to update the array that has been sorted. i tried to copy array but it does not seem to help me. how do i make sure sure that the array that is updated is the original array.

Desired outpur
if my original array is 1,8,9,7,9
I would like all the other functions to work on other arrays
the updated array shouls be 1,8,0,7,0
where all the 9's have been replaced with 0's.
this is my code


I'm not sure I understand your problem exactly, but here's some stuff that might help:

package Number1;
public class Main
{
    public static void main(String[] args)
    {
        //Declarations
        int key = 9;
        int Length = 5;
        int []array = new int[Length];
        int UpdatingValue = 0;//new value to update by
        int ValueToUpdate = 9;//value that is to be updated

You have only declared an array, it has nothing in it at this point.

//create and print
        insert(array);
        System.out.println("The original array is:");
        print(array);
        int[] CopyOFOriginalArray1 = array;
        int[] CopyOFOriginalArray2 = array;
        int[] CopyOFOriginalArray3 = array;

You have now declared three new arrays, all of which point to the same memory space as the original. Changing any of these changes all of them. You have also inserted random values into the array that they all point to.

Imagine four people pointing to the same cake. You tell the first guy to eat a piece of the cake he's pointing to - each of the others is now pointing to less cake. That's the situation you've set up here.
What the variable "array" actually contains is not the array. It contains a number, that number is the address of the array on the heap.
This line:

int[] CopyOFOriginalArray3 = array;

assigns the contents of the variable array (the memory address) to CopyOFOriginalArray3.

To make a copy of an array, you have to declare a new array and then assign each value from array into the copy, usually you'd do that in a for loop.

//maximum and minimum
        max(CopyOFOriginalArray1);
        min(CopyOFOriginalArray2);
        mean(CopyOFOriginalArray3);

Since all of these point to the same place, these all act as if they were acting on copies, until you change one of them - then, they all change.


Does this answer your question? If not, maybe you can rephrase it.

got it, thanx. my problem was that i was not making copies of the array. insterad i was creating another array that pointed to the sam e location in memory or something like that.

This question has already been answered. Start a new discussion instead.