Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I Write a program that takes in the following two 10-element arrays. The fi

ID: 3694622 • Letter: P

Question

Part I

Write a program that takes in the following two 10-element arrays. The first array is x an independent variable, the second array is y which is a function of x (y = f(x)).

x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

y = {0, 4, 8, 12, 16, 20, 24, 28, 32, 36}

With h = 1

Use the composite trapezoidal rule to compute the integral of f(x) from x = 0 to x = 9;

Use the composite Simpson Rule and compute the integral of f(x) from x = 0 to x = 9.

Display the results on the screen.

Part II:

For the previous program (the integrals one):

Create a Structure type called DataForFinal

This structure will have the following fields:

1) 10 element double array

2) 10 element double array

3) double variable

4) double variable

Create a structure and name it with your name

In the fields of this structure save the arrays x, and y and the variables containing the integrals using the trapezoidal and simpson rule methods.

Print the integral results on the screen using the fields in the array.

Explanation / Answer

Solution:Please follow these programs using simpson methods and trapeziodal methods

Implement Simpson method:

#include<stdio.h>

#include<conio.h>

#include<math.h>

char postfix[10];

float stack[10];

char stack1[10];

int top=-1,top1=-1;

float eval(char postfix[], float x1);

void infix_postfix(char infix[]);

main()

{

float x0, xn, h, s,e1,e2, e3;

char exp[80];

int i,l=0;

int arr[n] = {0,1,2,3,4,5,6,7,8,9}

clrscr();

printf(" Enter an expression: ");

gets(exp);

puts("Enter x0, xn and number of sub-intervals: ");

scanf("%f%f%d", &x0, &xn, &n);

h=(xn-x0)/n;

if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')

{

   l=strlen(exp);

   for(i=0;i<l-3; i++)

   arr[0]=exp[i+3];

   arr[i]='';

   infix_postfix(arr);

   e1=eval(postfix,x0);

   e2=eval(postfix,xn);

   e3=4*eval(postfix, x0+h);

   s=log(e1)+log(e2)+log(e3);

   for (i=3;i<=n-1;i+=2)

      s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);

}

else

{

   infix_postfix(exp);

   s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);

   for (i=3;i<=n-1;i+=2)

   s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);

}

printf("The value of integral is %6.3f ",(h/3)*s);

return(0);

}

/*Inserting the operands in a stack. */

void push(float item)

{

if(top==99)

{

   printf(" The stack is full");

   getch();

   exit(0);

}

else

{

top++;

stack[top]=item;

}

return;

}

/*Removing the operands from a stack. */

float pop()

{

float item;

if(top==-1)

{

printf(" The stack is empty ");

getch();

}

item=stack[top];

top--;

return (item);

}

void push1(char item)

{

if(top1==9)

{

printf(" The stack is full");

  getch();

exit(0);

}

else

{

top1++;

stack1[top1]=item;

}

return;

}

/*Removing the operands from a stack. */

char pop1()

{

char item;

if(top1==-1)

{

printf(" The stack1 is empty ");

getch();

}

item=stack1[top1];

top1--;

return (item);

}

/*Converting an infix expression to a postfix expression. */

void infix_postfix(char infix[])

{

int i=0,j=0,k;

char ch;

char token;

for(i=0;i<9;i++)

postfix[i]=' ';

push1('?');

i=0;

token=infix[i];

while(token!='')

{

   if(isalnum(token))

    {

      postfix[j]=token;

      j++;

    }

   else if(token=='(')

    {

     push1('(');

    }

   else if(token==')')

   {

    while(stack1[top1]!='(')

    {

     ch=pop1();

     postfix[j]=ch;

     j++;

    }

   ch=pop1();

}

else

{

while(ISPriority(stack1[top1])>=ICP(token))

{

   ch=pop1();

   postfix[j]=ch;

   j++;

}

push1(token);

}

i++;

token=infix[i];

}

while(top1!=0)

{

ch=pop1();

postfix[j]=ch;

j++;

}

postfix[j]='';

}

/*Determining the priority of elements that are placed inside the stack. */

int ISPriority(char token)

{

switch(token)

{

case '(':return (0);

case ')':return (1);

case '+':return (2);

case '-':return (3);

case '*':return (4);

case '/':return (5);

case '?':return (6);

default: printf("Invalid expression");

}

return 0;

}

/*Determining the priority of elements that are approaching towards the stack. */

int ICP(char token)

{

switch(token)

{

case '(':return (10);

case ')':return (9);

case '+':return (7);

case '-':return (7);

case '*':return (8);

case '/':return (8);

case '':return (0);

default: printf("Invalid expression");

}

return 0;

}

/*Calculating the result of expression, which is converted in postfix notation. */

float eval(char p[], float x1)

{

float t1,t2,k,r;

int i=0,l;

l=strlen(p);

while(i<l)

{

if(p[i]=='x')

push(x1);

else

   if(isdigit(p[i]))

    {

     k=p[i]-'0';

     push(k);

    }

else

{

t1=pop();

t2=pop();

switch(p[i])

{

   case '+':k=t2+t1;

   break;

   case '-':k=t2-t1;

   break;

   case '*':k=t2*t1;

   break;

   case '/':k=t2/t1;

   break;

   default: printf(" Invalid expression");

}

push(k);

}

i++;

}

if(top>0)

{

printf("You have entered the operands more than the operators");

exit(0);

}

else

{

r=pop();

return (r);

}

return 0;

}

2)Implement Trapezoidal method:

#include<stdio.h>

#include<conio.h>

#include<math.h>

char postfix[10];

float stack[10];

char stack1[10];

int top=-1,top1=-1;

float eval(char postfix[], float x1);

void infix_postfix(char infix[]);

main()

{

float x0, xn, h, s,e1,e2;

char exp[80];

int i,l=0;

int arr[n] = {0,1,2,3,4,5,6,7,8,9}

clrscr();

printf(" Enter an expression: ");

gets(exp);

puts("Enter x0, xn and number of subintervals");

scanf("%f%f%d", &x0, &xn, &n);

h=(xn-x0)/n;

if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')

{

l=strlen(exp);

for(i=0;i<l-3; i++)

   arr[0]=exp[i+3];

arr[i]='';

infix_postfix(arr);

e1=eval(postfix,x0);

e2=eval(postfix,xn);

s=log(e1)+log(e2);

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

s+=2*log(eval(postfix,x0+i*h));

}

else

{

infix_postfix(exp);

s=eval(postfix,x0)+eval(postfix,xn);

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

s+=2*eval(postfix,x0+i*h);

}

printf("Value of the integral is %6.3f ",(h/2)*s);

return(0);

}

/*Inserting the operands in a stack. */

void push(float item)

{

if(top==99)

{

printf(" The stack is full");

getch();

exit(0);

}

else

{

top++;

stack[top]=item;

}

return;

}

/*Removing the operands from a stack. */

float pop()

{

float item;

if(top==-1)

{

printf(" The stack is empty ");

getch();

}

item=stack[top];

top--;

return (item);

}

void push1(char item)

{

if(top1==9)

{

printf(" The stack is full");

getch();

exit(0);

}

else

{

top1++;

stack1[top1]=item;

}

return;

}

/*Removing the operands from a stack. */

char pop1()

{

char item;

if(top1==-1)

{

printf(" The stack1 is empty ");

getch();

}

item=stack1[top1];

top1--;

return (item);

}

/*Converting an infix expression to a postfix expression. */

void infix_postfix(char infix[])

{

int i=0,j=0,k;

char ch;

char token;

for(i=0;i<79;i++)

postfix[i]=' ';

push1('?');

i=0;

token=infix[i];

while(token!='')

{

if(isalnum(token))

{

postfix[j]=token;

j++;

}

else if(token=='(')

{

push1('(');

}

else if(token==')')

{

while(stack1[top1]!='(')

{

ch=pop1();

postfix[j]=ch;

j++;

}

ch=pop1();

}

else

{

while(ISPriority(stack1[top1])>=ICP(token))

{

ch=pop1();

/*Assigning the popped element into the postfix array. */

postfix[j]=ch;

j++;

}

push1(token);

}

i++;

token=infix[i];

}

while(top1!=0)

{

ch=pop1();

postfix[j]=ch;

j++;

}

postfix[j]='';

}

int ISPriority(char token)

{

switch(token)

{

case '(':return (0);

case ')':return (9);

case '+':return (7);

case '-':return (7);

case '*':return (8);

case '/':return (8);

case '?':return (0);

default: printf("Invalid expression");

break;

}

return 0;

}

/*Determining the priority of elements that are approaching towards the stack. */

int ICP(char token)

{

switch(token)

{

   case '(':return (10);

   case ')':return (9);

   case '+':return (7);

   case '-':return (7);

   case '*':return (8);

   case '/':return (8);

   case '':return (0);

   default: printf("Invalid expression");

   break;

}

return 0;

}

/*Calculating the result of expression, which is converted in postfix notation. */

float eval(char p[], float x1)

{

float t1,t2,k,r;

int i=0,l;

l=strlen(p);

while(i<l)

{

if(p[i]=='x')

push(x1);

else

if(isdigit(p[i]))

{

k=p[i]-'0';

push(k);

}

else

{

t1=pop();

t2=pop();

switch(p[i])

{

case '+':k=t2+t1;

break;

case '-':k=t2-t1;

break;

case '*':k=t2*t1;

break;

case '/':k=t2/t1;

break;

default: printf(" Invalid expression");

break;

}

push(k);

}

i++;

}

if(top>0)

{

printf("You have entered the operands more than the operators");

exit(0);

}

else

{

r=pop();

return (r);

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote