Posts

Showing posts from December, 2017

Homework 13

Image
#include <stdio.h> #include <math.h> #define N 25 #define FILENAME "grid1.txt" int main(void) { /* Declare variables. */ int nrows, ncols, i, j, count = 0; double elevation[N][N]; FILE *grid; int hi, hj, li, lj, highest = 0, lowest = 10000; /* Read information from a data file. */ grid = fopen(FILENAME,"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]); if (elevation[i][j] > highest) { highest = elevation[i][j]; hi = i; hj = j; } else if (elevation[i][j] < lowest) { lowest = elevation[i][j]; li = i; lj = j; } } /* Determine and print peak locations. */ printf("Top left point defined as row 0, column 0 \n"); for (i=1; i<=nrows-2; i++) for (j=1; j<=ncols-2; j++) { if ((elevation[i-1][...

Day24

Image
// These statements define a class for complex numbers. // This declaration is stored in complex.h. #include <iostream> #include <cmath> using namespace std; class complex { // Declare function prototypes for public members. public: complex(); complex(double a, double b); void print(); void input(); double magn(complex); double angle(complex); complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); // Declare private members. double real, imag; private: }; // // Class implementation // // These statements define implementation of a complex class. // This function is the default constructor to initialize a // complex number that is not given a value. complex::complex() { real = 0; imag = 0; } // This function is the constructor to initialize a complex // number to a specified value. complex::complex(double a, double b) { real = a; imag = b; } // This function p...

Another Day 23

Image
#include <cmath> #include"date.h" #include <iostream> using namespace std; class date { public: date(); date(int m, int d, int y); void input(); void print1(); void print2(); // Declare private data members. private: int mm, dd, yyyy; char* month; }; date::date() { mm = dd = yyyy = 0; } date::date(int m, int d, int y) { mm = m; dd = d; yyyy = y; } void date::input() { cin >> mm >> dd >> yyyy; } void date::print1() { cout << mm << "/" << dd << "/" << yyyy << endl; } void date::print2() { switch (mm) { case 1: month = "January"; break; case 2: month = "February"; break; case 3: month = "March"; break; case 4: month = "April"; break; case 5: month = "May"; break; case 6: month = "June"; break; case 7: month = "July"; br...

Day 23

Image
#include<iostream> #include<fstream> #include<string> #include <cstring> using namespace std; #define FILENAME "winds1.txt" int main() { double percent[8] = { 0 }, *ptr3 = &percent[0]; int maxpos[8] = { 0 }; string direction[] = { "N", "NE", "E", "SE", "S", "SW", "W", "NW" }; void perecnt(int *a, double *percent, int k, int maxpos[]); void change(int *a, string *b, int k); void change_arr(int *a, string *b, string c[], int k); string dir_arr[8] = { "↑", "↗", "→", "↘", "↓", "↙", "←", "↖" }; ifstream data; data.open(FILENAME); int k = 0; if (data.fail()) cout << "Error opening input file." << endl; else { int row, coln; //row = coln = 5; data >> row; data >> coln; //int wind_1[5][5], *ptr1 = &wind_1[...

Day 22

Image
#include <iostream> #include<fstream> #include <cmath> using namespace std; #define FILENAME "data.txt" int main(void) { //Declare and initialize variables. double unknown[5] = { 5.4, 7.2, 7.9, 7.4, 5.1 }, known[5], result[6]; //= { 6.2, 7.0, 8.0, 7.4, 5.8 }; double distance(double hand_1[5], double hand_2[5], double result[]); //cout << "Please enter five known data" << endl; //cin >> known[0] >> known[1] >> known[2] >> known[3] >> known[4]; ifstream data; data.open(FILENAME); int k = 0; if (data.fail()) cout << "Error opening input file." << endl; else { while (!data.eof()) { data >> known[k]; k++; } // Compute and print distance. cout << "Distance: " << distance(unknown, known, result) << endl; for (int i = 0; i < 5; i++) { //cout.setf(ios::fixed); cout.precision(2); ...

Day 21

Image
Quick sort with linklist : #include <stdio.h> #include <stdlib.h> void quicksort(int *a, int p, int r); int partition(int *a, int p, int r); int main(void){ int* test; int data,size,count=0; //quicksort(test, 0, sizeof(test) / sizeof(int)-1); //for (int i = 1; i<=size(test)+1; i++) //printf("%d\n", test[0]); //printf("Empty list\n"); int k=0; printf("Enter max size of array \n"); scanf("%d", &size); test=(int *)malloc(size*sizeof(int)); while (k != 2) { printf("Enter 1 to add list, 2 to quit. \n"); scanf("%d", &k); if (k == 1) { printf("Enter data value to add: \n"); scanf_s("%d", &data); test[count]=data; quicksort(test,0,count); count++; printf("\nList\n"); for (int i = 0; i<count; i++) printf("%d\n", test[i]); //insert(test, data); } } //...

Day 20

//Use Hardware and Timer Interrupts for Fun with Sound //Include the TimerOne library #include <TimerOne.h> //Button pins const int BUTTON_INT=0; //Interrupt 0 (pin 2 on the Uno) const int SPEAKER=12; //Speaker on pin 12 //Music keys #define NOTE_C 65 #define NOTE_D 73 #define NOTE_E 82 #define NOTE_F 87 #define NOTE_G 98 #define NOTE_A 110 #define NOTE_B 123 //Volatile variables can change inside interrupts volatile int key = NOTE_C; volatile int octave_multiplier = 1; void setup () { //Set up serial Serial.begin(9600); pinMode (SPEAKER, OUTPUT); //The pin is inverted, so we want to look at the rising edge attachInterrupt(BUTTON_INT, changeKey, RISING); //Set up timer interrupt Timer1.initialize(500000); // (.5 seconds) Timer1.attachInterrupt(changePitch); //Runs "changePitch" on each //timer interupt } void changeKey() { octave_multiplier=1; if (key == NOTE_C) key = NOTE_D; else if (key == NOTE_D) key = NOTE_E; else if (key == NOT...

Nothing on Day19

Another Day 17

Image
#include <stdio.h> #define FILENAME "storms2.txt" /* Define structure to represent a hurricane. */ struct hurricane { char name[10]; int year, category; }; int main(void) { /* Declare variables and function prototype. */ int max_category = 0, k = 0, npts; struct hurricane h[100]; FILE *storms; void print_hurricane(struct hurricane h); /* Read and print information from the file. */ storms = fopen(FILENAME, "r"); if (storms == NULL) printf("Error opening data file. \n"); else { printf("Hurricanes with Maximum Category \n"); while (fscanf(storms, "%s %d %d", h[k].name, &h[k].year, &h[k].category) == 3) { if (h[k].category > max_category) max_category = h[k].category; k++; } npts = k; for (k = 0; k <= npts - 1; k++) if (h[k].category == max_category) print_hurricane(h[k]); fclose(storms); } /* Exit program */ return 0; } /*??...

Day 18

Image
/* */ /* This program reads the information for a hurricane */ /* from a data file and then prints it, using a function. */ #include <stdio.h> #define FILENAME "storms2.txt" struct tsunami get_info(void); /* Define structure to represent a hurricane. */ struct hurricane { char name[10]; int year, category; }; int main(void) { /* Declare variables and function prototype. */ struct hurricane h1; FILE *storms; void print_hurricane(struct hurricane h); /* Read and print information from the file. */ storms = fopen(FILENAME,"r"); if (storms == NULL) printf("Error opening data file. \n"); else { while (fscanf(storms,"%s %d %d",h1.name,&h1.year,&h1.category) == 3) print_hurricane(h1); fclose (storms); } /* Exit program. */ return 0; } /* */ /* This function prints the hurricane information. */ void print_hurricane(struct hurricane h) { printf("Hurricane: %s \n",h.name); printf("Year: %d, ...

Day 17

Image
Homework: /* 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 #define THRESHOLD 1.5 int main(void) { /* Declare variables and function prototypes. */ int k, npts, short_window, long_window; double sensor[MAX_SIZE], time_incr, short_power, 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 num...

Day 16

Image
#include<stdio.h> #include<math.h> #define FILENAME "ENS01.txt" #define MAX_SIZE 1000 int main(void) { int k=0, year[MAX_SIZE], qtr[MAX_SIZE], max_k=0,nor_k=0; double index[MAX_SIZE]; FILE *enso; //READ SENSOR DATA FILE enso = fopen(FILENAME,"r"); if(enso==NULL) printf("Error opening input file. \n"); else { while(fscanf(enso,"%d %d %lf", year+k,qtr+k,index+k)==3) { if(*(index+k)>*(index+max_k)) max_k=k; if(fabs(*(index+nor_k))> fabs(*(index+k))) nor_k=k; k++; } } //print data for max el nion condition printf("Maximun EL Nion Conditions in Dara File \n"); printf("Year: %d, Quarter: %d \n", *(year+max_k),*(qtr+max_k)); printf("EL Nion Conditions closest to normal in Dara File \n"); printf("Year: %d, Quarter: %d \n\n", *(year+nor_k),*(qtr+nor_k)); //Printout all for(int i=0;i<k;i++) printf(...