/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#include <math.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
double elevation[N][N], Pcount = 0, Vcount = 0, valley, peak = 0, temp_peak, Peak1Dis;
FILE *grid;
/* Read information from a data file. */
grid = fopen("grid1.txt","r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
printf("Bottom left point(BLF) defined as row 5, column 0 \n\n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) && (elevation[i+1][j]<elevation[i][j]) && (elevation[i][j-1]<elevation[i][j]) && (elevation[i][j+1]<elevation[i][j]))
{
++Pcount;
printf("Peak at row: %d column: %d \n",i,j);
Peak1Dis = 100* sqrt(((5-i)*(5-i))+(j*j));
printf("Distance from BLP: %.2lf ft\n\n", Peak1Dis);
if (elevation[i][j] > peak)
{
peak = elevation[i][j];
}
}
printf("\nNumber of peaks: %lf\n\n", Pcount);
/* Determine and print valley locations. */
valley = elevation[i][j]; //initializes a variable to check lowest elevation against
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]>elevation[i][j]) && (elevation[i+1][j]>elevation[i][j]) && (elevation[i][j-1]>elevation[i][j]) && (elevation[i][j+1]>elevation[i][j])
&& (elevation[i-1][j-1]>elevation[i][j]) && (elevation[i-1][j+1]>elevation[i][j]) && (elevation[i+1][j-1]>elevation[i][j]) && (elevation[i+1][j+1]>elevation[i][j]))
{
++Vcount;
printf("Valley at row: %d column: %d \n",i,j);
if (elevation[i][j] < valley)
{
valley = elevation[i][j];
}
}
printf("\nNumber of valleys: %lf\n", Vcount);
printf("\nHighest elevation: %lf\n", peak);
printf("Lowest elevation: %lf\n", valley);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
Engineering 6 BNepomuceno
Thursday, December 13, 2018
sesimic
/* This program reads a seismic data file and then */
/* determines the times of possible seismic events. */
#include <stdio.h>
#define FILENAME "seismic1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare variables and function prototypes. */
int k, npts, short_window, long_window, counter = 0;
double sensor[MAX_SIZE], time_incr, short_power, threshold = 0,
long_power, ratio;
FILE *file_ptr;
double power_w(double *ptr,int n);
/* Read sensor data file. */
file_ptr = fopen(FILENAME,"r");
if (file_ptr == NULL)
printf("Error opening input file. \n");
else
{
fscanf(file_ptr,"%d %lf",&npts,&time_incr);
if (npts > MAX_SIZE)
printf("Data file too large for array. \n");
else
{
/* Read data into an array. */
for (k=0; k<=npts-1; k++)
fscanf(file_ptr,"%lf",&sensor[k]);
/* Read window sizes from the keyboard. */
printf("Enter number of points for short window: \n");
scanf("%d",&short_window);
printf("Enter number of points for long window: \n");
scanf("%d",&long_window);
while(threshold <= 1)
{
printf("Enter threshold value: \n");
scanf("%lf",&threshold);
}
/* Compute power ratios and search for events. */
for (k=long_window-1; k<=npts-1; k++)
{
short_power = power_w(&sensor[k],short_window);
long_power = power_w(&sensor[k],long_window);
ratio = short_power/long_power;
if (ratio > threshold)
{
printf("Possible event at %f seconds \n", time_incr*k);
counter++;
}
}
printf("Number of events: %d \n", counter);
/* Close file. */
fclose(file_ptr);
}
}
return 0;
}
/*????????????????????????????????????????????????????????????-*/
/* This function computes the average power in a specified */
/* window of a double array. */
double power_w(double *ptr, int n)
{
/* Declare and initialize variables. */
int k;
double xsquare=0;
/* Compute sum of values squared in the array x. */
for (k=0; k<=n-1; k++)
xsquare += *(ptr-k)*(*(ptr-k));
/* Return the average squared value. */
return xsquare/n;
}
/* determines the times of possible seismic events. */
#include <stdio.h>
#define FILENAME "seismic1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare variables and function prototypes. */
int k, npts, short_window, long_window, counter = 0;
double sensor[MAX_SIZE], time_incr, short_power, threshold = 0,
long_power, ratio;
FILE *file_ptr;
double power_w(double *ptr,int n);
/* Read sensor data file. */
file_ptr = fopen(FILENAME,"r");
if (file_ptr == NULL)
printf("Error opening input file. \n");
else
{
fscanf(file_ptr,"%d %lf",&npts,&time_incr);
if (npts > MAX_SIZE)
printf("Data file too large for array. \n");
else
{
/* Read data into an array. */
for (k=0; k<=npts-1; k++)
fscanf(file_ptr,"%lf",&sensor[k]);
/* Read window sizes from the keyboard. */
printf("Enter number of points for short window: \n");
scanf("%d",&short_window);
printf("Enter number of points for long window: \n");
scanf("%d",&long_window);
while(threshold <= 1)
{
printf("Enter threshold value: \n");
scanf("%lf",&threshold);
}
/* Compute power ratios and search for events. */
for (k=long_window-1; k<=npts-1; k++)
{
short_power = power_w(&sensor[k],short_window);
long_power = power_w(&sensor[k],long_window);
ratio = short_power/long_power;
if (ratio > threshold)
{
printf("Possible event at %f seconds \n", time_incr*k);
counter++;
}
}
printf("Number of events: %d \n", counter);
/* Close file. */
fclose(file_ptr);
}
}
return 0;
}
/*????????????????????????????????????????????????????????????-*/
/* This function computes the average power in a specified */
/* window of a double array. */
double power_w(double *ptr, int n)
{
/* Declare and initialize variables. */
int k;
double xsquare=0;
/* Compute sum of values squared in the array x. */
for (k=0; k<=n-1; k++)
xsquare += *(ptr-k)*(*(ptr-k));
/* Return the average squared value. */
return xsquare/n;
}
Weather Ballon
#include <stdio.h>
#include <math.h>
#define FILENAME "balloon1.txt"
int main(void)
{
/* Declare variables. */
double start_time_hour, start_time_minute, end_time_hour, end_time_minute, time_passed_hour, time_passed_minute;
double incre_min, total_time, time_incre, alt, vel, max_alt, max_time_of_alt;
double Ptime, Palt, Pvel, datapoints;
FILE *balloon;
/* Open output file. */
balloon = fopen(FILENAME,"w");
/* Get user input from the keyboard. */
do
{
printf("Enter start time of weather balloon release in hrs then min. \n");
printf("start time: \n");
scanf("%lf %lf", &start_time_hour, &start_time_minute);
printf("\nEnter final time of weather balloon traveling in hrs then min. \n");
printf("final time: \n");
scanf("%lf %lf",&end_time_hour, &end_time_minute);
/* Determine length of time passed based on user inputted times */
time_passed_hour = end_time_hour - start_time_hour;
time_passed_minute = end_time_minute - start_time_minute;
total_time = time_passed_hour + (time_passed_minute / 60);
/* Checks data entered to see if valid */
if (end_time_hour < start_time_hour)
{
printf("\nStart time is greater than final time. Please reenter time values again.\n\n");
}
if (end_time_hour > 48)
{
printf("\nFinal time value cannot be greater than 48 hours. Please reenter time values again.\n\n");
}
} while (end_time_hour < start_time_hour || (end_time_hour > 48));
printf("\nEnter time increment in minutes.\n");
printf("time increment: \n");
scanf("%lf",&incre_min);
double unceil_step = (total_time * 60) / incre_min;
double steps = ceil((total_time * 60) / incre_min);
/* Determine altitudes and velocites of balloon */
//fprintf(balloon,"(time(hrs)) (alitude(m)) (velocity(m/s))\n");
fprintf(balloon,"%lf\n", steps + 1 );
for (double k = 0; k <= steps; k++)
{
double T = (k*(total_time/unceil_step)) + start_time_hour;
alt = -0.12*pow(T,4) + 12*pow(T,3) - 380*pow(T,2) + 4100*T + 220;
vel = -0.48*pow(T,3) + 36*pow(T,2) - 760*T + 4100;
if(alt > max_alt)
{
max_alt = alt;
max_time_of_alt = T;
};
fprintf(balloon,"%.6lf %.6lf %.6lf\n", T, alt, vel/3600);
}
fprintf(balloon, "\nmax alitude: %.3lf m\n", max_alt);
fprintf(balloon, "\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
fclose(balloon);
/* Close file and exit . */
balloon = fopen("balloon1.txt","r");
fscanf(balloon," %lf", &datapoints);
printf("\nTIME ALT VEL\n");
for (double w = 1; w <= datapoints; w++ )
{
fscanf(balloon,"%lf %lf %lf",&Ptime,&Palt,&Pvel);
printf("%1.6lf %.6lf %.6lf\n",Ptime,Palt,Pvel);
}
printf("\nmax alitude: %.3lf m\n", max_alt);
printf("\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
return 0;
}
#include <math.h>
#define FILENAME "balloon1.txt"
int main(void)
{
/* Declare variables. */
double start_time_hour, start_time_minute, end_time_hour, end_time_minute, time_passed_hour, time_passed_minute;
double incre_min, total_time, time_incre, alt, vel, max_alt, max_time_of_alt;
double Ptime, Palt, Pvel, datapoints;
FILE *balloon;
/* Open output file. */
balloon = fopen(FILENAME,"w");
/* Get user input from the keyboard. */
do
{
printf("Enter start time of weather balloon release in hrs then min. \n");
printf("start time: \n");
scanf("%lf %lf", &start_time_hour, &start_time_minute);
printf("\nEnter final time of weather balloon traveling in hrs then min. \n");
printf("final time: \n");
scanf("%lf %lf",&end_time_hour, &end_time_minute);
/* Determine length of time passed based on user inputted times */
time_passed_hour = end_time_hour - start_time_hour;
time_passed_minute = end_time_minute - start_time_minute;
total_time = time_passed_hour + (time_passed_minute / 60);
/* Checks data entered to see if valid */
if (end_time_hour < start_time_hour)
{
printf("\nStart time is greater than final time. Please reenter time values again.\n\n");
}
if (end_time_hour > 48)
{
printf("\nFinal time value cannot be greater than 48 hours. Please reenter time values again.\n\n");
}
} while (end_time_hour < start_time_hour || (end_time_hour > 48));
printf("\nEnter time increment in minutes.\n");
printf("time increment: \n");
scanf("%lf",&incre_min);
double unceil_step = (total_time * 60) / incre_min;
double steps = ceil((total_time * 60) / incre_min);
/* Determine altitudes and velocites of balloon */
//fprintf(balloon,"(time(hrs)) (alitude(m)) (velocity(m/s))\n");
fprintf(balloon,"%lf\n", steps + 1 );
for (double k = 0; k <= steps; k++)
{
double T = (k*(total_time/unceil_step)) + start_time_hour;
alt = -0.12*pow(T,4) + 12*pow(T,3) - 380*pow(T,2) + 4100*T + 220;
vel = -0.48*pow(T,3) + 36*pow(T,2) - 760*T + 4100;
if(alt > max_alt)
{
max_alt = alt;
max_time_of_alt = T;
};
fprintf(balloon,"%.6lf %.6lf %.6lf\n", T, alt, vel/3600);
}
fprintf(balloon, "\nmax alitude: %.3lf m\n", max_alt);
fprintf(balloon, "\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
fclose(balloon);
/* Close file and exit . */
balloon = fopen("balloon1.txt","r");
fscanf(balloon," %lf", &datapoints);
printf("\nTIME ALT VEL\n");
for (double w = 1; w <= datapoints; w++ )
{
fscanf(balloon,"%lf %lf %lf",&Ptime,&Palt,&Pvel);
printf("%1.6lf %.6lf %.6lf\n",Ptime,Palt,Pvel);
}
printf("\nmax alitude: %.3lf m\n", max_alt);
printf("\ntime of max alitude: %.3lf hrs\n", max_time_of_alt);
return 0;
}
DNA strings
/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define long_leng 26
#define short_leng 3
int main(void)
{
/* Declare and initialize variables. */
int count=0;
printf("Enter sequence for long string DNA: ");
char long_str[long_leng];
scanf("%s", long_str);
printf("Enter sequence for short string DNA: ");
char short_str[short_leng];
scanf("%s", short_str);
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
printf("\nLong String of DNA: ");
printf(long_str);
printf("\nShort String of DNA: ");
printf(short_str);
/* Exit program. */
return 0;
}
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define long_leng 26
#define short_leng 3
int main(void)
{
/* Declare and initialize variables. */
int count=0;
printf("Enter sequence for long string DNA: ");
char long_str[long_leng];
scanf("%s", long_str);
printf("Enter sequence for short string DNA: ");
char short_str[short_leng];
scanf("%s", short_str);
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
printf("\nLong String of DNA: ");
printf(long_str);
printf("\nShort String of DNA: ");
printf(short_str);
/* Exit program. */
return 0;
}
structures_waves2
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr;
char location[20];
double fatalities, max_height;
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
{
FILE *waves2;
/* Read and print information from the file. */
waves2 = fopen(FILENAME,"r");
if (waves2 == NULL)
printf("Error opening data file. /n");
else
{
while (fscanf(waves2,"%d %d %d %s %lf %lf ",&t[k].mo,&t[k].da,
&t[k].yr, t[k].location, &t[k].fatalities,&t[k].max_height,) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
//ave = sum/npts;
printf("Summary Information for Tsunamis /n");
printf("Maximum Wave Height (in feet): %.2f /n",max*3.28);
printf("Average Wave Height (in feet): %.2f /n",ave*3.28);
printf("Tsunamis with greater than average heights: /n");
for (k=0; k <=npts-1; k++)
printf("%s /n",t[k].location);
fclose(waves2);
}
return 0;
}
}
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr;
char location[20];
double fatalities, max_height;
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
{
FILE *waves2;
/* Read and print information from the file. */
waves2 = fopen(FILENAME,"r");
if (waves2 == NULL)
printf("Error opening data file. /n");
else
{
while (fscanf(waves2,"%d %d %d %s %lf %lf ",&t[k].mo,&t[k].da,
&t[k].yr, t[k].location, &t[k].fatalities,&t[k].max_height,) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
//ave = sum/npts;
printf("Summary Information for Tsunamis /n");
printf("Maximum Wave Height (in feet): %.2f /n",max*3.28);
printf("Average Wave Height (in feet): %.2f /n",ave*3.28);
printf("Tsunamis with greater than average heights: /n");
for (k=0; k <=npts-1; k++)
printf("%s /n",t[k].location);
fclose(waves2);
}
return 0;
}
}
handprint
#include <iostream>
#include <cmath>
using namespace std;
#define FILENAME "handprint.txt"
int main(void)
{
const int N=20;
int k=0, npts=N;
double y[N];
//Declare and initialize variables.
//double unknown[5]={5.4,7.2,7.9,7.4,5.1},
double known[5]={6.2,7.0,8.0,7.4,5.8},
//test[5]={};
double distance(double hand_1[5],double hand_2[5]);
ifstream handprint;
// Open file, read data into an array.
handprint.open(FILENAME);
if (handprint.fail())
cout << "Error opening input file." << endl;
else
{
while (!handprint.eof())
{
handprint >> y[k];
k++;
}
npts = k;
// Find and print the maximum value.
// Compute and print distance.
cout << "Enter distances for each digit starting with the thumb:" << endl;
/*for (int i = 0; i < 5; i++)
{cin >> test[i];}*/
cout << "Distance: " << distance(y,known) << endl;
// Exit program.
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double hand_1[5],double hand_2[5])
{
// Declare variables.
int k;
double sum=0;
// Compute sum of absolute value differences.
for (k=0; k<=4; k++)
sum = sum + fabs(hand_1[k]-hand_2[k]);
// Return distance value.
return sum;
}
#include <cmath>
using namespace std;
#define FILENAME "handprint.txt"
int main(void)
{
const int N=20;
int k=0, npts=N;
double y[N];
//Declare and initialize variables.
//double unknown[5]={5.4,7.2,7.9,7.4,5.1},
double known[5]={6.2,7.0,8.0,7.4,5.8},
//test[5]={};
double distance(double hand_1[5],double hand_2[5]);
ifstream handprint;
// Open file, read data into an array.
handprint.open(FILENAME);
if (handprint.fail())
cout << "Error opening input file." << endl;
else
{
while (!handprint.eof())
{
handprint >> y[k];
k++;
}
npts = k;
// Find and print the maximum value.
// Compute and print distance.
cout << "Enter distances for each digit starting with the thumb:" << endl;
/*for (int i = 0; i < 5; i++)
{cin >> test[i];}*/
cout << "Distance: " << distance(y,known) << endl;
// Exit program.
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double hand_1[5],double hand_2[5])
{
// Declare variables.
int k;
double sum=0;
// Compute sum of absolute value differences.
for (k=0; k<=4; k++)
sum = sum + fabs(hand_1[k]-hand_2[k]);
// Return distance value.
return sum;
}
classes "date"
#include <iostream>
#include <cmath>
using namespace std;
class date
{
// prototypes for public members.
public:
void input();
void print_slash();
void print_comma();
// Declare private data members.
private:
int day, month, year;
};
void date::input()
{
cin >> day >> month >> year;
}
// This function prints the date as month/day/year.
void date::print_slash()
{
cout << month << "/" << day << "/" << year << "\n";
}
// This function prints the date as month day, year.
void date::print_comma()
{
switch(month)
{
case 1:
{cout << "Jan" << " " << day << "," << year << "\n";
break;
}
case 2:
{cout << "Feb" << " " << day << ","<< year << "\n";
break;
}
case 3:
{cout << "Mar" << " " << day << ","<< year << "\n";
break;
}
case 4:
{cout << "Apr" << " " << day << ","<< year << "\n";
break;}
case 5:
{cout << "May" << " " << day << ","<< year << "\n";
break;}
case 6:
{cout << "Jun" << " " << day << ","<< year << "\n";
break;
}
case 7:
{cout << "Jul" << " " << day << ","<< year << "\n";
break;}
case 8:
{cout << "Aug" << " " << day << ","<< year << "\n";
break;
}
case 9:
{cout << "Sep" << " " << day << ","<< year << "\n";
break;
}
case 10:
{cout << "Oct" << " " << day << ","<< year << "\n";
break;
}
case 11:
{cout << "Nov" << " " << day << ","<< year << "\n";
break;
}
case 12:
{cout << "Dec" << " " << day << ","<< year << "\n";
break;
}
}
}
_________________________________________________________________________________
#include <iostream>
#include <cmath>
#include "date.h"
using namespace std;
// Declare and initialize variables.
int main(void)
{
date date;
// Read input date.
cout << "Please enter the date as day, month, year" << endl;
date.input();
// Print date in comma and slash form.
cout << "Date in slash form:" << endl;
date.print_slash();
cout << "Date in comma form:" << endl;
date.print_comma();
// Exit program.
return 0;
}
#include <cmath>
using namespace std;
class date
{
// prototypes for public members.
public:
void input();
void print_slash();
void print_comma();
// Declare private data members.
private:
int day, month, year;
};
void date::input()
{
cin >> day >> month >> year;
}
// This function prints the date as month/day/year.
void date::print_slash()
{
cout << month << "/" << day << "/" << year << "\n";
}
// This function prints the date as month day, year.
void date::print_comma()
{
switch(month)
{
case 1:
{cout << "Jan" << " " << day << "," << year << "\n";
break;
}
case 2:
{cout << "Feb" << " " << day << ","<< year << "\n";
break;
}
case 3:
{cout << "Mar" << " " << day << ","<< year << "\n";
break;
}
case 4:
{cout << "Apr" << " " << day << ","<< year << "\n";
break;}
case 5:
{cout << "May" << " " << day << ","<< year << "\n";
break;}
case 6:
{cout << "Jun" << " " << day << ","<< year << "\n";
break;
}
case 7:
{cout << "Jul" << " " << day << ","<< year << "\n";
break;}
case 8:
{cout << "Aug" << " " << day << ","<< year << "\n";
break;
}
case 9:
{cout << "Sep" << " " << day << ","<< year << "\n";
break;
}
case 10:
{cout << "Oct" << " " << day << ","<< year << "\n";
break;
}
case 11:
{cout << "Nov" << " " << day << ","<< year << "\n";
break;
}
case 12:
{cout << "Dec" << " " << day << ","<< year << "\n";
break;
}
}
}
_________________________________________________________________________________
#include <iostream>
#include <cmath>
#include "date.h"
using namespace std;
// Declare and initialize variables.
int main(void)
{
date date;
// Read input date.
cout << "Please enter the date as day, month, year" << endl;
date.input();
// Print date in comma and slash form.
cout << "Date in slash form:" << endl;
date.print_slash();
cout << "Date in comma form:" << endl;
date.print_comma();
// Exit program.
return 0;
}
Subscribe to:
Posts (Atom)