#include #include #include /* * Example from Oct 3, 2013 * * * */ /* OCt 3 Quiz 1) What type of algorithms did we start discussing last class? 2) What is the name of the algorithm we implemented? 3) Describe briefly how it works. */ #define FAHRFREEZE 32 #define CELCFREEZE 0 #define MAXVALS 10 #define MAX 20 #define BIGSEARCH 13 //int z = 8; //A global variable will get a 0 on your lab in Dr T's class 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; 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}; char stocktype[6]={'X','A'}; char sti; int se = 68; int linresult, temp; char words[20][40]={{0}}; // array of strings char *tempwd, *word, *num; // &(buffer[0]) int *tempint; linresult = 10; int index1, index2, index3; tempwd = (char *) malloc(sizeof(char)*(linresult + 1) ); printf("\nValue of tempwd %p, address of buffer %p, linresult = %d stockprice[0] = %lf\n", tempwd, buffer, linresult, stockprice[0]); printf("\nEnter a line from a nursery rhyme: "); temp = getline(&tempwd, &linresult, stdin); printf("\nValue of tempwd %p, address of buffer %p, linresult = %d, temp = %d stockprice[0] = %lf\n", tempwd, buffer, linresult, temp, stockprice[0]); printf("\n%s -- %c\n",tempwd, tempwd[2]); x = 7; k=-1; // strlen x = strlen(tempwd); printf("\ntempwd is %d chars\n",x); // strcpy printf("\nbuffer %s, buffer is at %p\n",buffer, buffer); printf("\ntempwd %s, tempwd's value is %p\n",tempwd, tempwd); // buffer = tempwd; strcpy(buffer, tempwd); tempwd = buffer; printf("\nafter %s, buffer is at %p\n",buffer, buffer); printf("\nafter %s, tempwd's value is %p\n",tempwd, tempwd); // strca tempwd = (char *) malloc(sizeof(char)*100); strcpy(tempwd, "The owl and the pussycat went to sea "); printf("\ncopied %s, tempwd's value is %p\n",tempwd, tempwd); strcat(tempwd,buffer); //seg fault printf("\nconcatenated %s, buffer is at %p\n",buffer, buffer); printf("\nconcatenated %s, tempwd's value is %p\n",tempwd, tempwd); // strtok word = NULL; printf("\naddr in word is %p\n",word); word = strtok(tempwd, "., ;:?"); //strcpy(words[0],word); //printf("\nword was %s and addr is %p and words[0] is %s\n", word, word, words[0]); i= 0; while (word != NULL) { // printf("\nin while"); strcpy(words[i],word); printf("\nword was %s and addr is %p and words[%d] is %s\n", word, word, i, words[i++]); word = strtok(NULL, "., ;:?"); // printf("\n ----after strtok, word is %p",word); } // printf("***done**"); printf("\nWords in the words array are: \n"); for (k = 0; k < i; k++) printf("\nwords[%d] is %s", k, words[k]); // using strings for getting numbers strcpy(tempwd, "42.7 16 99.99 33 H 7"); num = strtok(tempwd, " "); //scorelist for ints, stockprice for doubles, notstring for chars index1 = index2 = index3 = 0; i= 0; while (num != NULL) { if (strpbrk(num, "0123456789") != NULL) //looking for digits { if (strchr(num, '.') != NULL) //looking for decimal point { // found a float stockprice[index1++] = atof(num); } else { // found an int scorelist[index2++] = atoi(num); } } else // num has a letter in it { notstring[index3++] = num[0]; } // strcpy(___[i],num); // printf("\nword was %s and addr is %p and words[%d] is %s\n", word, word, i, words[i++]); num = strtok(NULL, " "); } /* printf("\nWords in the words array are: \n"); for (k = 0; k < i; k++) printf("\nwords[%d] is %s", k, words[k]); */ // strspan // strcspan // Before starting a search, you need to know the characteristics of your data // data type, size, unsorted or sorted, are duplicates allowed, valid range, etc. // linresult = linsearch(scorelist,BIGSEARCH,se ); /* if (linresult == -1) { printf("\n Sorry, Charlie. Search element not found. "); } else { printf("\n Search element %d found at index %d", se, linresult); } */ // Want to sort the data now before searching. What do we need to know? // data type, size, how to handle duplications when present, how to sort. // very common to sort small to large. /* printf("\n\nBefore bubble sort: "); for (i=0;i < BIGSEARCH; i++) { printf ("\n scorelist[%d] is %d", i, scorelist[i]); } */ //Bubble sort of array with n values //For n-1 passes do the following: for (i = 0; i < BIGSEARCH; i++) { //For every pair of adjacent values in array, for (j = 0; j <= (BIGSEARCH -2); j++) { // Compare the values and if smaller index value is larger than // larger index value, if (scorelist[j] > scorelist[j+1]) { // then swap the values // printf("\n\ttemp gets scorelist[%d] of %d",j, scorelist[j]); temp = scorelist[j]; // printf("\n\tscorelist[%d] gets scorelist[%d] of %d",j,j+1, scorelist[j+1]); scorelist[j] = scorelist[j+1]; // printf("\n\tscorelist[%d] gets temp of %d\n",j+1, temp); scorelist[j+1] = temp; } } // printf("\nEnd of pass %d\n",i); } // printf("\n\nAfter bubble sort: "); for (i=0;i < BIGSEARCH; i++) { // printf ("\n scorelist[%d] is %d", i, scorelist[i]); } /* */ //printf("You entered d= %d and e= %d. d+c*e where c= %d is %d\n",d,e,c,f); //c = howdyfunc(e, d); //printf("\nHello world %d\n Print a quote \" \n", howdyfunc(e,d) ); //quizfunc(c, f); return 0; } //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; } int howdyfunc(int arr[], int asz) { int c = -543; //printf("Howdy!! \n"); printf("\nbegin asz is %d",asz); for (c=0; c