Posts

Showing posts from November, 2017

Day 22 Homework: C++ Skills Development

#include <iostream> #include <fstream> #include <string> using namespace std; string printDirection(int c); string visual(int); void main() { int counterArr[8][2] = {0,0,0,0,0,0,0,0}, temp, total =0; double ave; ifstream data; data.open("winds1.txt"); while (!data.eof()) { data >> temp; counterArr[temp - 1][0]++; total++; cout << visual(temp); if (total % 5 == 0) cout << endl; } for (int i = 0; i < 8; i++) { if (counterArr[i][0] > temp) { temp = counterArr[i][0]; for (int j = 0; j < 8; j++) counterArr[j][1] = 0; counterArr[i][1] = 1; } else if (counterArr[i][0] == temp) { counterArr[i][1] = 1; } if(counterArr[i][0] > 0) cout << printDirection(i + 1) << ": " << counterArr[i][0] << endl; } ave = double(temp) / total; for (int i = 0; i < 8; i++) { if (counterArr[i][1] == 1) cout ...

Day 21 Homework: Intro to C++

c++ program that reads hand lengths from a file and compares to the users input #include <iostream> #include <cmath> #include <fstream> using namespace std; double distance(double *hand_1, double *hand_2); int main(void) { ifstream data; //Declare and initialize variables. double unknown[5] , known[5], temp, min = 0; int size = 1, *match; match = new int[size]; data.open("hand_lengths.txt"); cout << "Enter Unknown Hand Lengths: "; for (int k = 0; k <= 4; k++) { cin >> unknown[k]; } for (int counter = 1; !data.eof(); counter++) { for (int k = 0; k <= 4; k++) { data >> known[k]; } temp = distance(unknown, known); if (temp < min || counter == 1) { min = temp; match = new int[1]; match[0] = counter; size = 1; } else if (temp == min) { size++; int *tempArr = match; match = new int[size]; for (int i = 0; i < size - 1; i...

Day 20 Homework: Dynamic Data Structures

1. Write a program to implement the quick sort function on the list of numbers using an array #include <stdio.h> int partition(int a[], int p, int r); void quicksort(int a[], int p, int r); /* MAIN FUNCTION WITH PRE-DEFINED ARRAY OF ELEMENTS*/ int main(void){ int arr[] = {1,8,2,9,3,7,5,6,4}; quicksort(arr, 0, 8); return 0; } /* RECURSIVE ' quicksort ' FUNCTION CALL TO AN ARRAY, IS SEPARATED BY A PARTITION THAT IS INITIALIZED AS THE FIRST ELEMENT IN THE ARRAY AND SUBSEQUENT SUB-ARRAYS */ 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);   } } /* ' partition' SWAPS THE PIVOT UNTIL ALL THE ELEMENTS GREATER ARE POSITIONED ABOVE IT, AND ALL THE ELEMENTS LESS THAN ARE POSITIONED BELOW */ int partition(int a[], int p, int r) {   int i, j, pivot, temp;   pivot = a[p];   i = p; ...