Python Programming, Below included the Skeleton, input and output. Could you also include indents in the solutions.
In this programming laboratory you will create a Python program that evaluates the sum of the following mathematical series:
Sum = x0 / 0! + x1 / 1! + x2 / 2! + x3 / 3! + … + xn / n!
The variable x is a real (float) number and the variable n is an integer that can assume values greater than or equal to 0. You program is to consist of a main program that allows the user to input values of x and n and then executes a loop with an index going from 1 to n. Your program should also include two functions, factorial and power, that calculate (respectively) factorial values and numbers to a power. The main program should call the factorial and power functions, from within the loop, to evaluate the expression for relevant values and add the value of the current term to the running sum. You may use the factorial function that was introduced in the examples in this week’s module (included in the skeleton of the program). You will need to write your own power function. The power function should compute x to the 0 power or x to positive integer powers. Do not worry about negative powers of x.
# Function to compute x to the k power
def power(x,k):
You will need to complete this function here
If x is 0 then return 0
Otherwise if n is 0 return 1
Otherwise use a loop to multiply x times itself n times and return the product
# The function below is the factorial function used in the Week Six examples
def factorial(num):
if num < 0:
print('Number must be >= 0')
exit()
else:
fac = 1
while num >= 1:
fac = fac * num
num = num - 1
return fac
# Main program
sum = 0.0
x = input("Enter x value ")
x = float(x)
n = input("Enter n value ")
n = int(n)
if n < 0:
print("n must be < 0")
quit()
…
You will need to complete your main
program here. It will
need to include a while loop that goes from 0 to n and calls to the
factorial and power functions.
After the loop, you will need to print the sum value
Your program should produce the results shown below when executed. Multiple executions with different x and n values are displayed.
Enter x value 2
Enter n value 0
Sum is 1.0
Enter x value 2
Enter n value 1
Sum is 3.0
Enter x value 2
Enter n value 2
Sum is 5.0
Enter x value 2
Enter n value 3
Sum is 6.333333333333333
In: Computer Science
You are to write a program that grades students (Maybe I will use it on you). Your program must open and read in "input.txt" (an example of which is below). It will contain the students name and their test scores. You are to calculate the students final grade in the course. Take the average of their test scores and output their average to a file with the below format. For the output.txt ensure that the formatting is in the block style shown below. Test scores should always be shown to the second decimal place. input txt: (Meryl Streep 100 105 98.5 99.5 106)
output txt : (Meryl StreepTest scores:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Average score: .......................101.80Test score 1: ........................100.00Test score 2: ........................105.00Test score 3: .........................98.50Test score 4: .........................99.50Test score 5: ........................106.00~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~) Input fields: <First Name> <Last Name> <Score on Test 1> <Score on Test 2> <Score on Test 3> <Score on Test 4> <Score on Test 5>
In: Computer Science
List.h
template
class List { // List class ADT
public:
virtual void clear() = 0;
// Inserts an item into the list at position index
virtual bool insert(const ListItemType& newItem) =
0;
// Append "it" at the end of the list
// The client must ensure that the list's capacity is
not exceeded
virtual bool append(const ListItemType& newItem) =
0;
// Deletes an item from the list at a given
position
virtual ListItemType remove() = 0;
// Set the current position to the start of the
list
virtual void moveToStart() = 0;
// Set the current position to the end of the
list
virtual void moveToEnd() = 0;
// Move the current position one step left, no
change if already at beginning
virtual void prev() = 0;
// Move the current position one step right, no
change if already at end
virtual void next() = 0;
//Return the number of items stored in the
list
virtual int length() const = 0;
// Return the position of the current element
virtual int currPos() const = 0;
// Set the current position to "pos"
virtual bool moveToPos(int pos) = 0;
// Return true if current position is at end of the
list
virtual bool isAtEnd() const = 0;
// Return the current element
virtual ListItemType getValue() const = 0;
// Note: The fololwing methods assume that ListItemType is comparable: that is to say the relational operators such as == exist.
//search for the first occurance in the list of an
item. If found return true and set curr to the location.
// if not found, return false and set curr to the end
of the list.
virtual bool find(const ListItemType& newItem) =
0;
//search for the next occurance in the list of an
item. Start with the next position after curr. If found return true
and set curr to the location.
// if not found, return false and set curr to the end
of the list.
virtual bool findNext(const ListItemType& newItem)
= 0;
// count the number of times a value is found in
the list
// curr should be set back to its current location
after the count.
virtual int count(const ListItemType& newItem) =
0;
};
LList.h
#include "list.h"
// Linked list implementation
template
class Node
{
public:
ListItemType element;
Node* next;
Node()
{
next = nullptr;
}
Node(const ListItemType& e)
{
this->element = e;
next = nullptr;
}
};
template
class LList : public List {
private:
Node* head;
Node* tail;
Node* curr;
int listSize;
public:
// Constructor
LList() {
head = tail = curr = nullptr;
listSize = 0;
}
//Destructor
~LList() {
clear();
}
// Remove all elements
void clear() {
if (listSize > 0) {
Node*
temp;
curr =
head;
do {
temp = curr -> next;
delete curr;
curr = temp;
} while (curr !=
nullptr);
}
curr = tail = head = nullptr;
listSize = 0;
}
bool insert(const ListItemType& newItem) {
if (listSize == 0) {
head = new
Node(newItem);
tail =
head;
curr =
head;
}
else if (curr == head) {
Node* temp = new
Node(newItem);
temp->next =
head;
head =
temp;
curr =
head;
}
else if (curr == nullptr) {
return
false;
}
else {
Node* temp =
head;
while
(temp->next != curr)
temp = temp->next;
temp->next =
new Node(newItem);
temp->next->next = curr;
curr =
temp->next;
}
listSize++;
return true;
}
// Append "it" to list
bool append(const ListItemType& newItem) {
// hint: handle an empty list and a
non-empty list separately
cout << "*****
complete this method ...\n";
return false;
}
// Remove and return current element
ListItemType remove() {
ListItemType it =
curr->element;
if (curr == head) {
head =
head->next;
delete
curr;
curr =
head;
listSize--;
if (head ==
nullptr)
tail = nullptr;
}
else if (curr == tail) {
moveToPos(listSize - 2);
delete
curr->next;
tail =
curr;
tail->next =
nullptr;
curr =
nullptr;
listSize--;
}
else {
Node* temp =
head;
while
(temp->next != curr)
temp = temp->next;
temp->next =
curr->next;
delete
curr;
curr =
temp->next;
listSize--;
}
return it;
}
void moveToStart() { curr = head; }
void moveToEnd() { curr = nullptr; }
void prev() {
if (head == curr) return;
Node* temp = head;
while (temp -> next != curr)
temp = temp -> next;
curr = temp;
}
void next() { if (curr != nullptr) curr = curr ->
next; }
int length() const { return listSize; }
int currPos() const {
if (curr == nullptr) return
listSize;
Node* temp = head;
int i;
for (i = 0; curr != temp;
i++)
temp = temp
-> next;
return i;
}
// Move down list to "pos" position
bool moveToPos(int pos) {
if ((pos < 0) || (pos >
listSize)) return false;
curr = head;
for (int i = 0; i next;
return true;
}
// Return true if current position is at end of the
list
bool isAtEnd() const { return curr == nullptr; }
// Return current element value.
ListItemType getValue() const { return curr ->
element; }
// Check if the list is empty
bool isEmpty() const { return listSize == 0; }
bool find(const ListItemType&
it) {
cout << "*****
complete this method ...\n";
// hint: if list is empty return
false
// set curr to head and traverse
list until curr becomes nullptr or "it"is found
return false;
}
//search for the next occurance in the list of an
item. Start with the next position after curr. If found return true
and set curr to the location.
// if not found, return false and set curr to the end
of the list.
bool findNext(const ListItemType&
it) {
cout << "*****
complete this method ..\n";
// hint: if curr is nullptr return
false
// set curr to curr->next and
traverse list until curr becomes nullptr or "it" is found
return false;
}
// count the number of times a value is found in the
list
// curr should remain where it is
int count(const ListItemType& it)
{
cout << "*****
complete this method ...\n";
// hint: do not use curr, but a
temp pointer to traverse the list.
// set temp to head, and traverse
list until temp is nullptr counting elements that are equal
int count = 0;
return count;
}
};
1- Complete the append method in LList.h( Bold
)
2- Complete the find, findNext and count in LList.h(Bold)
In: Computer Science
In: Computer Science
Using the bisection method, find the root of the following function:
f(x)=cos(2x) - x
Use the following initial values: xl=0 and xu=2
NOTE: Perform only 3 iterations.
In: Computer Science
Write a c++ function named multi_table to print out multiplication table for a given number of rows and columns. Your program should print a header row and a header column to represent each row and column number. For example your function call multi_table(4, 4) would print below to the command line:
1 | 1 2 3 4
_________
2 | 2 4 6 8
3 | 3 6 9 12
4 | 4 8 12 16
Test your function in a driver program for 12 by 12 multiplication table.
In: Computer Science
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses. Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wager, the program should calculate whether the player would win or lose. The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games. Add counters that count how many times the player wins and how many times the player loses. At the end of the 10,000 games, compute the probability of winning [i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who is going to win the most games, you or the house?
In: Computer Science
8,2,10,10,8,10,2,8,,8,10,2,8,1,1,8
Trace the above using (3 way partitioning) quick sort java
In: Computer Science
In: Computer Science
Using C:
Implement function types that takes no input, declares 3 variables of type char, 3 of type short, 3 of type int, and 3 of type double---in that order---and prints the addresses of the 12 variables---in the same order---in both hex (use %p conversion instruction) and unsigned long format.
&a1 = 0x7ffd45e3ac0f, 140725776002063
&a2 = 0x7ffd45e3ac0e, 140725776002062
&a3 = 0x7ffd45e3ac0d, 140725776002061
&b1 = 0x7ffd45e3ac0a, 140725776002058
&b2 = 0x7ffd45e3ac08, 140725776002056
...
In: Computer Science
Python program
A number game machine consists of three rotating disks labelled with the numbers 0 to 9 (inclusive). If certain combinations of numbers appear when the disks stop moving, the player wins the game.
The player wins if
otherwise the player loses.
Write a Python program to simulate this number game by randomly generating 3 integer values between 0 and 9 (inclusive), and printing whether the player has won or lost the game.
To randomly generate a number between and including 0 and 9, use the randintmethod from random library module. For example, x = random.randint(1,100)will automatically generate a random integer between 1 and 100 (inclusive) and store it in x.
Sample output 1:
Random numbers: 2 3 5 You lose.
Sample output 2:
Random numbers: 7 7 0 You win!
In: Computer Science
create a menu in Java. A menu is a presentation of options for you to select. The article above that is in our discussion board may be helpful.
You can do this in the main() method.
1. Create a String variable named menuChoice.
2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop.
A. Buy Stock. B. Sell Stock X. Exit Enter your Selection:
3. Prompt for the value menuChoice.
4. If menuChoice is "A", display "Buy Stock" and redisplay the menu.
If menuChoice is "B", display "Sell Stock" and redisplay the menu.
If menuChoice is "X", the loop needs to terminate.
If any other choice is given, display "Invalid selection" and redisplay the menu.
For grading, I will test with uppercase characters. Review section 5.1 for String comparisons.
Convert the menuChoice to an upper case String after input or as part of the input but before any processing is done. This will allow me to enter 'a' but process as 'A'. You can Google for "Java string uppercase" to see how this is done.
For Task 4 - Create 2 void methods - 1 for option A to call, and another for option B to call. Do the display work only inside these methods and call these methods for these options.
In: Computer Science
Write a JavaFX application that draws 5 squares. Use a random number generator to generate random values for the size, x, and y. Make the size between 100 and 200, x between 0 and 600, y between 0 and 400. You can pick any color, but you must use different colors for the 5 squares. Set your window to 600 by 400.
In: Computer Science
Using C:
Implement four versions of a function funSum that takes a positive integer n as input and returns the sum of all integers up to and including n that are divisible by 6 or 7: using a for loop in funSum1, using a while loop in funSum2, using a do-while loop in funSum3, and using recursion in funSum4. Your output for the included test code should be:
funSum1(20) = 57
funSum2(20) = 57
funSum3(20) = 57
funSum4(20) = 57
In: Computer Science
Look at the C code below. Identify the statements that contain a syntax error OR logical error. Evaluate each statement as if all previous statements are correct. Select all that apply
#include <stdio>
Int main()
{
Float webbing;
Puts(“Please enter the amount of webbing per cartridge: “)
Scanf(“%f”, webbing);
Printf(“You entered: “+ webbing + “\n”);
Return 0;
}
Answers:
Including the library for I/O
Declaring a variable
Writing output to the console using puts
Getting input from the console
Writing output to the console using printf
In: Computer Science