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

Joan wants to choose a vacant cubical which is the closest one to her friend Joh

ID: 3632570 • Letter: J

Question

Joan wants to choose a vacant cubical which is the closest one to her friend John’s cubical. Below is an example cubical plan where a 0 designates an empty cubical, a 1 designates a cubical that is already taken by someone else and 2 designates john's cubical.
1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 1 1 1 0 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 0 1 1 1 1 1
1 1 0 1 1 1 1 1 2 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
Write a C program that finds the cubical that she desires. The cubical plan is stored in"/input.dat"; it contains 6 lines of integers (0, 1 or 2) and each line contains 15 integers separated by spaces. In case of multiple options, it is enough to print any one. In order to calculate the distance between cubical A and cubical B, you should use the function named distance as below:
int distance(int Arow, int Acolumn, int Brow, int Bcolumn) {
int dist = ((Arrow-Brow)*(Arow -Brow)) + ((Acolumn - Bcolumn)*(Acolumn-Bcolumn));
return dis;
}
your output should be like
the cubical in row 2, column 5, should be chosen

Explanation / Answer

please rate - thanks

you never specified what to start at. I actually forced it to start at 1, based on your incorrect post

now starts at 0

#include <stdio.h>
#include <conio.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 ");
   getch();
    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);
getch();
return 0;      
}
int distance(int Arow, int Acolumn, int Brow, int Bcolumn) {
int dist = ((Arow-Brow)*(Arow -Brow)) + ((Acolumn - Bcolumn)*(Acolumn-Bcolumn));
return dist;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote