Question

In: Computer Science

Modify your second order sequence code to test for oscillation. This can be done by calculating...

Modify your second order sequence code to test for oscillation. This can be done by calculating the difference between subsequent numbers in the sequence and observing whether the difference changes sign (ie: Do they swap from positive to negative or are they always positive or negative?). To complete this task will need to declare a “flag” variable. It can be of type int and have any name of your choosing. Given the initial values of x1 and x2 calculate the difference: x2 − x1. If it is positive, make the flag zero. If it is negative, make the flag one. You may assume that the difference will always be non-zero (ie: the initial values will never be equal). Each time a new entry in the sequence is generated re-calculate the value xn − xn−1. If, at any point, the sign of this calculation is different to the sign of x2 − x1 the program should print that the sequence oscillates and exit. If, after 20 iterations, no change of sign has been observed the program should print that no oscillation occurred and exita . Example 1: For the initial values x1 = 1, x2 = -0.5 and coefficients b1 = 0.2, b2 = 0.2 the sequence is: 1.00000 -0.50000 0.10000 -0.08000 0.00400 -0.01520 and the sequence oscillates (first difference is -1.5, then +0.6).

#include "stdio.h"

int main()
{
double b1=0.2,b2 =0.2,x1=1,x2=-0.5,x,d;
  
int n=2,flag;
d=x2-x1;
  
  
printf("%f\n%f\n",x1,x2);
  
while (n<5)
{
if(d<0){
flag=1;
} else if(d>0)
{
flag=0;
}
x=b1*x2+b2*x1;
printf("%f %f\n",x);
n++;
x1=x2;
x2=x;
  
}

return(0);   
}

I cant do the difference sequence

Solutions

Expert Solution

Whole code:

#include <iostream>
using namespace std;

int main() {
   float sequence[6]= {1.00000,-0.50000,0.10000,-0.08000,0.00400,-0.01520};
   int flag[5];
   float difference[5];
   int i=0;
   for(i=0;i<5;i++)
   {
   difference[i]=sequence[i+1]-sequence[i];
   }
   cout<<"Sequential Difference: ";
   for(i=0;i<5;i++)
   {
   cout<<difference[i]<<" ";
   }
   cout<<endl;
  
  
   for(i=0;i<5;i++)
   {
   if(difference[i]<0)
   {
   flag[i]=1;
   }
   else
   {
   flag[i]=0;
   }
   }
   cout<<"Flags: ";

   for(i=0;i<5;i++)
   {
   cout<<flag[i]<<" ";
   }
   cout<<endl;
  
   for(i=0;i<5;i++)
   {
   if(flag[i]!=flag[i+1])
   {
   cout<<"Sequential"<<endl;
   break;
   }
   }
  
   return 0;
}

For sequential difference I have used this part of code:

You just have to make a float array and then using for loop calculate the difference between the current value and next value and store this in a new float array.

For flags this part of code is used:

If the current value of difference in the difference array is negative then the current value of flag in flag array will be 1 otherwise 0.

For finding if the oscillation is sequential:

If the current value and the next value of flag in the flag array does not match then the oscillation is sequential.

If you run the code as a whole the output will be:

If you found this answer helpful, please give it a thumbs up! :)


Related Solutions

HOW TO CODE AND SEQUENCE THE FOLLOWING: 1.ASSESSMENT: DKA, TYPE 1DM Tip: ASSIGN A SECOND CODE...
HOW TO CODE AND SEQUENCE THE FOLLOWING: 1.ASSESSMENT: DKA, TYPE 1DM Tip: ASSIGN A SECOND CODE FOR UNDERDOSING OF INSULIN FROM THE TABLE OF DRUGS AND CHEMICALS. 2. ASSESSMENT: TEST RESULTS SHOW ABNORMALLY LOW VITAMIN D LEVEL. PATIENT IS OBESE DUE TO EXCESS CALORIES AND HAS A (BMI) OF 30.5 KG/M2 TIP: Z CODE FOR ANNUAL CHECKUP IS NEEDED.
Prompt: Produce a 4th order Runge Kutta code in PYTHON that evaluates the following second order...
Prompt: Produce a 4th order Runge Kutta code in PYTHON that evaluates the following second order ode with the given initial conditions, (d^2y/dt^2) +4(dy/dt)+2y=0, y(0)=1 and y'(0)=3. After your code can evaluate the 2nd order ode, add a final command to plot your results. You may only use python.
Diagnostic Test: The why it is done, the preparation, how it’s done, what the results can...
Diagnostic Test: The why it is done, the preparation, how it’s done, what the results can mean for: contraction stress test, Biophysical profile,and fetal kick count
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Modify the original code and add an additional function of your choice. The function should be...
Modify the original code and add an additional function of your choice. The function should be unique and something you created for this assignment. Support your experimentation with screen captures of executing the new code. Prepare a test table with at least 3 distinct test cases listing input and expected output for your unique function. #include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x) = x*x*x\n"); printf ("c: c(x) = x^2 + 2*x...
How to validate Javascript form data? Here is the code. Can someone modify it so that...
How to validate Javascript form data? Here is the code. Can someone modify it so that all the information is validated? Thanks. <!DOCTYPE html> <html lang="en"> <head>    <title>Music Survey</title>    <meta charset="utf-8"> </head> <style>    legend { font-weight:bold;    }    </style> <body> <h1>Music Survey</h1> <form method="post" action=""> <label for="myname"><b>Name:</b></label>        <input type="text" name="myname" id="myname">        <br><br> <label for="myemail"><b>Email:</b></label>        <input type="email" name="myemail" id="myemail">        <br><br>   <fieldset> <legend>Select Your Favorite Types of Music:</legend> <input type="checkbox"...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set...
MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set by PASSING IN the arguments for the X and Y coordinates. The Z coordinate can be hard-coded (extra credit if it is passed in too). ADD CODE to print the new point location, and note to the user it is the new point using input arguments Add 2 new methods to the Point3d class, each accepting 3 values as input. pAdd(a1, a2, a3) will...
Write a R-script to (and show the outputs of your code) (a) Create a sequence of...
Write a R-script to (and show the outputs of your code) (a) Create a sequence of numbers starting at 3.5 and ending at 10.7 with increments of 0.79. Find the variance and mean of those numbers. And finally sort the vector in a decreasing manner (b) Create a 3 different 3 by 3 matrices such that each of the numbers 1,2,...,9 appear exactly once (Sudoku style) in each of the matrices.
CONFIDENCE INTERVAL PROJECT – Day 3 AFTER YOUR DONE CALCULATING, FOR EACH CONFIDENCE INTERVAL: Describe your...
CONFIDENCE INTERVAL PROJECT – Day 3 AFTER YOUR DONE CALCULATING, FOR EACH CONFIDENCE INTERVAL: Describe your process of constructing the 95% CI Interpret your results of the CI in context Do you believe your CI is accurate? Explain why or why not, using values as your justification. (You will not be graded on whether or not it’s accurate, but instead on how you discuss its accuracy.) Who do you think might be interested in your data? Why? How might they...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT