#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 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]; 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 = 10; fibofk = fib(k); printf("\nThe %dth Fibonacci value is %d\n", k, fibofk); /* */ return 0; } /* 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 > 0 // fib(index) = fib(index-1) + fib(index-2) return fib(index - 1) + fib(index - 2); } //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; } */