Monday, December 18, 2017

Day 23 - C++ Classes

CLASSWORK:
XY Coordinate Class:
// These statements define a class for xy-coordinates.
// This declaration is stored in xy_coordinate.h.
#include <iostream>
#include <cmath>
using namespace std;
class xy_coordinate
{
// Declare function prototypes for public members.
public:
void input();
void print();
double radius();
double angle();
private:
// Declare private data members.
double x, y;
};

// These statements define implementation of an 
// xy_coordinate class. They are stored in xy-coordinate.h.
// This function reads the xy coordinates from the keyboard.
void xy_coordinate::input()
{
cin >> x >> y;
}
// This function prints the xy coordinates to the screen.
void xy_coordinate::print()
{
cout << "(" << x << "," << y << ")" << "\n";
}
// This function computes the radius.
double xy_coordinate::radius()
{
return sqrt(x*x + y*y);
}
// This function computes the angle in radians.
double xy_coordinate::angle()
{
// Compute angle of polar form.
double z, pi=3.141593;
if (x >= 0)
z = atan(y/x);
if (x<0 && y>0)
z = atan(y/x) + pi;
if (x<0 && y<=0)
z = atan(y/x) - pi;
if (x==0 && y==0)
z = 0;
return z;
}



Sunday, December 17, 2017

Day 19 - Arrays of Structures

CLASSWORK:
Hurricane Data:
#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;
}
/*?????????????????????????????????????????????????????????????*/
/* This function prints the hurricane information. */
void print_hurricane(struct hurricane h)
{
printf("Hurricane: %s \n",h.name);
printf("Year: %d, Category: %d \n",h.year,h.category);
return;



HOMEWORK:
Tsunami Data:
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr, fatalities;
double max_height;
char location[20];
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
FILE *waves;
/* Read and print information from the file. */
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
while (fscanf(waves,"%d %d %d %lf %d %s",&t[k].mo,&t[k].da,
&t[k].yr,&t[k].max_height,&t[k].fatalities,t[k].location[20]) == 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++)
if (t[k].max_height > ave)
printf("%s \n",t[k].location);
fclose(waves);
}
return 0;

Day 18 - Structures

HOMEWORK:
Fingerprints:
/* - */
/* This program stores fingerprint information in a structure. */
/* It then references a function to compute the overall category.*/
#include <stdio.h>
/* Define a structure for the fingerprint information. */
/* The order for fingertips is right hand, thumb to pinky, */
/* and left hand, thumb to pinky. The codes are L for loops, */
/* W for whorls, and A for arches. */
struct fingerprint
{
int ID_number;
double overall_category;
char fingertip[10];
};

int arches=0, loops=0, whorls=0;

int main(void)
{
/* Declare and initialize variables. */
struct fingerprint new_print;
double compute_category(struct fingerprint f);
/* Specify information for the new fingerprint. */
new_print.ID_number = 2491009;
new_print.overall_category = 0;
new_print.fingertip[0] = 'W';
new_print.fingertip[1] = 'L';
new_print.fingertip[2] = 'L';
new_print.fingertip[3] = 'W';
new_print.fingertip[4] = 'A';
new_print.fingertip[5] = 'L';
new_print.fingertip[6] = 'L';
new_print.fingertip[7] = 'W';
new_print.fingertip[8] = 'A';
new_print.fingertip[9] = 'L';
/* Reference function to compute overall category. */
new_print.overall_category = compute_category(new_print);

/* Print overall category computed by the function. */
printf("Fingerprint Analysis for ID: %i \n",
new_print.ID_number);
printf("Overall Category: %.2f \n",new_print.overall_category);
printf("Arches Count: %i, %i%% \n", arches,10*arches);
printf("Loops Count: %i, %i%% \n", loops,10*loops);
printf("Whorls Count: %i, %i%% \n", whorls,10*whorls);
printf("% \n");
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------*/
/* This function computes the overall category */
/* for a fingerprint. */
double compute_category(struct fingerprint f)
{
/* Declare and initialize variables. */
double Rt=0, Ri=0, Rm=0, Rr=0, Rp=0, Lt=0, Li=0, Lm=0, Lr=0,
Lp=0, num, den;

/* Set values based on whorls. */
if (f.fingertip[0] == 'W')
Rt = 16;
if (f.fingertip[1] == 'W')
Ri = 16;
if (f.fingertip[2] == 'W')
Rm = 8;
if (f.fingertip[3] == 'W')
Rr = 8;
if (f.fingertip[4] == 'W')
Rp = 4;
if (f.fingertip[5] == 'W')
Lt = 4;
if (f.fingertip[6] == 'W')
Li = 2;
if (f.fingertip[7] == 'W')
Lm = 2;

for(int i = 0; i <= 9; i++)
{
if (f.fingertip[i] == 'A')
arches++;
if (f.fingertip[i] == 'L')
loops++;
if (f.fingertip[i] == 'W')
whorls++;
}

/* Compute the numerator and denominator for overall category. */
num = Ri + Rr + Lt + Lm + Lp + 1;
den = Rt + Rm + Rp + Li + Lr + 1;
return num/den;
}

Saturday, December 16, 2017

Lab 18***: Programming Pointers and STR Functions

CLASSWORK/ HOMEWORK:
DNA String Lab:
/*??????????????????????????????????????????????????????????????-*/
/* 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>
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[]="AAACTGACATTGGACCTACTTTGACT",
short_str[]="ACT";
char *ptr1=long_str, *ptr2=short_str;

printf("Long String: %s \n", long_str);
printf("Short String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

printf("ptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

Part 1:
/*??????????????????????????????????????????????????????????????-*/
/* 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 MAX_SIZE 1000
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];

printf("Enter in long DNA string: \n");
scanf("%s",long_str);

printf("Enter in short DNA string: \n");
scanf("%s",short_str);

char *ptr1=long_str, *ptr2=short_str;

printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

printf("\nptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

Part 2:
/*??????????????????????????????????????????????????????????????-*/
/* 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 MAX_SIZE 1000
#define FILENAME "dnastring.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *dna;
dna = fopen(FILENAME, "r");
if (dna == NULL)
printf("Error opening input file. \n");
else
{
fscanf(dna,"%s",long_str);

printf("Enter in short DNA string: \n");
scanf("%s",short_str);

char *ptr1=long_str, *ptr2=short_str;

printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

printf("\nptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(dna);
}
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

Part 3:
/*??????????????????????????????????????????????????????????????-*/
/* 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 MAX_SIZE 1000
#define FILENAME "dnastring.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *dna;
dna = fopen(FILENAME, "r");
if (dna == NULL)
printf("Error opening input file. \n");
else
{
fscanf(dna,"%s",long_str);

printf("Enter in short DNA string: \n");
scanf("%s",short_str);

char *ptr1=long_str, *ptr2=short_str;

int short_length = strlen(short_str);
int long_length = strlen(long_str);
if (short_length > long_length)
printf("Error: Short string must be smaller than long string.\n");
else
{
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

//printf("\nptr1\t\tlong_str \n");
//printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(dna);
}
}
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

Day 20 (contd) - Dynamic Memory Allocation

CLASSWORK:
/* */
/* This program determines the maximum contiguous */
/* memory allocation that can be reserved during a */
/* specific program execution. */
#include <stdio.h>
#include <stdlib.h>
#define UNIT 1000000
int main(void)
{
/* Declare and initialize variables. */
double k=1;
int *ptr;
/* Find maximum amount of contiguous memory */
/* available in units of millions of integers. */
ptr = (int *)malloc(UNIT*sizeof(int));
while (ptr != NULL)
{
printf("%f integers \n",(k)*UNIT);
free(ptr);
k++;
ptr = (int *)malloc(k*UNIT*sizeof(int));
}
/* Print maximum amount of memory available. */
printf("Maximum contiguous memory available: \n");
/* Exit program. */
return 0;
}

HOMEWORK:
/* */ 
/* This program reads a seismic data file and then */ 
/* determines the times of possible seismic events. */ 
/* Dynamic memory allocation is used. */ 
#include <stdio.h>
#define FILENAME "seismic1.txt" 
#define THRESHOLD 1.5 
int main(void) 
{ /* Declare variables and function prototypes. */ 
int k, npts, short_window, long_window; 
double *sensor, time_incr, short_power, long_power, ratio; 
FILE *file_ptr; 
double power_w(double *ptr,int n); 
/* Read data header and allocate memory. */ 
file_ptr = fopen(FILENAME,"r"); 
if (file_ptr == NULL) 
printf("Error opening input file. \n"); 
else { fscanf(file_ptr,"%d %lf",&npts,&time_incr); 
sensor = (double *)malloc(npts*sizeof(double)); 
if (sensor == NULL) 
printf("Not enough memory available. \n"); 
else (no changes in the remainder of the program)


Lab 18 - Using Timer Interrupts

CLASSWORK:
Blinking LED with Timer Interrupts:
//Using Timer Interrupts with the Arduino
#include <TimerOne.h>
const int LED=13;

void setup() 
{
  pinMode(LED, OUTPUT);
  Timer1.initialize(1000000); //Set a timer of length 1000000
                              //microseconds (1 second)
  Timer1.attachInterrupt(blinky); //Runs "blinky" on each 
                                  //time interrupt
}
void loop() 
{
  // put your main code here, to run repeatedly:
}
//Timer interrupt function
void blinky()
{
  digitalWrite(LED, !digitalRead(LED)); //Toggle LED State
}

HOMEWORK:
Basic Speaker with Hardware and Timer Interrupts:
//Use Hardware and Timer Interrupts for Fun with Sound
//Include the TimerOne library
#include <TimerOne.h>
//Button pins
const int BUTTON_INT =0;
const int SPEAKER    =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); //Set a timer of length .5 seconds
  Timer1.attachInterrupt(changePitch); //Runs "blinky" on each 
                                  //time interrupt
}
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;   
}
//Timer interrupt function
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);
}

Friday, December 15, 2017

Day 20 - Dynamic Data Structures

CLASSWORK:
Getting the linked list nodal functions to work:
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This program tests the functions to insert and delete */
/* items in a linked list. */
#include <stdio.h>
#include <stdlib.h>
/* Define structure to represent a node in a linked list. */
struct node
{
int data;
struct node *link;
};

/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function returns a value of one if the linked list */
/* is empty. */
int empty(struct node *head)
{
/* Declare variables. */
int k = 0;
/* Determine if the list is empty. */
if (head == NULL)
k = 1;
/* Return integer. */
return k;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/


/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function prints a linked list. */
void print_list(struct node *head)
{
/* Declare variables. */
struct node *next;
/* Print linked list. */
if (empty(head))
printf("Empty list \n");
else
{
printf("List Values: \n");
next = head;
while (next->link != NULL)
{
printf("%d \n", next->data);
next = next->link;
}
printf("%d \n", next->data);
}
/* Void return. */
return;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/


/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function inserts a new node in a linked list. */
void insert(struct node **ptr_to_head, struct node *nw)
{
/* Declare variables and function prototypes. */
struct node **next;
/* Check for insert to empty list. */
if (empty(*ptr_to_head))
*ptr_to_head = nw;
else
/* Traverse list to find location for insert. */
{
next = ptr_to_head;
while (((*next)->data < nw->data) &&
((*next)->link != NULL))
next = &(*next)->link;
/* Check for duplicate data. */
if ((*next)->data == nw->data)
printf("Node already in list. \n");
else
/* Check for insert after last node. */
if ((*next)->data < nw->data)
(*next)->link = nw;
else
{
nw->link = *next;
*next = nw;
}
}
/* Void return. */
return;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/

/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/
/* This function deletes a node from a linked list. */
void remove_e(struct node **ptr_to_head, int old)
{
/* Declare variables and function prototypes. */
struct node *next, *last, *hold, *head;
/* Check for delete to empty list. */
head = *ptr_to_head;
if (empty(head))
printf("Empty list. \n");
else
/* Check for deletion of first node. */
{
if (head->data == old)
{
/* Delete first node. */
hold = head;
*ptr_to_head = head->link;
free(hold);
}
else
/* Traverse list to find old node. */
{
next = head->link;
last = head;
while ((next->data < old) && (next->link != NULL))
{
last = next;
next = next->link;
}
/* Delete node if found. */
if (next->data == old)
{
hold = last;
last->link = next->link;
free(hold);
}
else
printf("Value %d not in list. \n", old);
}
}
/* Void return. */
return;
}

int main(void)
{
/* Declare variables and function prototypes. */
int k = 0, old, value;
struct node *head, *next, *previous, *nw, **ptr_to_head = &head;
void insert(struct node **ptr_to_head, struct node *nw);
void remove_e(struct node **ptr_to_head, int n);
int empty(struct node *head);
void print_list(struct node *head);
/* Generate and print a linked list with five nodes. */
head = (struct node *)malloc(sizeof(struct node));
next = head;
for (k = 1; k <= 5; k++)
{
next->data = k * 5;
next->link = (struct node *)malloc(sizeof(struct node));
previous = next;
next = next->link;
}
previous->link = NULL;
print_list(head);
/* Allow user to insert or delete nodes in the list. */
while (k != 2)
{
printf("Enter 0 to delete node, 1 to add node, 2 to quit. \n");
scanf("%d", &k);
if (k == 0)
{
printf("Enter data value to delete: \n");
scanf("%d",&old);
remove_e(ptr_to_head, old);
print_list(head);
}
else
if (k == 1)
{
printf("Enter data value to add: \n");
scanf("%d",&value);
nw = (struct node *)malloc(sizeof(struct node));
nw->data = value;
nw->link = NULL;
insert(ptr_to_head, nw);
print_list(head);
}
}
/* Exit program. */
return 0;
}
/*¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C¨C*/


HOMEWORK:
Quick Sort Program:
#include <stdio.h>

int partition(int a[], int p, int r)
{
int i, j, pivot, temp;
pivot = a[p]; //pivot value is a[initial] = 25
i = p; // i = 0 (initially)
j = r; // j = 7 (initially)
while(1) //while previous statements are true
{ //Start at a[initial]
while(a[i] < pivot && a[i] != pivot) //if the array element is less than & not equal to the pivot...
i++; //...increment i by one, moving to next array element
while(a[j] > pivot && a[j] != pivot) //Starting at a[final], if the array element is greater than &
j--; //not equal to the pivot, then decrement j by one
if(i < j) //if i is less than j...
{ //...and neither of the previous conditions are met...
temp = a[i]; //Set the variable temp equal to the ith array element
a[i] = a[j]; //Set the ith array element equal to the jth array element
a[j] = temp; //Set the jth array element equal to the variable temp
} //In other words... we swap the values a[i] and a[j]
else //if (i !< j) && other conditions are not met...
{
return j; //print out j
}
}
}
/*--------------------------------------------------*/
/* a[] is the array, p is starting index, that is 0,
and r is the last index of array. */
void quicksort(int a[], int p, int r)
{
//printf("Starting Index: %i Ending Index: %i \n", p, r);
if(p < r)
{
int q = partition(a, p, r);
quicksort(a, p, q);
quicksort(a, q+1, r);
}
}

void display(int a[], int size)
{
for (int k=0; k<size; k++)
{
printf("%i ", a[k]);
}
printf("\n");
}

int main(void)
{
int a[] = {25,52,37,63,14,17,8,6};
int n = sizeof(a)/sizeof(a[0]);
printf("Input Array: \t");
display(a, n);
quicksort(a, 0, n-1);
printf("Output Array: \t");
display(a, n);
return 0;

}