Question

In: Advanced Math

Parse string java code Write a recursive program that can calculate the value of a given...

Parse string java code
Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x
Using index of for -/+

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
 
void main()
{
    int array[MAXSIZE];
    int i, num, power;
    float x, polySum;
 
    printf("Enter the order of the polynomial \n");
    scanf("%d", &num);
    printf("Enter the value of x \n");
    scanf("%f", &x);
    /*  Read the coefficients into an array */
    printf("Enter %d coefficients \n", num + 1);
    for (i = 0; i <= num; i++)
    {
        scanf("%d", &array[i]);
    }
    polySum = array[0];
    for (i = 1; i <= num; i++)
    {
        polySum = polySum * x + array[i];
    }
    power = num;
 
    printf("Given polynomial is: \n");
    for (i = 0; i <= num; i++)
    {
        if (power < 0)
        {
            break;
        }
        /*  printing proper polynomial function */
        if (array[i] > 0)
            printf(" + ");
        else if (array[i] < 0)
            printf(" - ");
        else
            printf(" ");
        printf("%dx^%d  ", abs(array[i]), power--);
    }
    printf("\n Sum of the polynomial = %6.2f \n", polySum);
}

Runtime Test Cases

$ cc pgm.c

$ a.out
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6
Given polynomial is:
 + 3x^2   + 2x^1   + 6x^0
Sum of the polynomial =  22.00
 
$ a.out
Enter the order of the polynomial
4
Enter the value of x
1
Enter 5 coefficients
3
-5
6
8
-9
Given polynomial is:
 + 3x^4   - 5x^3   + 6x^2   + 8x^1   - 9x^0
Sum of the polynomial =   3.00

Related Solutions

Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write an assembly program which will read the value of the switches on the board, parse...
Write an assembly program which will read the value of the switches on the board, parse this number by looking at the 4 least-significant bits, using the lookup table from the prelab to get the value necessary to display this hex number on a 7-segment display, using shift instructions to adjust registers as necessary to parse the next set of values. Repeat until all four displays are accounted for. Finally, when one register has a bit-stream necessary to display all...
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT