Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with my binary search
    text
    copied!<p>I have described my program with comments. But i only have one issue of this program. I have tested with a lot of data. The only problem with my program is my displayContent function is not computer right. My output works until the program asks to enter the name of the person i want to search. When i enter in this, the program ends when it should keep going and it does output the info from my displayContent function. I feel that it may be a problem with my binary search in the function. But im not sure where my mistake is. I dont receive any errors the program breaks out. at this time.</p> <pre><code> /* M. Elliot Frost CMPSC 121 Sect 901 This program mimics the scores for a "Star Search" or other competion where 5 judges give a contestant a score, the score is totaled after dropping the maximum and minimum score. The program will employ 4 functions: one to get the score from a specific judge for a specific contestant (this will be called once for each judge), one the calculates the average score after dropping the highest and lowest scores, one that determines the maximum judges score for a specific contestant, and one that determines the mnimum score for a specific contestant. input: contestant's name, 5 Judges' names, 5 judges' scores ouput: contestant's name, scores from the five judges, and final score processing: enter the contestant name, enter the five judges' names, call a function multiple times to input a judge's score for specific contestant, call a function to calculate the final score. test data:/*contestant names = Shayna Skahan, Kelsey Himes, Kyle Strotz, David Cycon, Kristin Karg, Brad Lefever, Christy Collins, John Leonard, Puppy Chow scores for shayna=1,2,3,4,5 scoreforkelsey=1.2,2.2,3.2,4.2,5.2 scoreforKyle=9,8,7,6,5 scoreforDavid=4.5,3.4,2.3,1.2,5.1 scoreforKristin=1.234,2.345,3.456,4.567,5.678 scoreforBrad=1,3,5,7,9 scoreforChrist=2,4,6,8,1.1 scoreforJohnLeonard=3,3.4,5,5.6,7.85 scoreforPuppyChow=1.45,2.45,3.45,4.45,5.45 judge names (respective to scores)=1)Ryan Nolt 2)Griff Galante 3)Brett Graeff 4)Dan Lee 5)Elliot Frost */ #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;cctype&gt; #include &lt;iomanip&gt; using namespace std; struct info { string name; double score[5]; double avg; }; //function prototypes void getJudgeData(double&amp; , string , string ); double calcScore(double, double, double, double, double); double findLowest( double, double, double, double, double); double findHighest( double, double, double, double, double); void printScores(info[], int); void sortAscenduno(info[], int); void sortAscenddue(info[], int); void sortDescenduno(info[], int); void sortDescenddue(info[], int); void displayContent(info[], int, string); int main() { info contestant[25]; string judge[5]; char again = 'y'; char sort, sortby, howsort, search; int count=0; string name; //Loop control structure (for loop to enter judges names) for (int i = 0; i &lt; 5; i++) { cout&lt;&lt;"Please enter the "&lt;&lt; i + 1 &lt;&lt;" judge's first and last name. "; getline(cin,judge[i]); }//end loop to enter judges names //loop control structure(for loop to enter contestants names and scores) while(count&lt;25 &amp;&amp; again =='y') { cout&lt;&lt;"\nPlease enter the contestant's first and last name. "; getline(cin,contestant[count].name); cout&lt;&lt;endl; //Loop control structure(used for loop to call function(s) 5 times) for(int j=0;j&lt;5;j++) { getJudgeData(contestant[count].score[j], contestant[count].name, judge[j]); } //call function to calculate average contestant[count].avg = calcScore(contestant[count].score[0], contestant[count].score[1], contestant[count].score[2],contestant[count].score[3], contestant[count].score[4]); //output current contestant's scores cout&lt;&lt;"\n\nThe scores for contestant "&lt;&lt;contestant[count].name&lt;&lt;" were \n" &lt;&lt;contestant[count].score[0]&lt;&lt;" from "&lt;&lt;judge[0]&lt;&lt;endl&lt;&lt;contestant[count].score[1]&lt;&lt;" from " &lt;&lt;judge[1]&lt;&lt;endl&lt;&lt;contestant[count].score[2]&lt;&lt;" from "&lt;&lt;judge[2]&lt;&lt;endl&lt;&lt;contestant[count].score[3] &lt;&lt;" from "&lt;&lt;judge[3]&lt;&lt;" and "&lt;&lt;endl&lt;&lt;contestant[count].score[4]&lt;&lt;" from " &lt;&lt;judge[4]&lt;&lt;endl&lt;&lt;"\nThe average was "&lt;&lt;setprecision(1)&lt;&lt;fixed&lt;&lt;contestant[count].avg&lt;&lt;endl; cout&lt;&lt;"\n\nIs there another contestant (y/n)? "; cin&gt;&gt;again; cin.ignore(); //removes newline from input stream cout&lt;&lt;endl; //just to some space between contestants count+=1;//compound assignment operator }// end of loop to enter contestants names and scores cout&lt;&lt;"There were "&lt;&lt;count&lt;&lt;" contestants in this round."&lt;&lt;endl; //call function to print all scores printScores(contestant, count); cout&lt;&lt;"Would you like to sort these scores? "; cin &gt;&gt;sort; sort = toupper(sort);//to give back an uppercase letter so the user can enter a lower or upper case while (sort == 'Y') { cout&lt;&lt;"Please enter a: \n\tN to sort by names of the contesants or \n\tS to sort by the average scores"&lt;&lt;endl; cin&gt;&gt;sortby; sortby = toupper(sortby); //While loop to check validity of sortby variable while(sortby != 'N' &amp;&amp; sortby != 'S') { cout&lt;&lt;"You entered an invalid choice"&lt;&lt;endl; cout&lt;&lt;"Please enter a: \n\tN to sort by names of the contesants \n\tS to sort by the average scores \n\t or C to display the information of a single contestant."&lt;&lt;endl; cin&gt;&gt;sortby; sortby = toupper(sortby);//sends upper case letter to sort by }//end check of sortby loop switch(sortby)//switch for score to sort by { case 'N': do //sorting contestants by name { cout&lt;&lt;"Would you like to sort ascending (a) or descending (d) \nby the contestants' names? "; cin&gt;&gt;howsort; howsort = tolower(howsort);//to give back an lowercase letter so the user can enter a lower or upper case }while (howsort != 'a' &amp;&amp; howsort != 'd'); // end howsort loop if (howsort == 'a') { sortAscenduno(contestant, count); cout&lt;&lt;"\nAfter sorting ascending according to contestants' names"&lt;&lt;endl;//callfunction to sort by ascending name printScores(contestant, count);//call function to print structure array }//end if else { sortDescenduno(contestant, count);//callfunction to sort by descending name cout&lt;&lt;"\nAfter sorting descending according to contestants' names"&lt;&lt;endl; printScores(contestant, count);//call function to print structure array }//end else cout&lt;&lt;"Would you like to search for a single contestant?"; cin&gt;&gt;search; if(search=='Y' || 'y') { cout&lt;&lt;"Please enter the name of the person which you would like to search. "; getline(cin, name); cin.ignore(); displayContent(contestant, count, name); } break;//not to exit a loop but the switch case 'S': do {//sorting by avg score from contestants { cout&lt;&lt;"Would you like to sort ascending (a) or descending (d) \nby the contestants' average scores? "; cin&gt;&gt;howsort; howsort = tolower(howsort); }while (howsort != 'a' &amp;&amp; howsort != 'd'); // end howsort loop if (howsort == 'a')//if loop to sort ascending to average score { sortAscenddue(contestant, count); cout&lt;&lt;"\nAfter sorting ascending according to contestants' average score"&lt;&lt;endl; printScores(contestant, count); }//end if else { sortDescenddue(contestant, count); cout&lt;&lt;"\nAfter sorting descending according to contestants' average score"&lt;&lt;endl; printScores(contestant, count); }//end else cout&lt;&lt;"Would you like to search for a single contestant?"; cin&gt;&gt;search; if(search=='Y' || 'y') { cout&lt;&lt;"Please enter the name of the person which you would like to search. "; getline(cin, name); cin.ignore(); displayContent(contestant, count, name); } break;//not to exit a loop but to exit the switch }//end switch cout&lt;&lt;"Would you like to sort these scores again? "; cin &gt;&gt;sort; sort = toupper(sort); } return 0; } /*function to enter a single judge's score for a contestant. input: score as a reference parameter, contestant name and judge's name as value parameters. output: the score will be passed back to the function call by the reference parameter. processing: ask for the score and validate that it is between 0 and 10 using a while loop. Pre: Contestant's and Judges' names have been entered. Post: score for a specific contestant by a specific judge will be sent back to call. */ void getJudgeData(double&amp; value, string contname, string judname) { cout&lt;&lt;"Please enter the score for "&lt;&lt;contname&lt;&lt;" from "&lt;&lt;judname&lt;&lt;". "; cin&gt;&gt;value; //to validate the score and asks the user to reenter while (value &lt; 0 || value &gt; 10) { cout&lt;&lt;"An invalid score was entered. \nPlease enter the score for " &lt;&lt;contname&lt;&lt;" from "&lt;&lt;judname&lt;&lt;" again. "; cin&gt;&gt;value; } } /*Function to calculate the average score after eliminating the the highest and lowest scores. input: five scores, passed by value output: average will be displyed to screen processing: add five scores together call function to determine highest score and subtract that. call function to lowest score and subtract divide by 3 (because there were 5 scores but minus the lowest and highest there are only three doubles at this point) Pre: five valid scores have been determined Post: Average will be output to the screen */ double calcScore(double sc1, double sc2, double sc3, double sc4, double sc5) { double small, big, mean; //call function to find lowest small = findLowest(sc1, sc2, sc3, sc4, sc5); //call function to find highest big = findHighest(sc1, sc2, sc3, sc4, sc5); mean = (sc1 + sc2 + sc3+ sc4 + sc5 - small - big)/3; cout&lt;&lt;"\nThe average score was "&lt;&lt;setprecision(1)&lt;&lt;fixed&lt;&lt;mean&lt;&lt;endl; return mean; } /*function to determine the lowest value of the 5 scores input: five scores passes by value output: the lowest score will be sent back to call by a return statement processing: low to the first score initially. Then use a series of ifs to compare low to other scores Pre: That five valid scores have been enterd Post: The lowest socre is returned to function call */ double findLowest( double one, double two, double three, double four, double five) { double low; low = one; //compare to second score if (two &lt; low) { low = two; } //compare to third score if (three &lt; low) { low = three; } //compare to fourth score if (four &lt; low) { low = four; } //compare to fifth score if (five &lt; low) { low = five; } cout&lt;&lt;"\nThe lowest of the five scores was "&lt;&lt;low&lt;&lt;endl; //for testing return low; } /*function to determine the highest value of the 5 scores input: five scores passes by value output: the highest score will be sent back to call by a return statement processing: high to the first score initially. Then use a series of ifs to compare low to other scores Pre: That five valid scores have been enterd Post: The highest socre is returned to function call */ double findHighest( double one, double two, double three, double four, double five) { double high; high = one; //compare to second score if (two &gt; high ) { high = two; } //compare to third score if (three &gt; high) { high = two; } //compare to fourth score if (four &gt; high) { high = four; } //compare to fifth score if (five &gt; high) { high = five; } cout&lt;&lt;"\nThe highest of the five scores was "&lt;&lt;high&lt;&lt;endl; //for testing return high; } /*function to output all the scores for all the contestants for this round. input: taken from the main function is an array of structure data type and the number of contestants output: a title line containing judges names, then series of contestants names and score Pre: Contestants names and scores have been entered, average score has been calculated Post: A table appears to screen */ void printScores(info player[], int mag) { cout&lt;&lt;"The scores for the contestants are \n Contestant Judge1 Judge2 Judge3 Judge4 Judge5 Average" &lt;&lt;"\n______________________________________________________________________"&lt;&lt;endl; //loop to output contest name and scores for(int q = 0; q &lt; mag; q++) { cout&lt;&lt;right&lt;&lt;setw(15)&lt;&lt;fixed&lt;&lt;setprecision(1)&lt;&lt;player[q].name&lt;&lt;setw(10)&lt;&lt;player[q].score[0]&lt;&lt;setw(8)&lt;&lt;player[q].score[1]&lt;&lt;setw(8)&lt;&lt;player[q].score[2]&lt;&lt;setw(8)&lt;&lt;player[q].score[3] &lt;&lt;setw(8)&lt;&lt;player[q].score[4]&lt;&lt;setw(8)&lt;&lt;player[q].avg&lt;&lt;endl; } } /*function to sort arrays ascending in average score input: array of structure data type, and number of contestants output: no output generated, but array of structure data type will be sorted by ascending alphatbetically Processing: The bubble sort algorithm will be used Pre: scores will be entered and averages calculated Post: structure array will be rearranged */ void sortAscenduno(info contender[], int n) { int last = n-2, i; bool sawt = false; info temp; while (!sawt) { i = 0; sawt = true; while (i &lt;= last) { int compare=contender[i].name.compare(contender[i+1].name); if (compare &gt; 0) { temp=contender[i]; contender[i]=contender[i+1]; contender[i+1]=temp; sawt = false; } i++; } last = last -1; } } /*function to sort arrays descending by name input: array of structure data type, and number of contestants output: no output generated, but array of structure data type will be sorted by ascending alphatbetically Processing: The insertion sort algorithm will be used Pre: scores will be entered and averages calculated Post: structure array will be rearranged */ void sortAscenddue(info participators[], int mult) { int last=mult-2, i; int marker; bool sorted=true; // Insertion sort for (i = 0; i&lt;= last; i++) { if (participators[i].avg &gt; participators[i + 1].avg) { sorted = false; marker = i; info temp; while (!sorted) // loop to place element in preceding list { temp = participators[marker]; //swapping elements of array participators[marker] = participators[marker + 1]; participators[marker + 1] = temp; if (marker == 0) //at beginning of array { sorted = true; } else if (participators[marker].avg &gt; participators[marker - 1].avg) //Check to see if in order { sorted = true; } else { marker = marker - 1; //reset marker } cout&lt;&lt;endl; } } } } /*function to sort arrays descending to contestants' names input: array of structure data type, number of contestants output: Arrays will be changed with relationship of indices maintained Processing: The selection sort algorithm will be used Pre: scores will be entered and averages calculated Post: arrays will be rearranged */ void sortDescenduno(info attend[], int numb) { int outer, inner, maxi; info temp; int cmp; //nested loops for selection sort for(outer = 0; outer &lt;numb-1; outer++) { maxi = outer; for(inner = outer+1; inner &lt; numb; inner ++) { cmp=attend[outer].name.compare(attend[inner].name); if(cmp&lt;0) maxi = inner; } if (maxi != outer) { temp=attend[outer]; attend[outer]=attend[maxi]; attend[maxi]=temp; } } } /*function to sort arrays descending to contestants' average scores input: array of structure data type, number of contestants output: Arrays will be changed with relationship of indices maintained Processing: The selection sort algorithm will be used Pre: scores will be entered and averages calculated Post: arrays will be rearranged */ void sortDescenddue(info people[], int amount) { int second, first, larger; info temp; //nested loop for selection sort for(second = 0; second &lt;amount-1; second++) { larger = second; for(first = second+1; first &lt; amount; first ++) { if(people[second].avg &lt; people[first].avg) larger = first; } if (larger != second) { temp=people[second]; people[second]=people[larger]; people[larger]=temp; } } } /*Function will be able to let user search for any contestant and will print all of their information input:taken from main is the array of structure data type, the number of contestants, and a string variable output:printed is one contestant from the structure data type processing: binary search is done pre:elements of the structure data type array are entered post: one elements will be output to the screen */ void displayContent(info contest[], int quantity, string id) { int first=0, last=quantity-1, middle, position=-1; bool found=false; while(first&lt;=last &amp;&amp; !found) { middle=(first+last)/2; int look=contest[middle].name.compare(id); if(look==0) { found=true; position=middle; } else if(look&gt;0) { last=middle-1; } else { first=middle+1; } } if(position==-1) { cout&lt;&lt;"That person was not one of the contestants."; } else { cout&lt;&lt;"The scores for "&lt;&lt;id&lt;&lt;" are \n Contestant Judge1 Judge2 Judge3 Judge4 Judge5 Average" &lt;&lt;"\n______________________________________________________________________"&lt;&lt;endl; cout&lt;&lt;right&lt;&lt;setw(15)&lt;&lt;fixed&lt;&lt;setprecision(1) &lt;&lt;contest[position].name&lt;&lt;setw(10)&lt;&lt;contest[position].score[0]&lt;&lt;setw(8) &lt;&lt;contest[position].score[1]&lt;&lt;setw(8)&lt;&lt;contest[position].score[2]&lt;&lt;setw(8) &lt;&lt;contest[position].score[3] &lt;&lt;setw(8)&lt;&lt;contest[position].score[4]&lt;&lt;setw(8) &lt;&lt;contest[position].avg&lt;&lt;endl; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload