Convert For loops to While loops #include <stdio.h> #include <string.h> int main
ID: 3716885 • Letter: C
Question
Convert For loops to While loops
#include <stdio.h>
#include <string.h>
int main()
{
int total[4];
float adj_total[4];
float adj_avg[4];
int j;
char *firstName [4][15];//
char *lastName [4][20];
int scores[4][6];
float averages[4];
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
{
/*Determine Values*/
for (j=0; j<4; j++)
{
int i,maxScore,minScore,indexMax;
/*Prompt and read in data for one snow border */
printf(" Enter the snowboarders data in the format : firstname lastname score1 score2 score3 score4 score5 score6 %d.",j+1," ");
fscanf(fp,"%s %s", firstName[j], lastName[j]);
printf("%s %s ", firstName[j], lastName[j]);
for(i=0; i<6; i++)
{
fscanf(fp," %d",&scores [j][i]);
printf(" score: %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] ;
}
/* determine the minimum score*/
minScore=scores[j][0];
for(i=0; i<6; i++)
{
if(scores[j][i] < minScore)
minScore= scores[j][i] ;
}
/*sum up all 6 scores in an array*/
total[j] =0;
for(i=0; i<6; i++)
{
total[j]= total[j] +scores[j][i];
}
/* subtract max and min from sum*/
adj_total[j]= total[j]-maxScore-minScore;
/* compute the adjusted average of remaingn four scores*/
adj_avg[j]= adj_total[j]/4;
averages[j]= adj_avg[j];
}
// now finding the index of the highest adjusted average
int k = 0;
int m;
float max = adj_avg[k];
for (m = 0; m < 4; ++m)
{
if (adj_avg[m] > max)
{
max = (int)adj_avg[m];
k = m;
}
}
printf(" FirstName LastName Average ");
printf("--------- -------- ------- ");
printf("%s %s %-7.2f ",firstName[0],lastName[0],adj_avg[0]);
printf("%s %s %-7.2f ",firstName[1],lastName[1],adj_avg[1]);
printf("%s %s %-7.2f ",firstName[2],lastName[2],adj_avg[2]);
printf("%s %s %-7.2f ",firstName[3],lastName[3],adj_avg[3]);
printf(" Person %d is the winner ",k+1);// the person with highest adj average is the winner
printf("The winner is : %s %s with an average of %-7.2f ", firstName[k], lastName[k],adj_avg[k]);
return 0;
fclose(fp);
}
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main()
{
int total[4];
float adj_total[4];
float adj_avg[4];
int j;
char *firstName [4][15];//
char *lastName [4][20];
int scores[4][6];
float averages[4];
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
{
/*Determine Values*/
j = 0;
while (j<4)
{
int i,maxScore,minScore,indexMax;
/*Prompt and read in data for one snow border */
printf(" Enter the snowboarders data in the format : firstname lastname score1 score2 score3 score4 score5 score6 %d.",j+1," ");
fscanf(fp,"%s %s", firstName[j], lastName[j]);
printf("%s %s ", firstName[j], lastName[j]);
i = 0;
while(i<6)
{
fscanf(fp," %d",&scores [j][i]);
printf(" score: %d", scores[j][i]);
i++;
}
/*determine maximum score*/
maxScore=scores[j][0];
i = 0;
while(i<6)
{
if(scores[j][i] > maxScore)
maxScore= scores[j][i] ;
i++;
}
/* determine the minimum score*/
minScore=scores[j][0];
i=0;
while(i<6)
{
if(scores[j][i] < minScore)
minScore= scores[j][i] ;
i++;
}
/*sum up all 6 scores in an array*/
total[j] =0;
i=0;
while(i<6)
{
total[j]= total[j] +scores[j][i];
i++;
}
/* subtract max and min from sum*/
adj_total[j]= total[j]-maxScore-minScore;
/* compute the adjusted average of remaingn four scores*/
adj_avg[j]= adj_total[j]/4;
averages[j]= adj_avg[j];
j++;
}
// now finding the index of the highest adjusted average
int k = 0;
int m;
float max = adj_avg[k];
m = 0;
while (m < 4)
{
if (adj_avg[m] > max)
{
max = (int)adj_avg[m];
k = m;
}
++m;
}
printf(" FirstName LastName Average ");
printf("--------- -------- ------- ");
printf("%s %s %-7.2f ",firstName[0],lastName[0],adj_avg[0]);
printf("%s %s %-7.2f ",firstName[1],lastName[1],adj_avg[1]);
printf("%s %s %-7.2f ",firstName[2],lastName[2],adj_avg[2]);
printf("%s %s %-7.2f ",firstName[3],lastName[3],adj_avg[3]);
printf(" Person %d is the winner ",k+1);// the person with highest adj average is the winner
printf("The winner is : %s %s with an average of %-7.2f ", firstName[k], lastName[k],adj_avg[k]);
return 0;
fclose(fp);
}
}