#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 #define AVOWEL 'a' //int z = 8; //A global variable will get a 0 on your lab in Dr T's class struct UTID { char *last; char * first; long IDnum; int day, month, year; }; 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, sta; 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; /* while ((linresult % 2) == 1) { linresult += 3; } */ sta = 'a'; printf("\nPlease enter a vowel: "); scanf("%c", &sti); switch(sti) { case 'A': case 'a': printf("\nAAaaaugh!!\n"); se = 1; break; case 'E': case 'e': printf("\nEEEEeeeeeek!\n"); se = 2; break; case 'I': case 'i': printf("\nAye matey!\n"); se = 3; break; case 'O': case 'o': case '0': printf("\nOooh"); printf(" Oh Boy!\n"); break; case 'U': case 'u': printf("\nUp up and away\n"); break; case 'Y': case 'y': printf("\nYo ho ho and a bottle of rum\n"); break; default: printf("\nYou didn't enter a vowel, matey\n"); } // now use se // array[se] gets something k = 15; i =42; while ((k < 25) && ((i % 5) == 0)) { i--; k = k +2; if !(i % 5) { printf("\ni is %d and k is %d", i, k); } } /* 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); // strcat 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); 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 J K -5 3453.8 -.45"); printf("\nNew tempwd with numbers and chars: %s", tempwd); 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); printf("\ndouble %lf\n", stockprice[index1-1]); } else { // found an int scorelist[index2++] = (int) strtol(num, &tempwd, 0); printf("\nInt %d\n", scorelist[index2-1]); } } else // num has a letter in it { notstring[index3++] = num[0]; printf("\nchar %c\n", notstring[index3-1]); } num = strtok(NULL, " "); } // strspan // strcspan */ 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; }