In: Computer Science
Java:
int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71};
1) Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive.
2) Write code to find the smallest value in the given array and print the value out.
3) Write code to find the largest value in the given array and print it out.
4) Create an int array called flipped and reverse the sign of every number in the given array and place it in the flipped array.
public class list {
//THIS FUNCTION FINDS THE SMALLEST IN THE ARRAY AND RETURNS IT
public int smallest(int a[])
{
int min=a[0];
for(int i:a)
{
if(i<min)
{
min=i;
}
}
return min;
}
//THIS FUNCTION FINDS THE LARGEST IN THE ARRAY AND RETURNS
IT
public int largest(int a[])
{
int max=a[0];
for(int i:a)
{
if(i>max)
{
max=i;
}
}
return max;
}
//THIS FUNCTION RETURNS THE FLIPPED ARRAY
public int[] flip(int a[])
{
for(int i=0;i<a.length;i++)
{
a[i]=-a[i];
}
return a;
}
public static void main(String[] args) {
int array[]=
{-40,60,78,-51,65,-95,77,-48,-66,71};
int positive[]=new int[10];
int negative[]=new int[10];
int i=0,j=0;
list l=new list();
for(int a:array)
{
if(a>=0)
{
positive[i++]=a;
}
else
{
negative[j++]=a;
}
}
for(int p=0;p<i;p++)
{
System.out.print(positive[p]+"
");
}
System.out.println();
for(int p=0;p<j;p++)
{
System.out.print(negative[p]+"
");
}
System.out.println();
System.out.println(l.smallest(array));
System.out.println(l.largest(array));
int flipped[]=new int[10];
flipped=l.flip(array);
for(int p:flipped)
{
System.out.print(p+" ");
}
}
}
HERE IS CODE FOR ABOVE QUESTIONS
1) IMPLEMENTED IN MAIN ITSELF
2)WRITTEN SEPARATE FUNCTION
3)WRITTEN SEARATE FUNCTION
4)WRITTEN A SEPARATE FUNCTION
PLEASE UPVOTE IF U LIKE MY ANSWER AND WRITE IN COMMENTS IF U HAVE ANY DOUBTS