Posts

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...