Sunday, November 5, 2017

Day 5 - Loops

CLASSWORK:
Degrees to Radians Using While Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a while loop structure. */ 
#include <stdio.h>
#define PI 3.141593 

int main(void) {
 /* Declare and initialize variables. */ 
int degrees=0; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
while (degrees <= 360) { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
degrees += 10; 

/* Exit program. */ 
return 0; 
}

Degrees to Radians Using Do/While Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a do-while loop structure. */ 
#include <stdio.h> 
#define PI 3.141593

int main(void) { 
/* Declare and initialize variables. */ 
int degrees=0; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
do { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
degrees += 10; 
} while (degrees <= 360); 
/* Exit program. */ 
return 0; 
} 

Degrees to Radians Using For Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a for loop structure. */ 
#include <stdio.h> 
#define PI 3.141593 

int main(void) { 
/* Declare variables. */ 
int degrees; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
for (degrees=0; degrees<=360; degrees+=10) { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 

/* Exit program. */ 
return 0; 


HOMEWORK:
Wave Generator Code
#include <stdio.h>
#include <math.h>
#define PI 3.141592

int main (void)
{
  double wl_1, wl_2, wl_3, wh_1, wh_2, per_1, per_2, per_c, time;
  double sum, wavemax,steps, w_1, w_2, w_3;
  printf("Input period and wave height:\n" );
  printf("Wave 1: \n");
  scanf("%f" "%f", &per_1, &wh_1);
  printf("Wave 2: \n");
  scanf("%f" "%f", &per_2, &wh_2);
  
  wl_1 = 5.13*per_1*per_1;
  wl_2 = 5.13*per_2*per_2;
  
  per_c = per_1*per_2;
  wl_3 = 5.13*per_c*per_c;
  
  w_1 = .5*wh_1*sin((2*PI/per_1)*time);
  w_2 = .5*wh_2*sin((2*PI/per_2)*time);
  w_3 = .5*(wh_1 + wh_2)*sin((2*PI/per_c)*time);
  sum = w_1 + w_2;
  
  steps = per_c/200;

/*set new period to the product of the wave periods
set time increment to new period/200
set wavemax to 0
set time to 0
set steps to 0
while steps <= 199
set sum to wave 1 + wave 2
if sum > wavemax
set wavemax to sum
add 1 to steps
print wavemax*/
printf("Individual Wavelengths:");
for (time=0, sum=0; time<=2; time += steps)
{
  printf("WL 1: %5.4f""WL 2: %5.4f""WL 3: %5.4f", wl_1, wl_2, wl_3);
  if (sum > wavemax)
  {
    wavemax = sum;
  }
continue;
}
printf("Max Wave Height: %f", wavemax);

  return 0;
}

No comments:

Post a Comment