#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 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; 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[] = {-18, 2, 17, 25, 30, 31, 42, 60, 61, 62, 63, 99, 100, 105, 200}; 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]; // to get stock type 0 from room 2 use gardenstore[2].amountofstock[0] /* k = 10; factofk = fact(k); printf("\nFactorial of %d is %d\n", k, factofk); */ k= 1; /* while ( k != -1) { printf(" Enter the index of the desired Fibonacci value or -1 to quit :"); scanf("%d",&k); fibofk = fib(k); printf("\nThe %dth Fibonacci value is %d\n", k, fibofk); } k= 1; while ( k > 0) { printf(" How many rows in the pyramid? :"); scanf("%d",&k); fibofk = pyramid(k); } printf("\nThe function returned %d\n", fibofk); k= 1; while ( k > 0) { printf(" How many rows in the recursive pyramid? :"); scanf("%d",&k); fibofk = pyrarecur(k, k); } printf("\nThe function returned %d\n", fibofk); */ /* */ // Search for a value in array sorttest which is ordered small to large k= 1; while ( k != 999 ) { printf(" 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; } /* 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 binsearch(int array[], int first, int last, int searchk) { int middle = (first + last)/2; 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; } */