Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

nstructions: Using the example programs provided on Blackboard in the folder nam

ID: 3716406 • Letter: N

Question

nstructions:

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.

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.

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.

Explanation / Answer

Hi, here are the required steps before running this program.

- Create a file "data.txt" with following data

- Save it in the directory in which your program exists.

VERSION 1: please find the code below, it will work after creating the above data.txt file.

//---------------------------------CODE----------------------------------

#include <stdio.h>

#include <string.h>

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;

// define pointer variable of type FILE

FILE *fp;

   /* Open data file associated with fp in read mode */

/* here the please provide the path of the file - you can give the full path if

   * you are not sure about the directory of your text file*/

   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

   }

for(i = 0; i < loops; i++){

//load all of the data

printf("Please enter line number: %d ", i + 1);

fscanf(fp,"%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]);

fclose(fp);

return 0;

}

//------------------------------CODE ENDS--------------------------------

VERSION 2: with a while loop and removed comments

//----------------------------------CODE-------------------------------------

#include <stdio.h>

#include <string.h>

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;

// define pointer variable of type FILE

FILE *fp;

   /* Open data file associated with fp in read mode */

/* here the please provide the path of the file - you can give the full path if

   * you are not sure about the directory of your text file*/

   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

   }

   i=0;

while(i<loops){

//load all of the data

printf("Please enter line number: %d ", i + 1);

fscanf(fp,"%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]);

            i++;

}

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]);

fclose(fp);

return 0;

}

//------------------------------CODE ENDS--------------------------------

VERSION 3 : "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. " Need more clarity on this. I can not find the version 3 example which has a compound test.