#include #include #include /* * Example from Nov 5, 2013 * * * */ #define FAHRFREEZE 32 #define CELCFREEZE 0 #define MAXVALS 10 #define MAX 20 #define BIGSEARCH 13 #define AVOWEL 'a' #define XERIC_PERENNIAL 0 #define ANNUAL 1 #define SORTMAX 15 const char roomtypenames[5][40] = {"indoor sales room", "loading area", "outdoor sales garden section", "bathroom", "office" }; //int z = 8; //A global variable will get a 0 on your lab in Dr T's class // declaring a struct type struct day { int day, month, year; char moname[20]; }; typedef struct { float area; int amountofstock[6]; char roomname[100]; int roomnumber; float length, width; int roomtype; struct day roomday; } ROOM; typedef int BASICSIZE; void mergesort(int array[SORTMAX], int firstindex, int lastindex); void merge(int array[SORTMAX], int firstA, int lastA, int firstB, int lastB); void move( int fa[SORTMAX], int first1, int last1, int la[SORTMAX], int first2); char * printroomtype(int rt ); int howdyfunc(int arr[], int a); int pyramid(int nstop); int main(void) { int celsius_temp = 0, d, e, fahrenheit_temp= 42, c=0, f= 42, x, i, j, k; BASICSIZE variable1; double stockinfo[6][4] = {{4, 1.99, .25},{0,0,0}, {12, 87.95, 2.50}}; int multiD[2][3][4][5] = {{{{0}}}}; int scorelist[BIGSEARCH] = {90,87,88,-79, 45, 98, 32, 99, 100, -46, 87,68}; float classavgs[20]= {0}; char nameclass[ ]= {'C','_','c','l','a','s','s','\0'}; char nameclass2[40]="C 1320"; char notstring[ ] = {'f','o','o','t'}; char buffer[100]; int sorttest[] = {105, 30, 42, 200, 2, 60, 17, 61, 100, 25, 62, -18, 63, 99, 31}; double stockprice[6]={0}; int stockamts[6]={0}; char stocktype[6]={'X','A'}; char sti, sta; int se = 68, ft; float in = 0.0; int linresult, temp; char words[20][40]={{0}}; // array of strings char *tempwd, *word, *num; // &(buffer[0]) int *tempint; int numvals; linresult = 10; int fibofk, factofk, index1, index2, index3; ROOM theroom = { 0.0, {0,0,0,0,0,0}}; ROOM room2; int day, mo, yr; ROOM gardenstore[25]; ROOM *headptr, *currptr, *followptr; //get length of a ROOM in room2 :: room2.length headptr = (ROOM *) malloc(sizeof(ROOM)); //use headptr and get length :: (*headptr).length // to get stock type 0 from room 2 use gardenstore[2].amountofstock[0] /* */ k= 1; // Sort an array into order from smallest to largest printf("\nBefore mergesort Values of sorttest \n"); for (k=0; k < 16; k++) printf("\n%d",sorttest[k]); mergesort(sorttest, 0, 15); printf("\n\nValues of sorttest after mergesort\n"); for (k=0; k < 16; k++) printf("\n%d",sorttest[k]); // Search for a value in array sorttest which is ordered small to large k= 1; while ( k != 999 ) { printf("\n What value should we search for in the array? Type 999 to stop :"); scanf("%d",&k); if (k != 999) fibofk = binsearch(sorttest, 0, 15, k); //array, number of elements, search element printf("\nThe function returned %d\n", fibofk); } return 0; } /* Mergesort ( array, firstindex, lastindex) Test to see if array has only one element: if (firstindex == lastindex) If so, return If not, Cut array in half: middle = (lastindex + firstindex)/2 Sort first half from index [first1 = firstindex] to [last1 = middle] mergesort(array, firstindex, middle); Sort second half from index [first2 = middle + 1] to [last2 = lastindex] mergesort(array, (middle + 1), lastindex); When first half and second half sorts return, Merge the halves back together merge(array, first1, last1, first2, last2) */ void mergesort(int array[SORTMAX], int firstindex, int lastindex) { int middle; printf("\nIn mergesort -- start at index %d to index %d", firstindex,lastindex); if (firstindex == lastindex) return; middle = (lastindex + firstindex)/2; // find middle of array printf("\n\tMergesort first half**"); mergesort(array, firstindex, middle); // sort first half printf("\n\tMergesort second half@@"); mergesort(array, (middle + 1), lastindex); // sort second half printf("\n-- calling merge"); merge(array, firstindex, middle, (middle + 1), lastindex); return; } /* Merge(array, firstA, lastA, firstB, lastB) finalarray is a temporary array to hold the final merged array Take two sorted subarrays A and B of lengths lenA=(lastA - firstA +1) and lenB=(lastB-firstB+1) finlen = lenA + lenB At first merge, indexA = firstA At first merge, indexB = firstB At beginning, indexfin = 0 // index for finalarray while (indexA <= lastA) && (indexB <= lastB) { Compare first elements of A and B, else if (A[indexA] < B[indexB]) Put A[indexA] into finalarray[indexfin++] Increment indexA of A else Put B[indexB] into finalarray[indexfin++] Increment the indexB of B } Checking which array is not empty if (indexA == lastA) // end of array A Put remainder of array B in finalarray: move(array, indexB, lastB, finalarray, indexfin) else // if (indexB == lastB) // end of array B Put remainder of array A in finalarray move(array, indexA, lastA, finalarray, indexfin) move(finalarry, 0, finlen-1, array, firstA); */ void merge(int array[SORTMAX], int firstA, int lastA, int firstB, int lastB) { int finalarray[SORTMAX]; //temporary array to hold the final merged array int lenA, lenB, finlen, indexA, indexB, indexfin; lenA=(lastA - firstA +1); lenB=(lastB-firstB+1); finlen = lenA + lenB; indexA = firstA; indexB = firstB; indexfin = 0; // index for finalarray while ((indexA <= lastA) && (indexB <= lastB)) { if (array[indexA] < array[indexB]) // Compare first elements of A and B, { // Put A[indexA] into finalarray[indexfin++] finalarray[indexfin++] = array[indexA++]; // Increment indexA of A } else { //Put B[indexB] into finalarray[indexfin++] finalarray[indexfin++] = array[indexB++]; // Increment indexB } } // Checking which array is not empty if (indexA > lastA) // end of array A { // Put remainder of array B in finalarray: move(array, indexB, lastB, finalarray, indexfin); } else // if (indexB > lastB) // end of array B { // Put remainder of array A in finalarray move(array, indexA, lastA, finalarray, indexfin); } move(finalarray, 0, finlen-1, array, firstA); return; } /* void move( int fa[], int first1, int last1, int la[], int first2) { while (first1 <= last1) la[first2++] = fa[first1++]; } */ void move( int fa[SORTMAX], int first1, int last1, int la[SORTMAX], int first2) { while (first1 <= last1) la[first2++] = fa[first1++]; } /* Binary search = binsearch(sorttest, 0, num-1, k); //array, index of first element, //index of last element, search element // where num is number of elements in array int binsearch(int array[num], int first, int last, int searchk) Array from index [first] to index [last] find the middle element ((last + first) / 2) = m // index of middle element Test if (first >= last) return -1; Test if array[m] == k return m; // Found the value equal to k at location m Test if array[m] < k Binsearch the array from [m+1] to [last]; //binsearch(ar,m+1, last, k) Else Binsearch the array from [first] to [m - 1]; //binsearch(ar,first,m-1,k) int sorttest[] = {-18, 2, 17, 25, 30, 31, 42, 60, 61, 62, 63, 99, 100, 105, 200}; */ int binsearch(int array[], int first, int last, int searchk) { int middle = (first + last)/2; printf("\nMiddle index is [%d]",middle); if (first > last) return -1; // base case; if (array[middle] == searchk) return middle; // base case; if (array[middle] < searchk) return binsearch(array, middle+1, last, searchk); else return binsearch(array, first, middle-1, searchk); } /* Recursion - function that calls itself Mathematical example - factorial; only defined for positive integers k! = k * (k-1)! is the generalized recursive form of factorial 5! = 5 * 4! = 120 4! = 4 * 3! = 24 3! = 3 * 2! = 6 2! = 2 * 1! = 2 1! = 1 * 0! = 1 0! = 1 is "base case"; i.e. by definition (-1)! is undefined */ /* int fact(int k) { if (k < 0) // error checking domain { return -1; } if (k == 0) // base case { return 1; } // k > 0 // k! = k * (k-1)! is the generalized recursive form of factorial // fact(k) = k * fact(k-1) return (k * fact(k-1)); } */ /* //Can you implement factorial without recursion? int fact2(int k) { int product = 1; if (k < 0) // error checking domain { return -1; } for ( ;k > 0; k--) { product = product * k; } return product; } } */ /* Common recursive math function Fibonacci sequence 0,1,1,2,3,5,8,13,21,34,55,89, input is index 0,1,2,3,4,5,6,7, 8, 9, 10,11, current value = previous value + 2nd previous value fib(k) = fib(k-1) + fib(k-2) 89 = 55 + 34 55 = 34 + 21 34 = 21 + 13 // fib(9) = fib(8) + fib(7) . . 3 = 2 + 1 2 = 1 + 1 1 = 1 + 0 // fib(2) = fib(1) + fib(0) 1 by definition; base case fib(1) = 1 - fib(b) = b 0 by definition; base case fib(0) = 0 / Fibonacci sequence */ int fib(int index) { if (index < 0) return -1; if ((index == 0) || (index == 1)) return index; // index > 1 // fib(index) = fib(index-1) + fib(index-2) return fib(index - 1) + fib(index - 2); } /* Recursive function to print out a pyramid of numbers 1 22 333 4444 Where do we start? n = 1 is start Where do we stop? input to function should be "nstop" Definition of a "row" in the pyramid? for number n, print n "digit ns" for (i = 1; i <= nstop; i++) // the rows { for (j = 1; j <= i; j++) // one row { printf("%d",i); // one digit/element } printf("\n"); } */ int pyramid(int nstop) { int i, j; if (nstop <= 0) return 0; for (i = 1; i <= nstop; i++) // the rows { for (j = 1; j <= i; j++) // one row { printf("%d",i); // one digit/element } printf("\n"); } return 1; } int pyrarecur(int nstop, int ncur) // intial call is pyrarecur(k,k) { int i, j; if (ncur <= 0) //error checking and base case nstop == 0 return 0; i = nstop - ncur + 1; for (j = 1; j <= i; j++) // one row { printf("%d",i); // one digit/element } printf("\n"); return pyrarecur(nstop, ncur - 1); } //Linear search - any data type, any size array, unsorted, assume no dupes //Searching for what element - same type as data in array int linsearch(int sl[], int maxsize, int searchelement) { int index = 0; int foundat = -1; printf("\nIn linsearch, searchelement is %d and maxsize is %d\n",searchelement, maxsize); // for( index = 0; (index < maxsize)&&(sl[index] != searchelement) ; index++) for( index = 0; (index < maxsize); index++) { if (sl[index] == searchelement) foundat = index; printf("\nIn for loop, sl[%d] is %d\n", index, sl[index]); } //foundat = index; printf("\nIn linsearch, foundat is %d\n",foundat); return foundat; } /* const char roomtypenames[5][] = {"indoor sales room", "loading area", "outdoor sales garden section", "bathroom", "office" } */ /* char * printroomtype(int rt ) { char * temp; // use the constant global array of roomtype names temp = &(roomtypenames[rt]); return temp; } */