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

Using the example programs provided on Blackboard in the folder named “Lab3”, ad

ID: 3718746 • Letter: U

Question

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.

This is my code. please help using C language.

#include
#include

//* Declaring Variables //*
int main()
{
int total[3];
int j;
char *firstName [3][3];//
char *lastName [3][3];
int scores[3][6];
float newtotal[3];
float newaverage[3];

//*Determine Values*//
for (j=0;j<3;j++){

int i,maxScore,minScore,indexMax;

//*Enter information of Olympic snow boarders *//
printf("enter firstName lastName and 6 scores of person: %d",j+(1));
scanf("%s %s", firstName[j], lastName[j]);
printf("the name was : %s %s ", firstName[j], lastName[j]);

//*Scan in Answers//*
for(i=0;i<6;i++){
scanf(" %d",&scores [j][i]);
printf("the scores entered was: %d ", scores[j][i]);
}

//*determine maximum score*//
maxScore=scores[j][0];
for(i=0; i<6;i++)
{
if(scores[j][i] > maxScore)
maxScore= scores[j][i] ;
}
printf("the max score is : %d ",maxScore);

//* determine the minimum score*//
minScore=scores[j][0];
for(i=0; i<6;i++)
{
if(scores[j][i] < minScore)
minScore= scores[j][i] ;
}
printf("the minimal score is : %d ",minScore);

//*Get all six scores*//

total[j] =0;
for(i=0;i<6;i++)
{
total[j]= total[j] +scores[j][i];
}

printf("the total is : %d ", total[j]);

//* subtract max and min and get new total*//
newtotal[j]= total[j]-maxScore-minScore;
printf("the new total is : %f ", newtotal[j]);

//* compute the new average from the new total *//
newaverage[j]= newtotal[j]/4;
printf("the new average is : %f ", newaverage[j]);
}

//* Declare the winner*//

int k = 0;
int m;
float max = newaverage[k];
for (m = 0; m < 3; ++m)
{
if (newaverage[m] > max)
{
max = (int)newaverage[m];
k = m;
}
}

printf("Person %d is the winner ",k+1);// the person with highest adj average is the winner
printf("The winner for the gold medal : %s %s ", firstName[k], lastName[k]);


return 0;
}

data.txt

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 : Please post a separate question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote