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
//*************************************************************************
}