Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Okay, here is my question: Use only topics from chapters 1 - 8. Don’t get fancy.

ID: 3685471 • Letter: O

Question

Okay, here is my question:

Use only topics from chapters 1 - 8. Don’t get fancy.

Problem 1

Write a program to accomplish the following tasks:

              2) make an array of the size 10;

              3) fill the array with random integers from 0 to 999;

              4) print lines of output for each of these:

                            a)           the original members of the array (can use original array)

                            b)          every element at an even index based on the original displayed array (original)

                            c)           every even element based on the original displayed array (original)

                            d)          the reverse of the original array (you will need a copy of the array)

                            e)           the first and last element based on the original displayed array, use the length not a literal number (original)

                            f)           swap the first and last element based on the original displayed array (need to make another copy)

                            g)           shift all elements to the right once, move the last element to the first based on the original displayed array (need to make another copy)

                            h)          replace all even elements with the number zero based on the original array (need to make another copy)

                            i)            replace each element except the first and last with the larger of its two neighbors based on the original displayed array (need to make another copy)

                            j)            remove the two middle elements based on the original displayed array (need to make another copy)

                            k)           move all odd elements to the front otherwise preserving the order of the array based on the original displayed array (need to make another copy)

                            l)            display true if the original array is stored in increasing order

                            m)         display true if the array contains two adjacent elements that are equal

                            n)          compute the alternating sum of all elements in the array, for example: 1 2 3 4 5 would be computed as 1 - 2 + 3 - 4 + 5 ... (original)

                            o)          display a bar chart equal to the value of the number in each element, for example: 1 2 3 4 5 would display as:

                                          *

                                          **

                                          ***

                                          ****

                                          *****

Problem 2 (Bonus)

Take problem 1.o (the last problem directly above) and make it work for negative numbers.

If the numbers were 1, -3, 4, -5, 6

_____*               ç the underscores represent spaces, if -5 were the biggest number, you know how far

__*                                    before zero to start

_____****

*****

_____******

So this is what I got so far:

import java.util.Arrays;
public class COSC110Homework4 {

   public static void main(String[] args) {
       //2) make an array of the size 10
       int[]original = new int[10];
       System.out.println(Arrays.toString(original));
       //3) fill the array with random integers from 0 to 999
       for(int x=0; x<original.length; x++){
           original[x] = (int)(Math.random()*1000);
       }
       //4) print lines of output for each of these:
       //a) the original members of the array (can use original array)
       System.out.println(Arrays.toString(original));
       //b) every element at an even index based on the original displayed array (original)
       for(int x=0; x<original.length-1; x=x+2){
           System.out.print(original[x] + ",");
       }
       if((original.length-1)%2 == 0){
           System.out.println(original[original.length-1]);
       }
           //c) every even element based on the original displayed array (original)
           for(int x=0; x<original.length; x++){
               if(original[x]%2 ==0){
                   System.out.print(original[x]);
               }
           }
           System.out.println();
           //d) the reverse of the original array (you will need a copy of the array)
           for(int x=original.length-1; x>=0; x--){
               System.out.print(original[x] + " ");
           }
           System.out.println();
           //e) the first and last element based on the original displayed array, use the length not a literal number (original)
           System.out.println("First; " + original[original.length-original.length]);
           System.out.println("Last; " + original[original.length-1]);
          
       }
      
       }
      

   }

}

So, I'm sort of stuck after this. Could you help me? Can you build upon the problem that I've already started. And can you label each part of the question? Thank you so much!

Explanation / Answer

import java.util.Arrays;
public class PatterPrinting {
public static void main(String[] args) {
//2) make an array of the size 10
int[]original = new int[10];
int[] duplicate = new int[10];
int i = 0, temp;
//3) fill the array with random integers from 0 to 999
for(int x=0; x<original.length; x++){
original[x] = (int)(Math.random()*1000);
}
//4) print lines of output for each of these:
//a) the original members of the array (can use original array)
System.out.println(Arrays.toString(original));
//b) every element at an even index based on the original displayed array (original)
for(int x=0; x<original.length; x=x+2){
System.out.print(original[x] + ", ");
}
System.out.println();
//c) every even element based on the original displayed array (original)
for(int x=0; x<original.length; x++){
if(original[x]%2 ==0){
System.out.print(original[x] + ", ");
}
}
System.out.println();
//d) the reverse of the original array (you will need a copy of the array)
  
for(int x=original.length-1; x>=0; x--){
duplicate[i++] = original[x];
}
System.out.println(Arrays.toString(duplicate));
//e) the first and last element based on the original displayed array, use the length not a literal number (original)
System.out.println("First: " + original[original.length-original.length]);
System.out.println("Last: " + original[original.length-1]);

//f) swap the first and last element based on the original displayed array (need to make another copy)
  
temp = original[0];
duplicate = original;
duplicate[0] = duplicate[original.length-1];
duplicate[original.length-1] = temp;
System.out.println(Arrays.toString(duplicate));
//g) shift all elements to the right once, move the last element to the first based on the original displayed array (need to make another copy)
duplicate = original;
temp = duplicate[duplicate.length-1];
for(i = duplicate.length - 2; i >= 0; i--)
duplicate[i+1] = duplicate[i];
duplicate[0] = temp;
System.out.println(Arrays.toString(duplicate));
// h) replace all even elements with the number zero based on the original array (need to make another copy)
duplicate = original;
for(i = 0; i < duplicate.length; i++)
if(duplicate[i]%2 == 0)
duplicate[i] = 0;
System.out.println(Arrays.toString(duplicate));
//i) replace each element except the first and last with the larger of its two neighbors based on the original displayed array (need to make another copy)
  
duplicate = original;
for(i = 1; i < duplicate.length - 1; i++)
{
if(original[i-1] > original[i+1])
duplicate[i] = original[i-1];
else
duplicate[i] = original[i+1];
}
System.out.println(Arrays.toString(duplicate));
// j) remove the two middle elements based on the original displayed array (need to make another copy)
duplicate = original;
for(i = duplicate.length/2 - 1; i < duplicate.length-2; i++)
duplicate[i] = duplicate[i+2];
System.out.println(Arrays.toString(duplicate));
//k) move all odd elements to the front otherwise preserving the order of the array based on the original displayed array (need to make another copy)

  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote