Questions
4. For n = 50, 100, 200, 300, 400, do the following. Let A be the...

4. For n = 50, 100, 200, 300, 400, do the following. Let A be the n × n matrix with entacries Aij = p 2(i − j) 2 + n/5. Define xexact = [1, 1, . . . , 1]T , and then compute b = Axexact. Using Matlab’s backslash, numerically solve the equation

Ax = b,

thereby producing a computed solution xc. In exact arithmetic xc = xexact of course, but in IEEE double precision xc will not equal xexact. The quantity kxc − xexactk∞ is the forward error. Construct a table which, for each n, collects the relative forward error, relative backward error, magnification factor, and (infinity-norm) condition number of A. Discuss your results.

In: Computer Science

Etching and engraving are both intaglio processes. In 50 words or less, describe, in the text...

Etching and engraving are both intaglio processes. In 50 words or less, describe, in the text box below, the difference in line quality between an etching and an engraving.

In: Computer Science

I have this program in C that takes three char arrays that each have a first...

I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers.

#include <stdio.h>

void upper_string(char []);

int main()
{
char name1[100]="John Smith";
char name2[100]="Mary Cohen";
char name3[100]="Carl Williams";
upper_string(name1);// calling upper case function
upper_string(name2);
upper_string(name3);
printf("Names are in upper case \n\"%s\"\n""%s\"\n""%s\"\n", name1,name2,name3);
reverse(name1);// calling reverse string function
reverse(name2);
reverse(name3);
  
getch();
}

void upper_string(char s[]) {
int c = 0;

while (s[c] != '\0') {
if (s[c] >= 'a' && s[c] <= 'z') {
s[c] = s[c] - 32;
}
c++;
}
}
// function to reverse the string
void reverse(char string[]){

char temp;
int i, j = 0;
i = 0;
j = strlen(string) - 1;

while (i < j) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
i++;
j--;
}

printf("\nReverse string is :%s", string);
}

In: Computer Science

Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...

Consider the following type of binary trees:

data Tree a = Leaf a | Node (Tree a) (Tree a)

A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not.

balanced :: Tree a -> Bool

In: Computer Science

(MUST BE DONE IN C (NOT C++)) Define two different structures at the top of your...

(MUST BE DONE IN C (NOT C++))

Define two different structures at the top of your program. Define each structure with exactly three members (each member has to be a different datatype). You may set them up whichever way you want. Don’t forget your semicolons.

- Inside main, declare two structures. One for each of the two structures you defined.

- Then, initialize each one of the members manually (the three members of your first structure and the three elements of your second structure).

- Lastly, print out all six members with their respective message.

Notice the words “define”, “declare” and “initialize”. Using them properly with the guidelines in terms of programming is going to help a lot in understanding this problem.

In: Computer Science

Question 5 The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each...

Question 5

The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue.

Process          Burst Time      Arrival Time   Priority

P1                15            0            8

P2                20           0            3

P3                20           20           4

P4                20           25           4

P5                5            45           5

P6                15            55           5

What is the average turnaround time of these processes?

Answer:

Question 6

The following processes are being scheduled using a preemptive, priority-based, round-robin scheduling algorithm. Each process is assigned a numerical priority,with a higher number indicating a higher relative priority. The scheduler will execute the highest-priority process. For processes with the same priority, a round-robin scheduler will be used with a time quantum of 10 units. If a process is preempted by a higher-priority process, the preempted process is placed at the end of the queue.

Process          Burst Time      Arrival Time   Priority

P1                15            0            8

P2                20           0            3

P3                20           20           4

P4                20           25           4

P5                5            45           5

P6                15            55           5

What is the average waiting time of these processes?

Answer:

In: Computer Science

GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...

GETTING STARTED

Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java.

PART 1: Implementing BubbleSorter

Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on.

This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name, especially for an instance variable. But in literature about sorting algorithms it’s common to use “a” for an array that will be sorted.

In the sort() method, the outer loop should execute n times, where n is the length of the array. The inner loop should compare all adjacent pairs of elements, starting with a[n-1] vs. a[n-2], and ending with a[1] vs. a[0]. If comparison determines that the elements should be swapped, the inner loop should do the swap and increment nSwaps. Whether or not the elements are swapped, the inner loop should increment nVisits by 2. Note that this isn’t the most efficient way to control the loops, but it’s simple so it’s ok for now.

Complete the isSorted() method, which you can later use to test your code. After you sort an array, analyze it with isSorted() to make sure it’s really sorted. Before testing, look at BubbleSortTestCaseMaker.java. It provides 4 methods that return test case arrays. The first 3 methods are obvious. The last method builds an array containing random ints; its contents will be different every time the method is called.

Test your BubbleSorter code in main(). The starter file sorts a tiny test array. When that works, replace getTiny() with getAlreadySorted(), then with getBackward(). For each case, record the number of visits and swaps; you’ll need them later.

When your first 3 test cases are correctly sorted, test several times with a larger input array, returned by BubbleSortTestCaseMaker.buildRandom(100, 1000). Caution: this method returns a different random array every time it is called. Large random test cases are good for increasing confidence that an algorithm is correct, but they are difficult to debug because of their size and because they aren’t repeatable. So it’s good practice to start with simple test cases that make debugging easy, then move on to bigger cases after you have fixed all the bugs you can find with simple cases. Run the test 3 times, and record the number of visits and swaps for both runs.

Make a table like the following, and enter the counts that you observed.

Test case

Number of visits

Number of swaps

Tiny

Already sorted

Backward

Random #1

Random #2

Random #3

PART 2: BubbleSorter complexity

Complete the Statistician class, and use it to do experiments on your bubble sorter to determine if its complexity is O(n2). You can't draw valid conclusions from a single observation, or from a small number of observations. You need to execute a statistically large number of times, so that your conclusions have statistical strength. For this lab, an experiment will be 1000 executions. Define a private final static int called N_REPETITIONS, whose value is 1000.

The getStats() method of the Statistician class has an arg for specifying an array length. In that method’s loop, a random array of the specified length is created. Then a BubbleSorter sorts the array. Replace the “Assert …” comment line with a line that asserts that the sorter has correctly sorted. Paste that line into your report. Remember to configure Eclipse to run with assertions enabled (Run -> Run Configurations, then “Arguments tab” and type “-ea” into VM Arguments). If an assertion error is ever thrown, go back and fix your sorter. After the assertion line, replace the next comment with lines that retrieve the number of visits and swaps from the sorter, and store those numbers in the appropriate array lists.

After the loop in getStats(), write code that analyzes the 2 array lists, and prints the minimum, average, and maximum observed number of visits and number of swaps. Think about the benefit of creating a helper method that computes and prints min/avg/max for any array list of longs. Did you use a helper method?

Before running the statistician, think about what you expect to see. You haven’t been told the complexity of the bubble sort algorithm, but for now suppose that for an input array of length n, both the number of visits and the number of swaps are O(n2). If sorting an array of length=1000 requires v visits and s swaps, approximately how many visits and swaps are required to sort an array of length=3000?

In main(), the starter code calls getStats() for array lengths 1000 and 3000. Run main(). If it takes more than 2-3 minutes, try with shorter array lengths (e.g. 500 and 1500); the second length should be 3x the first length. Paste your output into your report. Does the output support the hypothesis that bubble sort is O(n2)?

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package bubble;

public class BubbleSorter
{
   private int[]       a;
   private long       nVisits;
   private long       nSwaps;
  
  
   public BubbleSorter(int[] a)
   {
       this.a = a;
   }
  
  
   public void sort()
   {
       for (/* control the outer loop */)
       {
           for (/* control the inner loop */)
           {
               // Increment number of visits by 2.
               if (/* if 2 elements you're visiting need to be swapped */)
               {
                   // Swap the elements and increment nSwaps.
               }
           }
       }
   }
  
  
   public String toString()
   {
       String s = nVisits + " visits, " + nSwaps + " swaps\n{";
       for (int n: a)
           s += " " + n;
       s += " }";
       return s;
   }
  
  
   public boolean isSorted()
   {
       /* Implement this */
   }
  
  
   public long getNVisits()
   {
       /* Implement this */
   }
  
  
   public long getNSwaps()
   {
       /* Implement this */
   }
  
  
   public int[] getArray()
   {
       return a;
   }
  
  
   public static void main(String[] args)
   {
       int[] a = BubbleSortTestCaseMaker.getTiny();
      
       BubbleSorter sorter = new BubbleSorter(a);
       sorter.sort();
       System.out.println(sorter);
       System.out.println(sorter.isSorted() ? "SORTED" : "NOT SORTED");
   }
}
---------------------------------------------------------------------------------------------------------

package bubble;

import java.util.*;


public class Statistician
{
   private static void getStats(int arrayLength)
   {
       ArrayList<Long> visitCounts = new ArrayList<>();
       ArrayList<Long> swapCounts = new ArrayList<>();
      
       for (int i=0; i<N_REPETITIONS; i++)
       {
           int[] array = BubbleSortTestCaseMaker.buildRandom(arrayLength, arrayLength*100);
           BubbleSorter sorter = new BubbleSorter(array);
           sorter.sort();
           // Assert that the sorter sorted correctly.
           // Append # visits and # swaps to the array lists.
       }

       // Compute and print min/average/max number of visits.
       // Compute and print min/average/max number of swaps.
   }
  
  
   public static void main(String[] args)
   {
       System.out.println("1000:");
       getStats(1000);
      
       System.out.println("3000:");
       getStats(3000);
   }
}
----------------------------------------------------------------------------------------------------------------------------------------

package bubble;

public class BubbleSortTestCaseMaker
{
   private final static int[]       TINY =
   {
       1000, 1, 2, 3
   };
  
   public static int[] getTiny()
   {
       return TINY;
   }
  
  
   private final static int[]       ALREADY_SORTED =
   {
       1, 2, 3, 4, 5, 6, 7, 8, 9, 10
   };
  
   public static int[] getAlreadySorted()
   {
       return ALREADY_SORTED;
   }
  
  
   private final static int[]       BACKWARD =
   {
       10, 9, 8, 7, 6, 5, 4, 3, 2, 1
   };
  
   public static int[] getBackward()
   {
       return BACKWARD;
   }
  
  
   public static int[] buildRandom(int length, int maxValue)
   {
       int[] array = new int[length];
       for (int i=0; i<length; i++)
           array[i] = (int)(Math.random()*(maxValue + 1));
       return array;
   }
  
  
   public static void main(String[] args)
   {
       for (int i: buildRandom(10, 100))
           System.out.println(i);
   }
}

In: Computer Science

Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11,...

Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11, where s is the board size. Before the game starts the program will prompt for the board size. Note: the winning conditions are the same as the original Tic-tac-toe game in that you need to fill the entire row/column/diagonal to win.

Here are a few sample runs. The output is a bit different so that we can handle two-digit coordinates consistently. We expect (but you won’t lose any marks if you don’t) that you make extensive use of compound data types and string library. Note that our solution has less than 50 lines of code. You may assume that a user always enters a valid move.

You may use any functions/operators for this question.

Sample runs:

 

Size--> 6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 1 0 X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 13 0 X 2 3 4 5 6 7 8 9 10 11 12 O 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 2 0 X X 3 4 5 6 7 8 9 10 11 12 O 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 14 0 X X 3 4 5 6 7 8 9 10 11 12 O O 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 3 0 X X X 4 5 6 7 8 9 10 11 12 O O 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 15 0 X X X 4 5 6 7 8 9 10 11 12 O O O 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 4 0 X X X X 5 6 7 8 9 10 11 12 O O O 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 16 0 X X X X 5 6 7 8 9 10 11 12 O O O O 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 5 0 X X X X X 6 7 8 9 10 11 12 O O O O 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 17 0 X X X X X 6 7 8 9 10 11 12 O O O O O 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 6 0 X X X X X X 7 8 9 10 11 12 O O O O O 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 18 0 X X X X X X 7 8 9 10 11 12 O O O O O O 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 0 X X X X X X X 7 8 9 10 11 12 O O O O O O 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Winner: X

 

Size--> 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 X--> 0 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 O--> 72 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O 73 74 75 76 77 78 79 80 X--> 1 X X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O 73 74 75 76 77 78 79 80 O--> 73 X X 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 80 X--> 8 X X 2 3 4 5 6 7 X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 80 O--> 80 X X 2 3 4 5 6 7 X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 O X--> 7 X X 2 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 79 O O--> 79 X X 2 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 O O X--> 2 X X X 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O 74 75 76 77 78 O O O--> 74 X X X 3 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O 75 76 77 78 O O X--> 3 X X X X 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O 75 76 77 78 O O O--> 75 X X X X 4 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O 76 77 78 O O X--> 4 X X X X X 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O 76 77 78 O O O--> 76 X X X X X 5 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O 77 78 O O X--> 5 X X X X X X 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O 77 78 O O O--> 77 X X X X X X 6 X X 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O O 78 O O X--> 15 X X X X X X 6 X X 9 10 11 12 13 14 X 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O O 78 O O O--> 78 X X X X X X 6 X X 9 10 11 12 13 14 X 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 O O O O O O O O O Winner: O

 

Size--> 6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 0 X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 1 X O 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 2 X O X 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 3 X O X O 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 4 X O X O X 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 5 X O X O X O 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 12 X O X O X O 6 7 8 9 10 11 X 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 13 X O X O X O 6 7 8 9 10 11 X O 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 14 X O X O X O 6 7 8 9 10 11 X O X 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 15 X O X O X O 6 7 8 9 10 11 X O X O 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 16 X O X O X O 6 7 8 9 10 11 X O X O X 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 O--> 17 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 X--> 24 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X 25 26 27 28 29 30 31 32 33 34 35 O--> 25 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O 26 27 28 29 30 31 32 33 34 35 X--> 26 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X 27 28 29 30 31 32 33 34 35 O--> 27 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X O 28 29 30 31 32 33 34 35 X--> 28 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X O X 29 30 31 32 33 34 35 O--> 29 X O X O X O 6 7 8 9 10 11 X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 11 X O X O X O 6 7 8 9 10 X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 O--> 10 X O X O X O 6 7 8 9 O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 9 X O X O X O 6 7 8 X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 O--> 8 X O X O X O 6 7 O X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 7 X O X O X O 6 X O X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 O--> 6 X O X O X O O X O X O X X O X O X O 18 19 20 21 22 23 X O X O X O 30 31 32 33 34 35 X--> 23 X O X O X O O X O X O X X O X O X O 18 19 20 21 22 X X O X O X O 30 31 32 33 34 35 O--> 22 X O X O X O O X O X O X X O X O X O 18 19 20 21 O X X O X O X O 30 31 32 33 34 35 X--> 21 X O X O X O O X O X O X X O X O X O 18 19 20 X O X X O X O X O 30 31 32 33 34 35 O--> 20 X O X O X O O X O X O X X O X O X O 18 19 O X O X X O X O X O 30 31 32 33 34 35 X--> 19 X O X O X O O X O X O X X O X O X O 18 X O X O X X O X O X O 30 31 32 33 34 35 O--> 18 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O 30 31 32 33 34 35 X--> 30 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X 31 32 33 34 35 O--> 31 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O 32 33 34 35 X--> 32 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X 33 34 35 O--> 33 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O 34 35 X--> 34 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O X 35 O--> 35 X O X O X O O X O X O X X O X O X O O X O X O X X O X O X O X O X O X O Winner: None

 

Size--> 3 0 1 2 3 4 5 6 7 8 X--> 0 X 1 2 3 4 5 6 7 8 O--> 1 X O 2 3 4 5 6 7 8 X--> 2 X O X 3 4 5 6 7 8 O--> 3 X O X O 4 5 6 7 8 X--> 4 X O X O X 5 6 7 8 O--> 5 X O X O X O 6 7 8 X--> 6 X O X O X O X 7 8 Winner: X

In: Computer Science

Java : Modify the selection sort algorithm to sort an array of integers in descending order....

Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field.

Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion

.......

........

sec01/SelectionSortDemo.java

import java.util.Arrays;

/**
This program demonstrates the selection sort algorithm by
sorting an array that is filled with random numbers.
*/
public class SelectionSortDemo
{
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println(Arrays.toString(a));

SelectionSorter.sort(a);

System.out.println(Arrays.toString(a));
}
}

sec01/ArrayUtil.java

import java.util.Random;

/**
This class contains utility methods for array manipulation.
*/
public class ArrayUtil
{
private static Random generator = new Random();

/**
Creates an array filled with random values.
@param length the length of the array
@param n the number of possible random values
@return an array filled with length numbers between
0 and n - 1
*/
public static int[] randomIntArray(int length, int n)
{
int[] a = new int[length];
for (int i = 0; i < a.length; i++)
{
a[i] = generator.nextInt(n);
}
  
return a;
}

/**
Swaps two entries of an array.
@param a the array
@param i the first position to swap
@param j the second position to swap
*/
public static void swap(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

sec01/SelectionSorter.java

/**
The sort method of this class sorts an array, using the selection
sort algorithm.
*/
public class SelectionSorter
{
/**
Sorts an array, using selection sort.
@param a the array to sort
*/
public static void sort(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
int minPos = minimumPosition(a, i);
ArrayUtil.swap(a, minPos, i);
}
}

/**
Finds the smallest element in a tail range of the array.
@param a the array to sort
@param from the first position in a to compare
@return the position of the smallest element in the
range a[from] . . . a[a.length - 1]
*/
private static int minimumPosition(int[] a, int from)
{
int minPos = from;
for (int i = from + 1; i < a.length; i++)
{
if (a[i] < a[minPos]) { minPos = i; }
}
return minPos;
}
}

Extra Note: Let’s program this algorithm, called selection sort. For this program, as well as the other programs in this chapter, we will use a utility method to generate an array with random entries. We place it into a class ArrayUtil so that we don’t have to repeat the code in every example. To show the array, we call the static toString method of the Arrays class in the Java library and print the resulting string (see Section 7.3.4). We also add a method for swapping elements to the ArrayUtil class. (See Section 7.3.8 for details about swapping array elements.)

This algorithm will sort any array of integers. If speed were not an issue, or if there were no better sorting method available, we could stop the discussion of sorting right here. As the next section shows, however, this algorithm, while entirely correct, shows disappointing performance when run on a large data set.

Special Topic 14.2 discusses insertion sort, another simple sorting algorithm.

In: Computer Science

I need a brief text file to explain why the program “filesize1v.c” gets stuck in the...

I need a brief text file to explain why the program “filesize1v.c” gets stuck in the “do/while” loop (i.e., explain why “ch != EOF” is always true) ??

example.txt :

hello
ÿhello everyone.

_________________

//filesize1v.c
#include 
#include 

int main(int argc, char *argv[]){  
        FILE *fd;
        
        unsigned char ch;  //what will happen if you uncomment this line and comment the                //next line
        //char ch;

        int fileSize=-1;

        fd = fopen(argv[1], "r"); 

        do{
                ch=getc(fd);  //0xFF
                fileSize++;
                printf("fileSize=%d\n", fileSize);
                printf("Char read is ch=%c, in hex ch=%#hhx EOF is %#x\n\n", ch, ch, EOF);
                sleep(1);
        } while( ch != EOF);  //ch =0x FF,  EOF=0x FFFF FFFF
        
        printf(" \nout of do while loop now.\n\n");
        printf("ch=%d EOF=%#x\n", ch, EOF);
        printf("size of char =%ld size of EOF=%ld\n", sizeof(char), sizeof(EOF));
        
        printf("Size of %s is %d\n", argv[1], fileSize);
}


//suggeson: levae all the printf statment in place
//run the code to observe the output
//then use gdb to find out the "bug"

In: Computer Science

Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending...

Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending in 3. • Write a for loop to display a string entered by the user backwards.

In: Computer Science

iKiwi wants to be able to analyse sales information from all its stores so that any...

iKiwi wants to be able to analyse sales information from all its stores so that any opportunities to increase revenue can be identified and acted on. They also want to be able to track product returns for quality control purposes. You are the Chief Knowledge Officer and have been asked to create two Data Warehouses:

1. Create a Data Warehouse schema (with one central table and at least four lookup tables) that iKiwi can use to monitor sales performance. Provide some context for your schema. 2. Create another Data Warehouse schema (with one central table and at least four lookup tables) to monitor products returned by customers. Provide some context for your schema.

In: Computer Science

create two examples for each of the vulnerabilities in the category below and possible fixes ....

create two examples for each of the vulnerabilities in the category below and possible fixes .

Missing Encryption of Sensitive Data

Execution with Unnecessary Privileges

Incorrect Permission Assignment for Critical Resource

In: Computer Science

Please write in python Use modular design to write a program that asks the user to...

Please write in python

Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet.

PLANET

CONVERSION FACTOR

Mercury

0.4155

Venus

0.8975

Earth

1.0000

Moon

0.1660

Mars

0.3507

Jupiter

2.5374

Saturn

1.0677

Uranus

0.8947

Neptune

1.1794

Pluto

0.0899

The Program must meet the following requirements:

  1. The program must have a menu
  2. The program should output an error message if the user doesn’t type a correct planet name. The prompt and the error message should make it clear to the user how a planet name must be entered.
  3. The output should be labeled clearly and formatted neatly.
  4. The output must be display with 2 decimal points.
  5. Use modular design
  6. Use proper documentation

In: Computer Science

Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...

Consider the following recursive method in Java

public static int mystery(int n)

{

  if (n == 0)

  return 1;

   else   

return 4 * mystery (n - 1);  

}

What is the output of  mystery (3) using the code segment above

Show your work on your trace file

In: Computer Science