Homework 5

i.Modify the program so that it finds the maximum crest and the minimum trough for the combined waves.

// Implement the pseudocode
//Yunfei Pan
#include<stdio.h>
#include<math.h>
#include<assert.h>
int main()
#define PI 3.1415926
{
  //initial
  double p1,p2,h1,h2,w1,w2,r=0,t,l1,l2,r1=0;
  //input
  printf("Enter integer wave period (s) and wave height (ft)\nfor wave 1:\n");
  scanf("%lf %lf",&p1,&h1);
  assert(p1>=0&&h1>=0);
  printf("Enter integer wave period (s) and wave height (ft)\nfor wave 2:\n");
  scanf("%lf %lf",&p2,&h2);
  assert(p2>=0&&h2>=0);
  t=p1*p2/200;
  //wave length
  l1=5.13*p1*p1;
  l2=5.13*p2*p2;
 
  //max hight
  for (int i=1;i<=200;i++)
  {
    w1=h1/2*sin(2*PI*t*i/p1);
    w2=h2/2*sin(2*PI*t*i/p2);
    if((w1+w2)>r)
      r=w1+w2;
  }
    for (int i=1;i<=200;i++)
  {
    w1=h1/2*sin(2*PI*t*i/p1);
    w2=h2/2*sin(2*PI*t*i/p2);
    if((w1+w2)<r1)
      r1=w1+w2;
  }
  //output
  printf("Wavelengths (in ft) are: %6.2f %6.2f\n",l1,l2);
  printf("Maximum combined wave height is %6.2f\n ft",r*=2);
  printf("maximum crest and the minimum trough is %6.2f\n ft %6.2f\n ft",r,r1);

return 0;
}


iii.Modify the program so that it also allows the user to enter a time, then the program computes and prints the combined wave height at that time.


// Implement the pseudocode
//Yunfei Pan
#include<stdio.h>
#include<math.h>
#include<assert.h>
int main()
#define PI 3.1415926
{
  //initial
  double p1,p2,h1,h2,w1,w2,r=0,t,l1,l2,r1=0;
  //input
  printf("Enter integer wave period (s) and wave height (ft)\nfor wave 1:\n");
  scanf("%lf %lf",&p1,&h1);
  assert(p1>=0&&h1>=0);
  printf("Enter integer wave period (s) and wave height (ft)\nfor wave 2:\n");
  scanf("%lf %lf",&p2,&h2);
  assert(p2>=0&&h2>=0);
  printf("Enter the time you want to calculate:\n");
  scanf("%lf",&t);
  t=p1*p2/200;
  //wave length
  l1=5.13*p1*p1;
  l2=5.13*p2*p2;
 

   w1=h1/2*sin(2*PI*t/p1);
   w2=h2/2*sin(2*PI*t/p2);
   
    r=w1+w2;

  //output
  printf("Wavelengths (in ft) are: %6.2f %6.2f\n",l1,l2);
  printf("combined wave height is %6.2f\n ft",r*=2);

return 0;
}



iv. Modify the program so that it allows the user to specify the number of points to compute for determining the maximum combined wave height over the specified period of time. Should this make any difference in the answer? Explain

The answer can be different since more points you have, more precise the answer will be.

// Implement the pseudocode input points
//Yunfei Pan
#include<stdio.h>
#include<math.h>
#include<assert.h>
int main()
#define PI 3.1415926
{
  //initial
  double p1,p2,h1,h2,w1,w2,r=0,t,l1,l2,pts;
  //input
  printf("Enter integer wave period (s) and wave height (ft)\nfor wave 1:\n");
  scanf("%lf %lf",&p1,&h1);
  assert(p1>=0&&h1>=0);
  printf("Enter integer wave period (s) and wave height (ft)\nfor wave 2:\n");
  scanf("%lf %lf",&p2,&h2);
  assert(p2>=0&&h2>=0);
  printf("Enter how many points to compute(whole number at least 1)");
  scanf("%lf",&pts);
  assert(pts==(int)pts&&pts>=1);
  t=p1*p2/pts;
  //wave length
  l1=5.13*p1*p1;
  l2=5.13*p2*p2;
 
  //max hight
  for (int i=1;i<=pts;i++)
  {
    w1=h1/2*sin(2*PI*t*i/p1);
    w2=h2/2*sin(2*PI*t*i/p2);
    if((w1+w2)>r)
      r=w1+w2;
  }
  //output
  printf("Wavelengths (in ft) are: %6.2f %6.2f\n",l1,l2);
  printf("Maximum combined wave height is %6.2f\n ft",r*=2);
}



v. Modify the program so that it allows the user to specify a phase shift between the two waves. This phase shift should be used as the phase shift for the second wave; the phase shift for the first wave should still be zero. Experiment with different phase shifts to see if they change the maximum possible wave height.

The maximun will be different since the phase shifts change the way that waves conbined.

// Implement the pseudocode input phase shift
//Yunfei Pan
#include<stdio.h>
#include<math.h>
#include<assert.h>
int main()
#define PI 3.1415926
{
  //initial
  double p1,p2,h1,h2,w1,w2,r=0,t,l1,l2,pts,phase1,phase2;
  //input
  printf("Enter  wave period (s) and wave height (ft)\nfor wave 1:\n");
  scanf("%lf %lf",&p1,&h1);
  assert(p1>=0&&h1>=0);
  printf("Enter  wave period (s) and wave height (ft)\nfor wave 2:\n");
  scanf("%lf %lf",&p2,&h2);
  assert(p2>=0&&h2>=0);
    printf("Enter phase shift of wave 1 and wave 2 (in radian)\nfor wave 2:\n");
  scanf("%lf %lf",&phase1,&phase2);
  assert(phase1>=0&&phase2>=0);
  pts=200;
  // printf("Enter how many points to compute(whole number at least 1)");
  // scanf("%lf",&pts);
  // assert(pts==(int)pts&&pts>=1);
  t=p1*p2/pts;
  //wave length
  l1=5.13*p1*p1;
  l2=5.13*p2*p2;
  
  //max hight
  for (int i=1;i<=pts;i++)
  {
    w1=h1/2*sin(2*PI*t*i/p1+phase1);
    w2=h2/2*sin(2*PI*t*i/p2+phase2);
    if((w1+w2)>r) 
      r=w1+w2;
  }
  //output
  printf("Wavelengths (in ft) are: %6.2f %6.2f\n",l1,l2);
  printf("Maximum combined wave height is %6.2f\n ft",r*=2);
}




vi. Modify the program so that it uses units of meters and seconds, instead of feet and seconds. Remember to modify the program that computes the wavelengths appropriately.

// Implement the pseudocode output meter sec
//Yunfei Pan
#include<stdio.h>
#include<math.h>
#include<assert.h>
int main()
#define PI 3.1415926
{
  //initial
  double p1,p2,h1,h2,w1,w2,r=0,t,l1,l2,pts,phase1,phase2;
  //input
  printf("Enter  wave period (s) and wave height (ft)\nfor wave 1:\n");
  scanf("%lf %lf",&p1,&h1);
  assert(p1>=0&&h1>=0);
  printf("Enter  wave period (s) and wave height (ft)\nfor wave 2:\n");
  scanf("%lf %lf",&p2,&h2);
  assert(p2>=0&&h2>=0);
  // printf("Enter phase shift of wave 1 and wave 2 (in radian)\nfor wave 2:\n");
  // scanf("%lf %lf",&phase1,&phase2);
  // assert(phase1>=0&&phase2>=0);
  phase1=phase2=0;
  pts=200;
  // printf("Enter how many points to compute(whole number at least 1)");
  // scanf("%lf",&pts);
  // assert(pts==(int)pts&&pts>=1);

  
  t=p1*p2/pts;
  //wave length
  l1=5.13*p1*p1;
  l2=5.13*p2*p2;
  
  //max hight
  for (int i=1;i<=pts;i++)
  {
    w1=h1/2*sin(2*PI*t*i/p1+phase1);
    w2=h2/2*sin(2*PI*t*i/p2+phase2);
    if((w1+w2)>r) 
      r=w1+w2;
  }
  //convert ft to m
  l1*=.3048;
  l2*=.3048;
  r*=.3048;
  //output
  printf("Wavelengths (in m) are: %6.2f %6.2f\n",l1,l2);
  printf("Maximum combined wave height is %6.2f m\n",r*=2);
}

Comments

Popular posts from this blog

Homework 6

Another Day 23

Homework4