#include <iostream>
#include <string>
#include <ctime>
using namespace std;
void displayArray(double * items, int start, int end)
{
for (int i = start; i <= end; i++)
cout << items[i] << " ";
cout << endl;
}
//The legendary "Blaze Sort" algorithm.
//Sorts the specified portion of the array between index start and end (inclusive)
//Hmmm... how fast is it?
/*
void blazeSort(double * items, int start, int end)
{
if (end - start > 0)
{
int p = filter(items, start, end);
blazeSort(items, start, p - 1);
blazeSort(items, p + 1, end);
}
}
*/
int main()
{
////////////////////////////////////////////////////
//Part 1: Implement a method called filter.
////////////////////////////////////////////////////
//Filter is a function that takes in an array and a range (start and end).
//
//Call the first item in the range the 'pivot'.
//
//Filter's job is to simply separate items within the range based on whether they are bigger or smaller than the pivot.
//In the example array below, 13 is the pivot, so all items smaller than 13 are placed in indices 0-3. The pivot is then placed at index 4, and all
//remaining items, which are larger than the pivot, are placed at positions 5-10. Note that the array is NOT sorted, just "partitioned" around
//the pivot value. After doing this, the function must return the new index of the pivot value.
double testNumsA[] = { 13, 34.1, 43, 189, 4, 4.5, 18.35, 85, 3, 37.2, 5 };
//The filter will place all items <= 13 to the left of value 13, and all items large than 13 to the right of 13 in the array.
int p = filter(testNumsA, 0, 10);
cout << p << endl; //should be 4, the new index of 13.
displayArray(testNumsA, 0, 10); //should display something like this: 5 3 4.5 4 13 18.35 85 189 37.2 43 34.1
//One more example:
double testNumsB[] = { 13, 34.1, 43, 189, 4, 4.5, 18.35, 85, 3, 37.2, 5 };
p = filter(testNumsB, 2, 6); //Here we are only interested in items from indices 2-6, ie, 43, 189, 4, 4.5, 18.35
cout << p << endl; //should be 5
displayArray(testNumsB, 0, 10); //Notice only indices 2-6 have been partioned: 13 34.1 18.35 4 4.5 43 189 85 3 37.2 5
/////////////////////////////////////////////////////////////////////////////////
//Part 2: Uncomment "Blaze Sort".
//Blaze Sort uses/needs your filter to work properly.
/////////////////////////////////////////////////////////////////////////////////
//Test if Blaze Sort correctly sorts the following array.
double testNums[] = { 13, 34.1, 43, 189, 4, 4.5, 18.35, 85, 3, 37.2, 5 };
blazeSort(testNums, 0, 10);
displayArray(testNums, 0, 10);
/////////////////////////////////////////////////////////////////////
//Part 3: Test how fast Blaze Sort is for large arrays.
//What do you think the run-time (big-Oh) of blaze sort is?
/////////////////////////////////////////////////////////////////////
//Stress test:
int size = 100; //test with: 1000, 10000, 100000,1000000, 10000000
double * numbers = new double[size];
for (int i = 0; i < size; i++)
{
numbers[i] = rand();
}
clock_t startTime, endTime;
startTime = clock();
blazeSort(numbers, 0, size - 1);
endTime = clock();
displayArray(numbers, 0, size - 1);
cout << "Blaze sort took: " << endTime - startTime << " milliseconds to sort " << size << " doubles." << endl;
////////////////////////////////////////////////////////////////////
//Part 4: Sort Moby Dick, but this time with Blaze Sort
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//1) Create a second version of Blaze Sort that sorts arrays of strings
//(instead of arrays of doubles).
//2) Download whale.txt from the previous lab. Read the words from the file into
//an array, sort the array with Blaze Sort, and then write the sorted words to an output file.
//
//This time, it has to be fast! Must finish in under 10 seconds.
return 0;
}In: Computer Science
What are different methods by which you can compute the PCA? Does every method will yield same result or answer at the end?
In: Computer Science
P4.2.7 Write a script that draws a bullseye with n concentric rings. The kth ring should have inner radius k – 1 and outer radius k. (Thus, the innermost ring is just a radius-1 disk.) The kth ring should be colored white if k is odd and red if k is even.
This is a matlab problem
In: Computer Science
In this assignment, you are given partial code and asked to finish the rest according to the specified requirement.
Problem statement: one benefit of inheritance is the code reuse. Instead of writing everything from scratch, you can write a child class that reuse all the features already existed in its parent, grandparent, great grandparent, etc. In the child class, you only need to supply small amount of code that makes it unique. In the following, you are given code for two classes: Coin and TestCoin. You study these classes first and try to understand the meaning of every line. You can cut and paste the code to Eclipse and run it.
The following Java codes are adapted from (John Lewis and William Loftus (2007). “Software Solutions Foundations of Program Design, 5th edition”, Pearson Education, Inc. pp. 220-222).
public class Coin {
private final int HEADS = 0;
private final int TAILS = 1;
private int face;
public Coin(){
flip();
}
public void flip(){
face = (int)(Math.random()*2);
}
public boolean isHeads(){
return (face == HEADS);
}
public String toString(){
String faceName;
if(face == HEADS){
faceName = "Heads";
}else{
faceName = "Tails";
}
return faceName;
}
}
public class TestCoin {
public static void main(String[] args) {
Coin myCoin = new Coin();
myCoin.flip();
System.out.println(myCoin);
if(myCoin.isHeads()){
System.out.println("The coin has head and you win.");
}else{
System.out.println("The coin has tail and you lose.");
}
}
}
To help your understanding, I give the following brief explanation. The Coin class stores two integer constants (HEADS and TAILS) that represent the two possible states of the coin, and an instance variable called face that represents the current state of the coin. The Coin constructor initially flips the coin by calling the flip method, which determines the new state of the coin by randomly choosing a number (either 0 or 1). Since the random() method returns a random value in the range [0-1) (the left end is closed and the right end is open), the result of the expression (int)(Math.random()*2) is a random 0 or 1. The isHeads() method returns a Boolean value based on the current face value of the coin. The toString() method uses an if-else statement to determine which character string to return to describe the coin. The toString() method is automatically called when the myCoin object is passed to println() in the main method.
The TestCoin class instantiates a Coin object, flips the coin by calling the flip method in the Coin object, then uses an if-else statement to determine which of two sentences gets printed based on the result.
After running the above two classes, the following is one of the possible outputs:
Tails
The coin has tail and you lose.
Design and implement a new class MonetaryCoin that is a child of the Coin class
The new class should satisfy the following requirement. Besides all the features existed in the Coin class, the MonetaryCoin will have an extra instance variable called value to store the value of a coin. The variable value is the type of integer and has the range of 0-10. The derived class should also have an extra method getValue() that returns the value. The new class should have two constructors: one is the no-argument constructor MonetaryCoin(), the other takes one parameter aValue of integer that is used to initialize the instance variable value. According to good practice, in the body of constructor, the first line should call its super class’ no-arg constructor. To demonstrate that your new class not only has the old feature of keeping track of heads or tails but also has the new feature of keeping track of value, you should overwrite/override the method toString(). The specification for the new toString() is:
Header: String toString()
This method gives values of all instance variables as a string.
Parameters: no.
Precondition: no.
Returns: a string that takes one of the following value:
If the face is 0, value is x (here it represents an integer in the range 0..10).
The returned string should be “The coin has head and value is x.”
If the face is 1, value is x,
The returned string should be “The coin has tail and value is x.”
As a summary, this class should have four methods:
Constructor: public MonetaryCoin()
Constructor: public MonetaryCoin(int aValue)
Getter method: public int getValue()
toString method: public String toString()()
Requirements
Grading
Your grade in this assignment depends on the following:
Meet the requirement.
Required Work
You are asked to the following to get points:
In order to check the correctness of your code, I give you the following driver class. You must use this driver class to get full credit.
public class TestCoin {
public static void main(String[] args) {
int totalHeads = 0;
int totalValue = 0;
MonetaryCoin mCoin = new MonetaryCoin(1);
mCoin.flip();
System.out.println(mCoin);
if(mCoin.isHeads()) {
totalHeads += 1;
}
totalValue += mCoin.getValue();
mCoin = new MonetaryCoin(10);
mCoin.flip();
System.out.println(mCoin);
if(mCoin.isHeads()) {
totalHeads += 1;
}
totalValue += mCoin.getValue();
mCoin = new MonetaryCoin(2);
mCoin.flip();
System.out.println(mCoin);
if(mCoin.isHeads()) {
totalHeads += 1;
}
totalValue += mCoin.getValue();
mCoin = new MonetaryCoin(3);
mCoin.flip();
System.out.println(mCoin);
if(mCoin.isHeads()) {
totalHeads += 1;
}
totalValue += mCoin.getValue();
System.out.println("The total heads is " + totalHeads);
System.out.println("The total value is " + totalValue);
}
}
The driver class instantiates four MonetaryCoin objects. It has two variables (totalHeads, totalValue) to keep track of the total number of heads and the accumulative value. Finally, it prints two summary information about the total heads and the total value.
2. Using UML notation, draw the class diagram in the space below (your class diagram must match your code, your class diagram should contain TestCoin, Coin, MonetaryCoin and the relationships among them) (10 pts).
SUPPLY THE CLASS DIAGRAM IN THE SPCE BELOW TO SCORE YOUR POINTS:
3. Choose one method from the MonetaryCoin class and give one paragraph of description in the space below. You can focus on one or two of the following areas: 1) functional perspective such as what the method will do; or 2) implementation perspective such as whether the method is overloaded, etc.
In: Computer Science
MySQL: What are the various SELECT statement clauses? Which ones are necessary and optional? How are the results filtered using COMPARISON and LOGICAL OPERATORS? Give one of these operators examples. Describe how parentheses can influence a query result.
In: Computer Science
Define the following terms and security objectives and give examples:
Confidentiality
Integrity
Availability
Authentication
Authorization
In: Computer Science
Write 3 vulnerabilities related to transport layer protocols and processes. Explain each vulnerability and it's significance to your data security or privacy.
In: Computer Science
United States v. Barrington===Computer Crime.
Make an argument. Do you agree or disagree.
Search the internet for this case, then search the book's PDF to find it.
In: Computer Science
Use Zeller’s algorithm to figure out the days of the week for a given date in c.
W = (d + 13(m+1)/5 + y + y/4 + c/4 - 2c + 6) mod 7
Y is the year minus 1 for January or February, and the year for any other month
y is the last 2 digits of Y
c is the first 2 digits of Y
d is the day of the month (1 to 31)
m is the shifted month (March=3,...January = 13, February=14)
w is the day of week (0=Sunday,..,6=Saturday)
Output should look the following:
Please enter the day [YYYY/Month/Day]: 2020/1/1
Wednesday
In: Computer Science
If none of the above conditions are met, simply print a blank new line
In: Computer Science
•Formal definition as a search problem (tic tac toe):
–Initial state: ??
–Player(s): ??
–Actions(s): ??
–Result(s,a): ??
–(2nd ed.: Successor function: ??
–Terminal-Test(s): ??
–Utility function(s,p): ??
•E.g., win (+1), lose (-1), and draw (0) in tic-tac-toe.
In: Computer Science
/*You will be have to round numbers very often so you decided to
create your own function round_off()
that will receive the number to be rounded and the number of
decimal digits that the number should be rounded to and will
return
the value rounded to the specified number of decimal digits. You
need to create a program to test the function.
It will ask the user to enter the double precision real number to
be rounded and a whole number indicating the number of decimal
digits.
It will then display the original number with ten digits and the
rounded value (also a double precision real number) with the number
of
digits specified by the user plus 2 more.
This assignment will be completed in two steps:
1) First you will implement the algorithm shown below in which the
rounding will be done in main()
2) Once you have this working you will need to modify your solution
so you: Declare the prototype of the function above main()
Call the function in main() to do the rounding Define the function
below main()
*/
#include <iostream>
// to use cin and cout
#include <typeinfo>
// to be able to use operator
typeid
// Include here the libraries that your program needs to compile
using namespace std;
// Ignore this; it's a little function used for making
tests
inline void _test(const char* expression, const char* file, int
line)
{
cerr << "test(" << expression << ")
failed in file " << file;
cerr << ", line " << line << "."
<< endl << endl;
}
// This goes along with the above function...don't worry about
it
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 :
_test(#EXPRESSION, __FILE__, __LINE__))
// Insert here the prototype of the function here
int main()
{
// Declare variable value, valuero that hold double precision real
numbers
double value;
double valuero;
// Declare variable decdig that holds whole numbers
int decdig;
// Prompt the user to "Enter the real number: "
cout << "Enter the real number: ";
// Read from keyboard the value entered by the user and assign it
to side
cin >> value;
// Prompt the user to "Enter number of digits: "
cout << "Enter number of digits: ";
// Read from keyboard the value entered by the user and assign it
to decdig
cin >> decdig;
// Round the real number to the number of decimal digits specified
and assign the result to valuero
// Format the output to display the numbers in fixed format with ten decimal digits
// Display on the screen, using 23 columns, the message
// "The original number is ", value
// Format the output to display the numbers in fixed format with the number of decimal digits specified plus 2
// Display on the screen, using 23 columns, the message
// "The rounded number is ", valuero
system("pause");
// Do NOT remove or modify the following statements
cout << endl << "Testing your solution"
<< endl << endl;
test(typeid(value) == typeid(1.));
//
Incorrect data type used for value
test(typeid(valuero) == typeid(1.));
// Incorrect data type used
for valuero
test(typeid(decdig) == typeid(1));
//
Incorrect data type used for decdig
/*
test(fabs(round_off(125.123456789,2) - 125.12 ) <
0.001);
// Incorrect rounding to two decimal
digits
test(fabs(round_off(125.123456789,4) - 125.1235) <
0.00001); //
Incorrect rounding to four decimal digits
test(fabs(round_off(125.987654321,0) - 126.) <
0.001);
// Incorrect rounding to no
decimal digits
test(fabs(round_off(125.987654321, 5) - 125.98765)
< 0.000001);
// Incorrect rounding to five decimal digits
*/
system("pause");
return 0;
}
//************************ Function definition
*************************
// Read the handout carefully for detailed description of the
functions that you have to implement
// Rounds the value received in the first parameter to the
number of digits received in the second parameter
In: Computer Science
Write a MIPS Assembly Language program to perform the following operations:
int RecurseFunc( int Dn, int Up )
{
if( Dn < 1 ) return 0;
return Dn * Up + RecurseFunc( Dn - 1, Up + 1 );
}
In: Computer Science
Java
Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the method is called, it will return “nSmith45” as the user name.
In: Computer Science
write a c code to test the 3rd and 8th bit set or not
(1 or 0).
if both set, display "true";
if either one set, display "half true";
if neither set, display "false".
In: Computer Science