Site hosted by Angelfire.com: Build your free website today!
// MATRIXEX.CPP

#include "matrix.h"

int main()
{
  matrix  CharMatrix(3,3,' ');
  matrix  OriginalMatrix;
  int row, col;
  char c;

  for(row = 0; row < 3; row++)
  {
    for(col = 0; col < 3; col++)
    {
      cout << "Please enter a character for matrix position ("
	   << row << ", " << col << ") :";
      cin >> c;
      CharMatrix [row][col] = c;
    }
  }

  OriginalMatrix = CharMatrix;

  cout << "The CharMatrix has " << CharMatrix.numrows() << " rows and "
       << CharMatrix.numcols() << " columns.\n";

  CharMatrix.resize(2,2);

  cout << "CharMatrix now has " << CharMatrix.numrows() << " rows and "
       << CharMatrix.numcols() << " columns.\n";

  cout << "\nThe original matrix looked like:\n";

  for(row = 0; row < OriginalMatrix.numrows(); row++)
  {
    for(col = 0; col < OriginalMatrix.numcols(); col++)
    {
      cout << OriginalMatrix [row][col] << ' ';
    }
    cout << endl;
  }

  cout << "\nThe resized matrix looks like:\n";

  for(row = 0; row < CharMatrix.numrows(); row++)
  {
    for(col = 0; col < CharMatrix.numcols(); col++)
    {
      cout << CharMatrix [row][col] << ' ';
    }
    cout << endl;
  }

  return 0;
}

// NAMQUEUE.CPP // An example of using a queue. // compiler directives #include #include // global declarations struct queue_node // node for item in queue { char name[20]; queue_node *next; }; queue_node *head_ptr; // pointer to head of queue queue_node *tail_ptr; // pointer to tail of queue // constants for error conditions const int QUEUE_EMPTY = 2; const int QUEUE_ERROR = 1; const int NOERROR = 0; // function prototypes void handle_choice(int choice); int add_customer(char *name); void display_queue(); int next_customer(char *name); void delete_queue(); // main function int main() { int choice; tail_ptr = NULL; // Initialize pointers to head_ptr = NULL; // NULL since no queue exists. do { cout << endl; cout << "1 - Add a person to the queue\n"; cout << "2 - Take next customer\n"; cout << "3 - Display queue\n"; cout << "4 - Quit\n"; cout << "Enter choice: "; cin >> choice; if(choice != 4) // If user does not want to quit, { // do handle_choice. handle_choice(choice); } } while(choice != 4); // Loop until user chooses Quit. if(head_ptr != NULL) { // If queue isn't empty, delete_queue(); // delete the queue to free the memory. } return 0; } // end of main function // Function that handles the user's choice. void handle_choice(int choice) { char name[20]; switch(choice) { case 1: // User chose to add a customer to the queue. cin.ignore(80,'\n'); cout << "\nEnter customer's name: "; cin.get(name, 20); if(add_customer(name) == QUEUE_ERROR) { cout << "\nQUEUE ERROR\n\n"; } break; case 2: // User chose to get next customer from queue. if(next_customer(name) == QUEUE_EMPTY) { cout << "\nQUEUE EMPTY\n\n"; } else { cout << "\nThe next customer is " << name << endl; } break; case 3: // User chose to display the queue. if(head_ptr != NULL) { display_queue(); } else { cout << "\nQUEUE EMPTY\n\n"; } break; default: cout << "\nInvalid choice: Enter 1, 2, 3 or 4.\n\n"; break; } } // Function to add a customer to the queue. int add_customer(char *name) { int status = NOERROR; queue_node *new_node; new_node = new queue_node; // Allocate memory for the new node. if(new_node == NULL) { // If memory allocation problem, set error status = QUEUE_ERROR; // status and proceed to exit the function. } else // else copy data into the node and add to queue. { strcpy(new_node->name, name); if(tail_ptr == NULL) { // If queue is empty, make the new new_node->next = NULL; // node the head and tail. tail_ptr = new_node; head_ptr = new_node; } else { // If queue is not empty, add the tail_ptr->next = new_node; // customer to the queue. new_node->next = NULL; tail_ptr = new_node; } } return(status); // return error status } // Function to display entire queue. void display_queue() { queue_node *current_ptr; current_ptr = head_ptr; // Move current_ptr to head of queue. cout << endl; // Display blank line before output. if(current_ptr != NULL) // If queue is not empty, start displaying. { do { cout << current_ptr->name << endl; current_ptr = current_ptr->next; // set current_ptr to point to next node } while(current_ptr != NULL); // loop until end of list } else { cout << "QUEUE EMPTY\n\n"; } } // Function that gets next customer from queue. int next_customer(char *name) { queue_node *temp_ptr; int status = NOERROR; if(head_ptr != NULL) // If queue is not empty, get next customer. { strcpy(name, head_ptr->name); // Copy data out of head node. temp_ptr = head_ptr->next; // Set temp pointer to node after head. delete head_ptr; // Delete head node. head_ptr = temp_ptr; // Move head_ptr to new head of queue. if(head_ptr == NULL) { // IMPORTANT!! If last customer is removed from tail_ptr = NULL; // queue, set tail_ptr to NULL. } } else // If queue is empty, set error status. { status = QUEUE_EMPTY; } return(status); } // Function that frees the memory used by the queue. void delete_queue() { char name[20]; int status = NOERROR; do { status = next_customer(name); // Remove customers until } while(status != QUEUE_EMPTY); // queue is empty. }
// EVENSUM.CPP #include int Display(int num); int main() { int n; cout << "Enter an even number: "; cin >> n; Display(n); return 0; } int Display(int num) { if(num % 2 == 0) { while(num > 0) { num = num - 2; cout << num << endl; } } else { cout << "Error, entered number is not even, please try again.\n"; main(); } return(num); }
// MODTREE.CPP // TREE.CPP // A demonstration of tree traversals. #include #include struct tree_node { int string; tree_node *left; tree_node *right; }; tree_node *root; void add_to_tree(int string); void attach_node(tree_node *new_node_ptr); void in_order(tree_node *current_node); void pre_order(tree_node *current_node); void post_order(tree_node *current_node); void dispose_of_tree(tree_node *current_node); int main() { int string; root = NULL; // initialize root of tree to NULL cout << "Enter a series of strings to be ordered numerically\n" << "in a binary search tree. These strings can be last names\n" << "or any other strings you wish to enter.\n\n" << "Enter a blank string to end.\n\n"; do { cout << "Enter a string: "; cin >> string; //if(strlen(string) != 0) // { // add_to_tree(string); // } } while(string != 0); cout << "\nAn in-order traversal of the tree yields:\n"; in_order(root); cout << "\nPress Enter to continue.\n"; cin >> string; cout << "\nA preorder traveral of the tree yields:\n"; pre_order(root); cout << "\nPress Enter to continue.\n"; cin >> string; cout << "\nA postorder traveral of the tree yields:\n"; post_order(root); cout << "\nPress Enter to continue.\n"; cin >> string; dispose_of_tree(root); return 0; } void add_to_tree(int string) { tree_node *new_node_ptr; new_node_ptr = new tree_node; // Initialize new node. new_node_ptr->string = string; new_node_ptr->left = NULL; new_node_ptr->right = NULL; if(root == NULL) { root = new_node_ptr; } else { attach_node(new_node_ptr); } } void attach_node(tree_node *new_node_ptr) { tree_node *search_ptr; tree_node *follow_ptr; search_ptr = root; follow_ptr = root; while(search_ptr != NULL) { follow_ptr = search_ptr; if(new_node_ptr->string == search_ptr->string) { search_ptr = search_ptr->left; } else { search_ptr = search_ptr->right; } } if(new_node_ptr->string == follow_ptr->string) { follow_ptr->left = new_node_ptr; } else { follow_ptr->right = new_node_ptr; } } void in_order(tree_node *current_node) { if(current_node != NULL) { in_order(current_node->left); cout << current_node->string << endl; in_order(current_node->right); } } void pre_order(tree_node *current_node) { if(current_node != NULL) { cout << current_node->string << endl; pre_order(current_node->left); pre_order(current_node->right); } } void post_order(tree_node *current_node) { if(current_node != NULL) { post_order(current_node->left); post_order(current_node->right); cout << current_node->string << endl; } } void dispose_of_tree(tree_node *current_node) { if(!((current_node->left == NULL) && (current_node->right == NULL))) { if(current_node->right != NULL) { dispose_of_tree(current_node->right); } if(current_node->left != NULL) { dispose_of_tree(current_node->left); } } delete current_node; }
// Nathan Egelhof // SELSORT.CPP // SELSORT.CPP // An implementation of a selection sort algorithm. // compiler directives #include // function prototypes void selection_sort(int input_array[], int input_size); void display_array(int input_array[], int input_size); int main() { int nums[7] = {0, -31, 19, 104, 19, 73, -4}; int nums_length = 7; cout << "Unsorted Array:\n"; display_array(nums, nums_length); selection_sort(nums, nums_length); cout << "Sorted Array:\n"; display_array(nums, nums_length); return 0; } // Selection sort procedure. Sorts an array of ints in descending order. void selection_sort(int input_array[], int input_size) { int i, j; int small, temp; for (i = input_size - 1; i > 0; i--) { small = 0; // Initialize small to first element. // Find the smallest element between the positions 1 and i. for (j = 1; j <= i; j++) { if (input_array[j] < input_array[small]) { small = j; } } // Swap the smallest element found with element in position i. temp = input_array[small]; input_array[small] = input_array[i]; input_array[i] = temp; } } // Function that simply displays each element of input_array. void display_array(int input_array[], int input_size) { int i; for (i = 0; i < input_size; i++) { cout << input_array[i] << ' '; } cout << "\n\n"; }
//INSSORT2.CPP // INSSORT.CPP // An implementation of an insertion sort algorithm. // compiler directives #include // function prototypes void insertion_sort(int array[], int arrayLength); void display_array(int input_array[], int input_size); int main() { int nums[8] = {20, 31, 17, 47, 14, 35, -10, 35}; int nums_length = 8; cout << "Unsorted Array:\n"; display_array(nums, nums_length); insertion_sort(nums, nums_length); cout << "Sorted Array:\n"; display_array(nums, nums_length); return 0; } // Insertion sort function. Sorts an array of ints in descending order. void insertion_sort(int array[], int array_length) { int j, i, key; for (j = 1; j < array_length; j++) { key = array[j]; // Move all values smaller then key up one position. for (i = j - 1; (i >= 0) && (array[i] < key); i--) { array[i + 1] = array[i]; } array[i + 1] = key; // insert key into proper position } } // Function that simply displays each element of input_array. void display_array(int input_array[], int input_size) { int i; for (i = 0; i < input_size; i++) { cout << input_array[i] << ' '; } cout << "\n\n"; }
// BUBSORT2.CPP // BUBSORT.CPP // An implementation of a bubble sort algorithm. // compiler directives #include // function prototypes void bubble_sort(int array[], int arrayLength); void display_array(int input_array[], int input_size); int main() { int nums[5] = {20, 31, 17, 47, 14}; int nums_length = 5; cout << "Unsorted Array:\n"; display_array(nums, nums_length); bubble_sort(nums, nums_length); cout << "Sorted Array:\n"; display_array(nums, nums_length); return 0; } // Bubble sort function. Sorts an array of ints in descending order. void bubble_sort(int array[], int arrayLength) { int i, j, flag = 1; int temp; for(i = 1; (i <= arrayLength) && flag; i++) { flag = 0; for(j = 0; j < (arrayLength - i); j++) { if (array[j + 1] < array[j]) { temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; flag = 1; } } } } // Function that simply displays each element of input_array. void display_array(int input_array[], int input_size) { int i; for (i = 0; i < input_size; i++) { cout << input_array[i] << ' '; } cout << "\n\n"; }
// QUIKSORT.CPP // An implementation of a quicksort algorithm. // compiler directive #include // function prototypes void quicksort(int input_array[], int top, int bottom); int partition(int input_array[], int top, int bottom); void display_array(int input_array[], int input_size); int main() { int nums[5] = {20, 31, 17, 47, 14}; int nums_length = 5; cout << "Unsorted Array:\n"; display_array(nums, nums_length); quicksort(nums, 0, nums_length - 1); cout << "Sorted Array:\n"; display_array(nums, nums_length); return 0; } // Quicksort procedure. Sorts an array of ints in descending order. void quicksort(int input_array[], int top, int bottom) { int middle; if (top < bottom) { middle = partition(input_array, top, bottom); quicksort(input_array, middle + 1, bottom); quicksort(input_array, top, middle); } } // partitions input_array, returning middle index. Used by procedure quicksort. int partition(int input_array[], int top, int bottom) { int x = input_array[top]; int i = top - 1; int j = bottom + 1; int temp; do { // move j towards top to the next element less than or equal to x. do { j--; } while(x > input_array[j]); // move j towards bottom to the next element greater than or equal to x. do { i++; } while(x < input_array[i]); if (i < j) { // switch elements at positions i and j. temp = input_array[i]; input_array[i] = input_array[j]; input_array[j] = temp; } } while(i < j); // loop ends when i and j have met in the middle. return j; // j and above represents top partition, below j is bottom partition. } // Function that simply displays each element of input_array. void display_array(int input_array[], int input_size) { int i; for (i = 0; i < input_size; i++) { cout << input_array[i] << ' '; } cout << "\n\n"; }