#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'}; double stockprice[6]={0}; char stocktype[6]={'X','A'}; char sti; int se = 68; int linresult, temp; char words[6][40]={{0}}; char tempwd[100]; linresult = 100; printf("\nEnter a line from a nursery rhyme: "); temp = getline(tempwd, &linresult, stdin); x = 7; k=-1; // 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