Question: Code for lab 2 shown below #include #include void bubbleSort(int arr[], int n){ int i, j, temp; for (i = 0; i < n -
Code for lab 2 shown below
#include
void bubbleSort(int arr[], int n){ int i, j, temp; for (i = 0; i < n - 1; i++){ for(j = 0; j < n - 1; j++){ if (arr[j] > arr[j + 1]){ temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
int main(void) { int loops = 3; //number of loops char firstnames[loops][20]; char lastnames[loops][20]; int scores[loops][6]; float totals[loops]; int i, j;
printf("Enter the snow boarders data in the format: firstname lastname score1 score2 score3 score4 score5 score6 "); for(i = 0; i < loops; i++){ //load all of the data printf("Please enter line number: %d ", i + 1); scanf("%s %s %d %d %d %d %d %d", firstnames[i], lastnames[i], &scores[i][0], &scores[i][1], &scores[i][2], &scores[i][3], &scores[i][4], &scores[i][5]); }
for(i = 0; i < loops; i++){ int n = sizeof(scores[i])/sizeof(scores[i][0]); //sort scores bubbleSort(scores[i], n); //determine the total totals[i] = (scores[i][1] + scores[i][2] + scores[i][3] + scores[i][4]) / (float)4; }
for(i = 0; i < loops; i++){ printf("Name: %s %s Total: %.2f ", firstnames[i], lastnames[i], totals[i]); }
float highestScore = 0; int highestScorer = 0; //the array index of the highest scorer
for(i = 0; i < loops; i++){ //if the score of this athlete is the highest we've seen... if(highestScore < totals[i]){ highestScorer = i; //make him the highest scorer highestScore = totals[i]; //and update the highest score } }
printf("The gold medal is awarded to %s %s with a score of %.2f", firstnames[highestScorer], lastnames[highestScorer], totals[highestScorer]);
return 0;
*Please provide screenshots and lab report*
Description:
The topic of this programming assignment is the same as Lab #2: determining the gold-medal winner of a halfpipe snowboarding competition, based on scores that are examples of actual data compiled and analyzed during the recent winter Olympic Games in South Korea. In this lab, you will be reusing and making minor modifications in the code you wrote for Lab #2.New requirement for this lab: The names of all snowboarders and scores earned for one run should be saved in a data file, rather than entered on the keyboard.
Instructions: Using the example programs provided on Blackboard in the folder named Lab3, add the necessary code to your program from Lab #2 in order to read the data for each snowboarder from a data file rather than the keyboard.
(Example 1) #include// Lab 3 example int main() { /* Declare variables */ char firstnames [3][15]; char lastnames [3][20]; int scores [6]; int i,j; int sum; float average; FILE *fp; // define pointer variable of type FILE /* Open data file associated with fp in read mode */ fp=fopen("data.txt", "r"); /* Check if open was successful */ if (fp == NULL) // Not successful { printf("Program cannot open the file "); return -1; // indicates a problem; if you have this, // no need for else below } else // strictly speaking not required, but helps mark place { for (i=0; i<3; i++) // This requires trusting there are 3 entries { /* Prompt and read in values */ printf("Enter fn ln and 6 scores. "); fscanf(fp,"%s %s ",&firstnames[i],&lastnames[i]); /* Echo print name */ printf("The name is: %s %s ",firstnames[i],lastnames[i]); sum = 0; /* Read in 6 scores */ for (j=0; j < 6; j++) { fscanf(fp,"%d", &scores[j]); /* Echo print each score read */ printf("The score is: %d ",scores[j]); sum = sum + scores[j]; } printf("The sum for this snowboarder is: %d ",sum); /* Find average of ALL 6 scores */ average = float(sum) / 6; printf("The average for this snowboarder is: %f ",average); } //end of for loop using i fclose(fp); } // end of else } //end of main
Use the provided file data.txt as the sample test case. This file has data for 4 snowboarders, in the same format as used for Lab #2.
Sean White 96 98 88 99 98 100 Chloe Kim 92 99 100 98 99 97 Ben Ferguson 90 95 91 85 94 88 St. Bernard 99 97 100 98 99 100
There will be three versions of your Lab #3, as described below. All three will use data from the input data file.
In the first version, keep your for-loop to read and process the data for each of the snowboarders, but read the data from the input data file rather than the keyboard. Test your program and capture a screenshot of the output. It should show that there is processing for only 3 snowboarders, despite the fact that the data file has data for 4. Insert your screenshot into the Testing--Screenshot of output window section in your Lab Report. Below the screenshot, explain the output results.
Second, revise your program (you can just comment out lines not needed and add new ones) so that it uses a while-loop to control the processing for each snowboarder rather than a for-loop. Refer to the example program provided, Version 2. There is just one while-loop to be added; do not replace internal for-loops that handle other tasks. Test your program and capture a screenshot of the result. Place the screenshot in your lab report and explain the output results.
Finally, revise your program again so that it uses a compound test for your while-loop, as shown in the example program, Version 3. This while loop checks if there is additional data left to read AND checks that there will not be an attempt to over-fill the array. Test your program and capture a screenshot of the result. Place the screenshot in your lab report and explain the output results.
***PLEASE COMMENT IF YOU NEED CLARIFICATION AND HELP I'M SO CONFUSED :((***
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
