Pi Calculation implementation.
Pi = 4 * (1/1 – 1/3 + 1/5 – 1/7 + 1/9 … (alternating + -) (1/n))
First, Pi should be a double to allow many decimals. Notice that the numerator is always 1 or -1. If you started the numerator at -1, then multiply each term by -1. So if started the numerator at -1 and multiplied it by -1, the first numerator will be 1, then the next the numerator will be -1, alternating + and -.
Then notice that the denominator goes from 1,3,5,7,9 etc. So this is counting by 2s starting at one. For loops are good when you know how many times it will go through the loop. So a for loop might be something like:
for (long denom=1; denom <n;
denom=denom+2)
where denom(inator) is the term that changes by 2 starting at 1
(not zero). We use a long to allow very large numbers
We are using longs, so we can have very long numbers. Likewise, PI should be a double (not a float) to have a very large decimal accuracy
Write a c++ program to calculate the approximate value of pi using this series. The program takes an input denom that determines the number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough to give you an accurate estimation of PI. You will try it with numbers read in from a file. to see the accuracy increase. Use a while loop to read in number of values from a file. Then inside that loop, use a for loop for the calculation.
In: Computer Science
1) given the 8-bit binary number of 10111110 (two)
a)what is the hex representation of this number/
b) what is the decimal value if this number which is an 8-bit number as a two complement signed number. all steps included
c)what is the decimal value of this number as an 8-bit unsigned number, please write all steps
2) given the decimal number (-0.875) or (-7/8) convert it to single-precision floating-point. please show the easiest way to get to this,.
In: Computer Science
In JAVA PLEASE
With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to an equation f(x) = 0 for some function f. Use the bisection method. To solve the problem using this method first find two values of x, A and B, such that when evaluated in the function f(x) they give opposites signs for the y value. If f(x) is continuous between these two values then we know that there is at least one x which evaluates to a 0 y value, which is between these two values A and B. Treat the positive value as an upper bound and the negative value as a lower bound. Divide the space between A and B in half and evaluate the function at that new point. If the value is positive than it replaces the existing upper-bound and if it is negative it replaces the existing lower-bound. Continue dividing the space between the upper-bound and lower-bound in half and evaluating this new value and generating new upper and lower bounds as the case may be. Continue the evaluation process until the x value that you are plugging into the function evaluates to a y value that is zero plus or minus .0000001.
Consider the possibility of finding all the real roots of any given function up to and including x raised to the fifth power. Input should consist of reading the coefficients one at a time to the powers of x up to 5 and some constant. Do a desk check with calculator on y = X2 -2 and identify the variables associated with that problem. Write the code that follows your algorithm. Then test your program on other polynomials such as 2x5 -15x4 + 35x3 -15x2-37x + 30 (roots are -1, 1, 2, 2.5, 3) and 3x5 -17x4 + 25x3 + 5x2 -28x + 12 (roots are -1,1, 2/3, 2, 3). Use at lest 3 methods. One to read the 5 coefficients, one to calculate the value of the polynomial and one to do the binary bisection search. Use the following for loop to work through the X values:for(double x = -5.0000001; x < 5.0000001; x = x + .1)
OUTPUT SHOULD BE LIKE THIS
---------------------------
Please enter the number of coefficients : 5
PLEASE ENTER 5TH COEFFICIENT
2
PLEASE ENTER 4TH COEFFICIENT
-15
PLEASE ENTER 3RD COEFFICIENT
35
PLEASE ENTER 2ND COEFFICIENT
-15
PLEASE ENTER 1ST COEFFICIENT
-37
PLEASE ENTER ZERO COEFFICIENT
30
root is -0.99999999999947171
root is 1.0000000004308118
root is 2.0000000019209287
root is 2.500000000019
root is 2.999999999989406
-------------------------------
JUST LIKE THAT, DO IT WITHOUT ENTERING ANY BRAKETING NUMERS. NO BRAKETING NUMERS, PLEASE.
Do what way you want, as Long As You got the Output like that.
THANK YOU SO MUCH
In: Computer Science
I cant get this to compile in java idk if im making the wrong file or what but this si the code I was given I would like stsep by step java instuctions and which compiler to use to execute this code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
// We are going to create a Game of 15 Puzzle with Java 8 and
Swing
// If you have some questions, feel free to ue comments ;)
public class GameOfFifteen extends JPanel { // our grid will be
drawn in a dedicated Panel
// Size of our Game of Fifteen instance
private int size;
// Number of tiles
private int nbTiles;
// Grid UI Dimension
private int dimension;
// Foreground Color
private static final Color FOREGROUND_COLOR = new Color(239, 83,
80); // we use arbitrary color
// Random object to shuffle tiles
private static final Random RANDOM = new Random();
// Storing the tiles in a 1D Array of integers
private int[] tiles;
// Size of tile on UI
private int tileSize;
// Position of the blank tile
private int blankPos;
// Margin for the grid on the frame
private int margin;
// Grid UI Size
private int gridSize;
private boolean gameOver; // true if game over, false
otherwise
public GameOfFifteen(int size, int dim, int mar) {
this.size = size;
dimension = dim;
margin = mar;
// init tiles
nbTiles = size * size - 1; // -1 because we don't count blank
tile
tiles = new int[size * size];
// calculate grid size and tile size
gridSize = (dim - 2 * margin);
tileSize = gridSize / size;
setPreferredSize(new Dimension(dimension, dimension +
margin));
setBackground(Color.WHITE);
setForeground(FOREGROUND_COLOR);
setFont(new Font("SansSerif", Font.BOLD, 60));
gameOver = true;
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// used to let users to interact on the grid by clicking
// it's time to implement interaction with users to move tiles to
solve the game !
if (gameOver) {
newGame();
} else {
// get position of the click
int ex = e.getX() - margin;
int ey = e.getY() - margin;
// click in the grid ?
if (ex < 0 || ex > gridSize || ey < 0 || ey >
gridSize)
return;
// get position in the grid
int c1 = ex / tileSize;
int r1 = ey / tileSize;
// get position of the blank cell
int c2 = blankPos % size;
int r2 = blankPos / size;
// we convert in the 1D coord
int clickPos = r1 * size + c1;
int dir = 0;
// we search direction for multiple tile moves at once
if (c1 == c2 && Math.abs(r1 - r2) > 0)
dir = (r1 - r2) > 0 ? size : -size;
else if (r1 == r2 && Math.abs(c1 - c2) > 0)
dir = (c1 - c2) > 0 ? 1 : -1;
if (dir != 0) {
// we move tiles in the direction
do {
int newBlankPos = blankPos + dir;
tiles[blankPos] = tiles[newBlankPos];
blankPos = newBlankPos;
} while(blankPos != clickPos);
tiles[blankPos] = 0;
}
// we check if game is solved
gameOver = isSolved();
}
// we repaint panel
repaint();
}
});
newGame();
}
private void newGame() {
do {
reset(); // reset in intial state
shuffle(); // shuffle
} while(!isSolvable()); // make it until grid be solvable
gameOver = false;
}
private void reset() {
for (int i = 0; i < tiles.length; i++) {
tiles[i] = (i + 1) % tiles.length;
}
// we set blank cell at the last
blankPos = tiles.length - 1;
}
private void shuffle() {
// don't include the blank tile in the shuffle, leave in the solved
position
int n = nbTiles;
while (n > 1) {
int r = RANDOM.nextInt(n--);
int tmp = tiles[r];
tiles[r] = tiles[n];
tiles[n] = tmp;
}
}
// Only half permutations o the puzzle are solvable
// Whenever a tile is preceded by a tile with higher value it
counts
// as an inversion. In our case, with the blank tile in the solved
position,
// the number of inversions must be even for the puzzle to be
solvable
private boolean isSolvable() {
int countInversions = 0;
for (int i = 0; i < nbTiles; i++) {
for (int j = 0; j < i; j++) {
if (tiles[j] > tiles[i])
countInversions++;
}
}
return countInversions % 2 == 0;
}
private boolean isSolved() {
if (tiles[tiles.length - 1] != 0) // if blank tile is not in the
solved position ==> not solved
return false;
for (int i = nbTiles - 1; i >= 0; i--) {
if (tiles[i] != i + 1)
return false;
}
return true;
}
private void drawGrid(Graphics2D g) {
for (int i = 0; i < tiles.length; i++) {
// we convert 1D coords to 2D coords given the size of the 2D
Array
int r = i / size;
int c = i % size;
// we convert in coords on the UI
int x = margin + c * tileSize;
int y = margin + r * tileSize;
// check special case for blank tile
if(tiles[i] == 0) {
if (gameOver) {
g.setColor(FOREGROUND_COLOR);
drawCenteredString(g, "\u2713", x, y);
}
continue;
}
// for other tiles
g.setColor(getForeground());
g.fillRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.BLACK);
g.drawRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.WHITE);
drawCenteredString(g, String.valueOf(tiles[i]), x , y);
}
}
private void drawStartMessage(Graphics2D g) {
if (gameOver) {
g.setFont(getFont().deriveFont(Font.BOLD, 18));
g.setColor(FOREGROUND_COLOR);
String s = "Click to start new game";
g.drawString(s, (getWidth() - g.getFontMetrics().stringWidth(s)) /
2,
getHeight() - margin);
}
}
private void drawCenteredString(Graphics2D g, String s, int x, int
y) {
// center string s for the given tile (x,y)
FontMetrics fm = g.getFontMetrics();
int asc = fm.getAscent();
int desc = fm.getDescent();
g.drawString(s, x + (tileSize - fm.stringWidth(s)) / 2,
y + (asc + (tileSize - (asc + desc)) / 2));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawGrid(g2D);
drawStartMessage(g2D);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Game of Fifteen");
frame.setResizable(false);
frame.add(new GameOfFifteen(4, 550, 30),
BorderLayout.CENTER);
frame.pack();
// center on the screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
In: Computer Science
In: Computer Science
In: Computer Science
intrusion Detection course
a long and comprehensive explanation by typing please
Why is wireless security important?
Explain the significance of wireless security.
Name 3 attacks relevant to wireless security and how to protect against them.
In: Computer Science
1) Write a single function CalcArea( ) that has no input/output arguments. Inside the function, you will first print a “menu” listing 2 items: “rectangle”, “circle”. It prompts the user to choose an option, and then asks the user to enter the required parameters for calculating the area (i.e., the width and length of a rectangle, the radius of a circle) and then prints its area. The script simply prints an error message for an incorrect option.
Here are two examples of running it:
>> CalcArea
Menu
1. Rectangle
2. Circle
Please choose one:2
Enter the radius of the circle: 4.1
The area is 52.81.
>> CalcArea
Menu
1. Rectangle
2. Circle
Please choose one: 1
Enter the length: 4
Enter the width: 6
The area is 24.00.
In: Computer Science
Write a C# program using Repl.it that asks the user for a numeric grade between 0 and 100, and displays a letter grade using the following conversion table:
Numeric grade Letter grade
90 < grade <= 100 A
80 < grade <= 90 B
70 < grade <= 80 C
60< grade <= 70 D
grade <= 0.0 F
Don’t forget the comments in your code. The output should be similar to: Enter the test score: 87 The letter grade for a score of 87 is B. Submit the repl.it link to the text submission area of the assignment dropbox.
In: Computer Science
Discuss how companies use social media channel "influencers" to promote their product. Provide a link to a specific example that illustrates your ideas.
In: Computer Science
In: Computer Science
Complete the following tasks:
In: Computer Science
Problem 5. It is the next installment of the ECE 5484 Scavenger Hunt! Two pioneers of early computers and computer organization were Howard H. Aiken and John von Neumann. Answer the questions below regarding Aiken and von Neumann. Cite sources used for your answers. a) Is Aiken or von Neumann associated with the so-called Princeton architecture? b) Is Aiken or von Neumann associated with the so-called Harvard architecture? c) Briefly, and in your own words, what is the key feature of a Princeton architecture computer compared to the Harvard architecture? d) Does the MARIE architecture owe more to Aiken or von Neumann? In your own words, briefly justify your answer
In: Computer Science
To understand the value of counting loops:
Sample Result is shown below:
Enter the number of rows of matrix A: 2
Enter the number of columns of matrix A: 3
Enter the number of columns of matrix B: 3
Enter the number of columns of matrix B: 4
Enter matrix A;
1 2 3
4 5 6
Enter matrix B:
7 8 9 10
11 12 13 14
15 16 17 18
Matrix A:
1 2 3
4 5 6
Matrix B:
7 8 9 10
11 12 13 14
15 16 17 18
Product of matrix A and Matrix B ( A x B) :
74 80 86 92
173 188 203 218
In: Computer Science
In: Computer Science