Write a program to parse and evaluate programs consisting of assignment and prin
ID: 3548042 • Letter: W
Question
Write a program to parse and evaluate programs consisting of assignment and print statements with fully parenthesized arithmetic expressions (see Program 4.3.5). For example, given the input
A = 5
B = 10
C = A + B
D = C * C
print(D)
your program should print the value 225. Assume that all variables and values are of type double. Use a symbol table to keep track of variable names.
Experiments
Write a program to evaluate polynomials of the form:
ax^5 + bx^4 + cx^3 + dx^2 + ex + f
Discuss the limitations of this language. What kinds of programs can you write in Java that you cannot write here?
Explanation / Answer
#include <stdio.h>
#include<math.h>
int main(void)
{
int a[1000]={0};
int i=0,n,x;
printf("Enter the degree of polynomial : ");
scanf("%d",&n);
printf("Enter the co-effecients of the polynomial : ");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
printf("Enter the vale of x : ");
scanf("%d",&x);
//Initializing value of polynomial to 0
int sum = 0;
for(i=n;i>=0;i--)
{
int k = pow(x,i);
k=k*a[i];
sum=sum+k;
}
printf(" Value of polynomial = %d ",sum);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.