explain the program below by placing comments within the text #include <stdio.h>
ID: 3632934 • Letter: E
Question
explain the program below by placing comments within the text#include <stdio.h>
int distance(int Arow, int Acolumn, int Brow, int Bcolumn);
int main()
{
FILE *input;
int cube[6][15];
int jrow,jcol,minr ,minc ,i,j,mindistance=10000,d;
input = fopen("input.dat","r");
if(input == NULL)
{ printf("Error opening input file ");
return 0;
}
printf("The cubicles-(0 -vacant, 1 - occupied, 2 - John ");
for(i=0;i<6;i++)
{for(j=0;j<15;j++)
{fscanf(input,"%d",&cube[i][j]);
if(cube[i][j]==2)
{jrow=i;
jcol=j;
}
printf("%d ",cube[i][j]);
}
printf(" ");
}
printf(" Johns cubicle is row %d, column %d ",jrow,jcol);
for(i=0;i<6;i++)
for(j=0;j<15;j++)
if(cube[i][j]==0)
{d=distance(jrow,jcol,i,j);
if(d<mindistance)
{minr=i;
minc=j;
mindistance=d;
}
}
printf("the cubical in row %d, column %d, should be chosen ",minr,minc);
fclose(input);
return 0;
}
int distance(int Arow, int Acolumn, int Brow, int Bcolumn) {
int dist = ((Arow-Brow)*(Arow -Brow)) + ((Acolumn - Bcolumn)*(Acolumn-Bcolumn));
return dist;
Explanation / Answer
please rate - thanks
message me if any questions
#include <stdio.h>
int distance(int Arow, int Acolumn, int Brow, int Bcolumn);
int main()
{
FILE *input;
int cube[6][15];
int jrow,jcol,minr ,minc ,i,j,mindistance=10000,d;
input = fopen("input.dat","r"); //open file for reading
if(input == NULL) //if file didn't open abort program
{ printf("Error opening input file ");
return 0;
}
printf("The cubicles-(0 -vacant, 1 - occupied, 2 - John ");
for(i=0;i<6;i++) //for 6 rows
{for(j=0;j<15;j++) //15 columns in each row
{fscanf(input,"%d",&cube[i][j]); //input the cubile infomation
if(cube[i][j]==2) //looking for john
{jrow=i; //save rows cubicle location
jcol=j; //in jrow and jcol
}
printf("%d ",cube[i][j]); //as reading in information, output it
}
printf(" "); //going to a new line, after each row completely read in
}
printf(" Johns cubicle is row %d, column %d ",jrow,jcol); //print where john is
for(i=0;i<6;i++) //for each cubile
for(j=0;j<15;j++)
if(cube[i][j]==0) //if it's vacant(only possible places to move to)
{d=distance(jrow,jcol,i,j); //caluculate the distance-using formula provided
if(d<mindistance) //if the distance is less then any previous minimum distance save it's
{minr=i; //location in minr and minc
minc=j;
mindistance=d; //and save it as the new minimumdistance
}
}
printf("the cubical in row %d, column %d, should be chosen ",minr,minc); //ouput cubile chosen
fclose(input);
return 0;
}
int distance(int Arow, int Acolumn, int Brow, int Bcolumn) {
int dist = ((Arow-Brow)*(Arow -Brow)) + ((Acolumn - Bcolumn)*(Acolumn-Bcolumn));
return dist;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.