#include #include #include #include /* * Example from Nov 23, 2013 * * * */ #define MAXVALS 10 #define MAX 20 #define BIGSEARCH 13 #define AVOWEL 'a' #define SORTMAX 15 const int MAXSZ=42; const char roomtypenames[5][40] = {"indoor sales room", "loading area", "outdoor sales garden section", "bathroom", "office" }; // 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; struct person { long ID; char firstname[100]; char *lastname; char jbtyp; float hrwg; float hrswkd; struct person *next; struct person *prev; }; 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 scorelist[BIGSEARCH] = {90,87,88,-79, 45, 98, 32, 99, 100, -46, 87,68}; char nameclass[ ]= {'C','_','c','l','a','s','s','\0'}; char nameclass2[40]="C 1320"; char buffer[100]; int sorttest[] = {105, 30, 42, 200, 2, 60, 17, 61, 100, 25, 62, -18, 63, 99, 31}; char sti, sta; int se = 68, ft; float in = 0.0, wage, hours; int linresult, temp; char words[20][40]={{0}}; // array of strings char *tempwd, *word, *num; // &(buffer[0]) int *tempint, numvals; int fibofk, factofk, index1, index2, index3; int day, mo, yr; long int tempidnum; FILE *infile, outfile; struct person *headptr, *currptr, *followptr, *newptr; int seed=4, flag; infile = fopen("peoplefileA.txt","r"); if (infile == NULL) { printf ("Input file is not available"); return 0; } headptr = (struct person *) malloc(sizeof(struct person)); tempwd = (char *) malloc(sizeof(char)*100); // give values to elements of the struct fscanf(infile, "%ld %c %f %f %s", &tempidnum, &sti, &wage, &hours, &words[0]); getline(&tempwd, &index1, infile); printf("\nInput data for head ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s", tempidnum, sti, wage, hours, words[0], tempwd); // get values from a file, from the user, from defined info... headptr->ID = tempidnum; headptr->jbtyp = sti; headptr->hrwg = wage; headptr->hrswkd = hours; strcpy(headptr->firstname, words[0]); headptr->lastname = (char *) malloc(sizeof(char)*(index1+1)); strcpy(headptr->lastname, tempwd); printf("\nheadptr->ID is %ld", headptr->ID); headptr->next = NULL; headptr->prev = NULL; currptr = headptr; followptr = NULL; // now start adding structs to the linked list while(!feof(infile)) // condition to stop reading data { newptr = (struct person *) malloc(sizeof(struct person)); // put in values fscanf(infile, "%ld %c %f %f %s", &tempidnum, &sti, &wage, &hours, &words[0]); getline(&tempwd, &index1, infile); printf("\nInput data for new ptr is id= %ld, job type = %c, wage=%f, hours=%f, name is %s %s", tempidnum, sti, wage, hours, words[0], tempwd); // get values from a file, from the user, from defined info... newptr->ID = tempidnum; newptr->jbtyp = sti; newptr->hrwg = wage; newptr->hrswkd = hours; strcpy(newptr->firstname, words[0]); newptr->lastname = (char *) malloc(sizeof(char)*(index1+1)); strcpy(newptr->lastname, tempwd); newptr->next = NULL; newptr->prev = NULL; printf("\ndata in the ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s\n", newptr->ID, newptr->jbtyp, newptr->hrwg, newptr->hrswkd, newptr->firstname, newptr->lastname); currptr = headptr; followptr = NULL; // newptr->ID = rand(); printf("\nnewptr->ID is %ld", newptr->ID); if (newptr->ID < headptr->ID) { // new head of list newptr->next = headptr; printf("\nbefore head of list: newptr->ID is %ld, headptr->ID is %ld",newptr->ID, headptr->ID); headptr = newptr; } else { // find location in between two elements while ((currptr->next != NULL) && // order of the tests is important (newptr->ID > currptr->ID )) { //increment the current pointer followptr = currptr; currptr = currptr->next; printf("\nin while: followptr->ID is %ld, currptr->ID is %ld",followptr->ID, currptr->ID); } if (newptr->ID < currptr->ID) // currptr->ID = followptr->next->ID { followptr->next = newptr; newptr->next = currptr; printf("\nbetween: followptr->ID is %ld, newptr->ID is %ld, currptr->ID is %ld",followptr->ID, newptr->ID, currptr->ID); } else if (currptr->next == NULL) // after last element { currptr->next = newptr; printf("\nat end: currptr->ID is %ld, newptr->ID is %ld",currptr->ID, newptr->ID); } else { printf("\nWHAT CASE AM I?"); } } } // GO through the list and print addresses of each struct currptr = headptr; printf("\n\nLinked list elements:"); while ( currptr != NULL) { printf("\n currptr is %p, ID is %ld, currptr->next is %p",currptr, currptr->ID, currptr->next); printf("\ndata in the ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s\n", currptr->ID, currptr->jbtyp, currptr->hrwg, currptr->hrswkd, currptr->firstname, currptr->lastname); currptr = currptr->next; } printf("\nEnd of Linked list"); // scope : where is a variable visible? // global scope - visible everywhere, defined outside of main // local scope - defined in a function, visible within a function // storage class : how long does a variable exist while the program is running? // automatic - created at declaration, destroyed at end of function where declared // static - created at declaration, destroyed at end of program for local vars // - created at declaration and local to current FILE for global definitions // register - behaves like an auto, requests to OS to store value in register, // not guaranteed - defaults to auto // external - links identifiers across files; file with main in it is always to starting // file. In main file, define global constants and function prototypes, and main // Global constants are the same, prototypes have extern keyword // extern int howdyfunc(int arr[], int a); // For the file with howdyfunc in it, define function normally but any global // constants from main file are repeated with extern keyword // extern const int MAXSZ=42; // typedef - gives a new or additional name to an existing type // to get stock type 0 from room 2 use gardenstore[2].amountofstock[0] /* k= 1; // queue - an array with FIFO first-in first-out behavior // stack - an array with LIFO last-in first-out behavior // 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) { static int callcount = 1; 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); printf("\nMergesort has been called %d times\n",callcount++); 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; } */