Question

Problem 4. (Root Finding) Write a program root.py (a variant of the sqrt.py program we dicussed...

Problem 4. (Root Finding) Write a program root.py (a variant of the sqrt.py program we dicussed in class) that accepts k (int), c (float), and epsilon (float) as command-line arguments, and writes to standard output the kth root of c, up to epsilon decimal places.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code with related comments areas below:

import sys

if len(sys.argv) is not 4:                       #check if all the arguments are present
   print ('Number of arguments not satisfied')   
   exit()                                       #if not present print the statement and exit
  
root_number = int(sys.argv[1])                   #load the arguments into variables
number = float(sys.argv[2])
decimal_number = int(sys.argv[3])
root = number**(1/root_number)                   #caluclation of the root
rounded_root = round(root, decimal_number)       #rounding the root the epsilon value
print ('The '+str(root_number)+'th root of '+str(number)+' is '+str(rounded_root)) # printing the result

Add a comment
Know the answer?
Add Answer to:
Problem 4. (Root Finding) Write a program root.py (a variant of the sqrt.py program we dicussed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 4. PYTHON: (Root Finding) Write a program root.py (a variant of the sqrt.py program we...

    Problem 4. PYTHON: (Root Finding) Write a program root.py (a variant of the sqrt.py program we discussed in class) that accepts k (int), c (float), and epsilon (float) as command-line arguments, and writes to standard output the kth root of c, up to epsilon decimal places. Example: $ python3 root . py 3 2 1e -15 1.2599210498948732 Hints: Set t (our guess) to c Use the condition |1 − c/tk| > to continue the loop At each iteration, replace the...

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • C# QUESTION Requirements Your task is to write a program that will print out all the s of the com...

    C# QUESTION Requirements Your task is to write a program that will print out all the s of the command line arguments to a program For example, given the arguments 'gaz, wx,and 'edc, your program should output wsxaazea Your implementation is expected to use Heap's algorithm according to the following pseudocode: procedure generatelk: integer, A: array of any): if k 1 then output A) else // Generate permutations with kth unaltered //Initially k length(A) // Generate permutations for kth swapped...

  • Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained...

    Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained in a1q4.c and perform the following tasks in your program’s main() function: (8 marks) • Declare and initialize at least one variable of these types: char, int, unsigned int, float, double and long double. When initializing your variables, ensure the constants you use are of the same type as your variable. See the “Constants” section of Topic 7 for more information. • Use printf(3)...

  • Write a program that reads 2 doubles, 2 float, and 4 integers and prints them four...

    Write a program that reads 2 doubles, 2 float, and 4 integers and prints them four in a line separated by tabs or spaces. The user should be prompted for input with a suitable instruction and the output should line up to the right. The numerical output should also have a minimum width of 5 and a precision of 2 decimal places to the right of the decimal. In C programming

  • 1) Write a complete C or C++ program to print Hello World Greetings. 2) Using the...

    1) Write a complete C or C++ program to print Hello World Greetings. 2) Using the command line, compile and generate the executable for the above program. Let’s call helloWorld the target executable. 3) Write a C program that does the following: a) forks a child to execute helloWorld b) waits for the child execution to end 4) Reuse the above program to try a different variant of exec family of system calls. OPTIONAL: 1) write a program main that...

  • c++ Newton method for iteratively finding the root f(x) = 0. The equation is Where f(x)...

    c++ Newton method for iteratively finding the root f(x) = 0. The equation is Where f(x) is the function, f'(x) is the derivative of f9x), Write a C++ program to find root for the function of f(x). The function is on your C++ homework 2 for F(x) = x + 2x -10 You may have two functions, for example, float f(float x) float f=x*x-4; //any function equation return f; float prime(float x) float prime = 2 * x; //derivative of...

  • Write a program using Visual Studio 2017 that accepts four (4) lines of input: • The...

    Write a program using Visual Studio 2017 that accepts four (4) lines of input: • The first line contains a float value in 4.2f format • The second line contains a char value • The third line contains a 4-digit int value • The fourth line contains a char value and then displays all of the input on a single line Phone Number Write a program that accepts a phone number of the form +1(xxx)-xxxxxxx where x is a digit,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT