Question

In: Computer Science

How would I write a program for C++ that checks if an entered string is an...

How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.

Solutions

Expert Solution

Aa polynomial of the form cnxn + cn-1xn-1 + cn-2xn-2 + … + c1x + c0

and a value of x, Here cn, cn-1, .. are integers (may be negative)

and n is a positive integer.

Input is in the form of an array = poly[]

where, poly[0] represents coefficient for xn and poly[1] represents coefficient for xn-1 and so on.

Example:-

#include <iostream>

using namespace std;

int horner(int poly[], int n, int x) {

int result = poly[0];

    for (int i=1; i<n; i++)

        result = result*x + poly[i];

return result;

}

int main()

{

    // Let us evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3

    int poly[] = {2, -6, 2, -1};

    int x = 3;

    int n = sizeof(poly)/sizeof(poly[0]);

    cout << "Value of polynomial is " << horner(poly, n, x);

    return 0;

}

Result:-

Value of polynomial is 5

Time complexity is Big-Oh


Related Solutions

How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
How would I write this driver program in C++?? We just started learning C++ and I...
How would I write this driver program in C++?? We just started learning C++ and I still don't understand how to use classes or randomly generate numbers, and I'm struggling to complete this assignment. The premise of the whole assignment is to randomly generate dates within a range and format the dates in 3 different ways. Calendar.cpp Create a driver program with a main function. Use symbolic constants or "const" declarations here, also. In particular, define local constant SIZE to...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
Write a PHP program that checks the elements of a string array named $Passwords. Use regular...
Write a PHP program that checks the elements of a string array named $Passwords. Use regular expressions to test whether each element is a strong password. For this exercise, a strong password must have at least one number, one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. (Hint: Use the [^0-9A-Za-z] character class.) The string should also be between 8 and 16 characters long. The $Passwords array should contain...
How do I write a COBOL program that translates a word entered into pig Latin?
How do I write a COBOL program that translates a word entered into pig Latin?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT