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

// code begin. #include <stdio.h> // Lab 3 example int main() { /* Declare varia

ID: 3716596 • Letter: #

Question

// code begin.

#include <stdio.h>
// Lab 3 example
int main()
{
/* Declare variables */
char firstnames [4][15]; //Changed from 3 --> 4
char lastnames [4][20]; //Changed from 3 --> 4
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<4; i++) // This requires trusting there are 4 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   
}

//code ends

Can anyone tell me why this code won't compile?

Explanation / Answer

#include <stdio.h>
// Lab 3 example
int main()
{
/* Declare variables */
char firstnames [4][15]; //Changed from 3 --> 4
char lastnames [4][20]; //Changed from 3 --> 4
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<4; i++) // This requires trusting there are 4 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
}

//code ends

//There is no such function as float()... you need to cast sum into float by "float(sum)"