Day 23
#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[0][0];
//string wind_2[5][5], *ptr2 = &wind_2[0][0];
int *ptr1 = new int[row*coln];
string *ptr2 = new string[row*coln];
string *ptr_arr = new string[row*coln];
while (!data.eof())
{
data >> *(ptr1 + k);
k++;
}
//cout << k << endl;
change(ptr1, ptr2, k);
change_arr(ptr1, ptr_arr, dir_arr, k);
perecnt(ptr1, ptr3, k, maxpos);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < coln; j++)
//cout << wind_2[i][j] << " ";
cout << *(ptr2 + i*row + j) << " ";
//cout << direction[*(ptr1 + i*row + j)] << " ";
cout << endl;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < coln; j++)
//cout << wind_2[i][j] << " ";
cout << *(ptr_arr + i*row + j) << " ";
//cout << direction[*(ptr1 + i*row + j)] << " ";
cout << endl;
}
//for (int i = 0; i < 8; i++)
//cout << *(percent + i) << endl;
//cout << maxpos[i] << endl;
for (int i = 0; i < 8; i++)
if (maxpos[i])
cout << "The wind is blowing from the " << direction[i] << " " <<dir_arr[i]<<" "<< percent[i] << "% of the time." << endl;
if (percent[0]>0 || percent[1]>0)
if (percent[2]>0 || percent[3] > 0)
if (percent[4] > 0 || percent[5] > 0)
if (percent[6] > 0 || percent[7] > 0)
cout << "Possible cyclone or hurricane" << endl;
}
system("pause");
return 0;
}
void change(int *a, string *b, int k)
{
int c = 0;
while (c < k)
{
switch (*(a + c))
{
case 1:
*(b + c) = "N";
break;
case 2:
*(b + c) = "NE";
break;
case 3:
*(b + c) = "E";
break;
case 4:
*(b + c) = "SE";
break;
case 5:
*(b + c) = "S";
break;
case 6:
*(b + c) = "SW";
break;
case 7:
*(b + c) = "W";
break;
case 8:
*(b + c) = "NW";
break;
}
c++;
}
}
void change_arr(int *a, string *b,string c[],int k)
{
int count = 0;
while (count < k)
{
*(b + count) = c[ *(a+count) - 1];
count++;
}
}
void perecnt(int *a, double *percent, int k, int maxpos[])
{
int count = 0;
int max = 0;
//count for each direction
while (count < k)
{
(*(percent + *(a + count) - 1))++;
count++;
}
// find out max
for (int i = 0; i < 8; i++)
if (*(percent + i)>max)
max = *(percent + i);
//findout how many maxs
for (int i = 0; i < 8; i++)
if (max == *(percent + i))
maxpos[i] = 1;
//calculate percentage
for (int i = 0; i < 8; i++)
(*(percent + i)) /= k;
}
Lab
//Temperature Logger SD Card
#include <SD.h>
#include <Wire.h>
int temp_address=72; //1001000 written as decimal number
const int CS_PIN=10; //Define CS pin
long lastTime=0; //Initialize last time
//Define interrupt buttons
const int decreaseButton=0;
const int increaseButton=1;
volatile int interval=1000; //Initial interval
void setup() {
Serial.begin(9600);
Wire.begin(); //Create wire object
Serial.println("Initializing Card");
pinMode(CS_PIN,OUTPUT); //Set CS pin as output
//Attach interrupts
attachInterrupt(decreaseButton,decrease,RISING);
attachInterrupt(increaseButton,increase,RISING);
if(!SD.begin(CS_PIN)) //Print error message if SD.begin fails
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
}
//Function to decrease interval by 200 milliseconds
void decrease()
{
if(interval>200 && interval<=10000)
{
interval-=200;
}
else if(interval<=200)
{
interval=200; //Minimum interval is 200 milliseconds
}
}
//Function to increase interval by 200 milliseconds
void increase()
{
if(interval<10000 && interval>=200)
{
interval+=200;
}
else if(interval>=10000)
{
interval=10000; //Maximum interval is 10000 milliseconds
}
}
void loop() {
//Send request and start talking to device at specified address
Wire.beginTransmission(temp_address);
Wire.write(0); //Send bit asking for register zero, the data register
Wire.endTransmission(); //Complete Transmission
//Read temperature from the device
//Request 1 byte from specified address
Wire.requestFrom(temp_address,1);
//Wait for response
while(Wire.available() ==0);
//Get temperature and read it into variable
int temperature=Wire.read();
long timeStamp=millis();
//Find difference between time stamp and the last time
long timeDiff=timeStamp-lastTime;
//Continue for 60 seconds, reading after every interval
if(timeStamp<=60000 && timeDiff>=interval)
{
//Open a file and write to it
File dataFile=SD.open("temps.csv",FILE_WRITE);
if(dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.println(temperature);
dataFile.close(); //data not written until connection is closed
//Print information to screen for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.print(interval);
Serial.print(",");
Serial.println(temperature);
}
else //Print error message if file cannot be opened
{
Serial.println("Couldn't open temps file");
}
//Reset the last time
lastTime=timeStamp;
}
//Stop reading if time exceeds 60 seconds
else if(timeStamp>60000)
{
return;
}
}



Comments
Post a Comment