Day 21 Nov 9
For the homework I had to create a quick sort function, and then use a quicksort function in a linked list. Here's a screenshot of both parts of the homework:
Just the quick list part
#include <stdio.h>
#include <stdlib.h>
//void quicksort(int[], int, int);
//int partition( int[], int, int);
int partition(int a[], int p, int r)
{
int i,j, pivot, temp;
pivot = a[p];
i = p;
j = r;
while(1)
{
while(a[i] < pivot && a[i] != pivot)
i++;
while(a[j] > pivot && a[j] != pivot)
j--;
if (i <j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
return j;
}
}
}
void quicksort(int a[], int p, int r)
{
if (p < r)
{
int q;
q = partition(a, p ,r);
quicksort(a,p,q);
quicksort(a, q+1, r);
}
}
int main(void)
{
struct sort;
int a[]= {25, 52, 37, 63, 14, 17, 8, 6};
int i;
quicksort( a, 0, 7);
printf("Sorted array is: ");
for(i=0; i<8; ++i)
printf(" %d", a[i]);
return 0;
}
Here's a screenshot of the linked list working.
For lab we had to connect a speaker to an interrupter here's a video
here's the code
#include <TimerOne.h>
const int BUTTON_INT =0;
const int SPEAKER =12;
#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 int key = NOTE_C;
volatile int octave_multiplier = 1;
void setup()
{
Serial.begin(9600);
pinMode (SPEAKER, OUTPUT);
attachInterrupt(BUTTON_INT, changeKey, RISING);
Timer1.initialize(500000);
Timer1.attachInterrupt(changePitch);
// put your setup code here, to run once:
}
void changeKey()
{
octave_multiplier =1;
if (key ==NOTE_C)
key = NOTE_D;
else if(key == NOTE_D)
key = NOTE_E;
else if (key == NOTE_E)
key = NOTE_F;
else if (key == NOTE_F)
key = NOTE_G;
else if (key == NOTE_G)
key = NOTE_A;
else if (key ==NOTE_A)
key = NOTE_B;
else if (key == NOTE_B)
key = NOTE_C;
}
void changePitch()
{
octave_multiplier = octave_multiplier*2;
if (octave_multiplier >16) octave_multiplier =1;
tone(SPEAKER, key*octave_multiplier);
}
void loop()
{
Serial.print("Key: ");
Serial.print(key);
Serial.print(" Multiplier: ");
Serial.print(octave_multiplier);
Serial.print(" Frequency: ");
Serial.println(key*octave_multiplier);
delay(100);
}// put your main code here, to run repeatedly:
Just the quick list part
#include <stdio.h>
#include <stdlib.h>
//void quicksort(int[], int, int);
//int partition( int[], int, int);
int partition(int a[], int p, int r)
{
int i,j, pivot, temp;
pivot = a[p];
i = p;
j = r;
while(1)
{
while(a[i] < pivot && a[i] != pivot)
i++;
while(a[j] > pivot && a[j] != pivot)
j--;
if (i <j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
return j;
}
}
}
void quicksort(int a[], int p, int r)
{
if (p < r)
{
int q;
q = partition(a, p ,r);
quicksort(a,p,q);
quicksort(a, q+1, r);
}
}
int main(void)
{
struct sort;
int a[]= {25, 52, 37, 63, 14, 17, 8, 6};
int i;
quicksort( a, 0, 7);
printf("Sorted array is: ");
for(i=0; i<8; ++i)
printf(" %d", a[i]);
return 0;
}
Here's a screenshot of the linked list working.
For lab we had to connect a speaker to an interrupter here's a video
here's the code
#include <TimerOne.h>
const int BUTTON_INT =0;
const int SPEAKER =12;
#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 int key = NOTE_C;
volatile int octave_multiplier = 1;
void setup()
{
Serial.begin(9600);
pinMode (SPEAKER, OUTPUT);
attachInterrupt(BUTTON_INT, changeKey, RISING);
Timer1.initialize(500000);
Timer1.attachInterrupt(changePitch);
// put your setup code here, to run once:
}
void changeKey()
{
octave_multiplier =1;
if (key ==NOTE_C)
key = NOTE_D;
else if(key == NOTE_D)
key = NOTE_E;
else if (key == NOTE_E)
key = NOTE_F;
else if (key == NOTE_F)
key = NOTE_G;
else if (key == NOTE_G)
key = NOTE_A;
else if (key ==NOTE_A)
key = NOTE_B;
else if (key == NOTE_B)
key = NOTE_C;
}
void changePitch()
{
octave_multiplier = octave_multiplier*2;
if (octave_multiplier >16) octave_multiplier =1;
tone(SPEAKER, key*octave_multiplier);
}
void loop()
{
Serial.print("Key: ");
Serial.print(key);
Serial.print(" Multiplier: ");
Serial.print(octave_multiplier);
Serial.print(" Frequency: ");
Serial.println(key*octave_multiplier);
delay(100);
}// put your main code here, to run repeatedly:


Comments
Post a Comment