int whereis (int P, int* Xval, int* Yval) // looks for P in the spiral. // If fo
ID: 3746951 • Letter: I
Question
int whereis (int P, int* Xval, int* Yval)
// looks for P in the spiral.
// If found it returns 1 and puts the co-ordinates in Xval and Yval
// if P is not found (perhaps because of some limits on the search) it
// returns 0.
int whatsat (int X, int Y)
// returns the integer found at co-ordinates X,Y in the spiral.
An example run of a completed program is given on the next page. Invalid commands are detected, an invalid parameter is interpreted as zero.Partial program is displayed after example run,example run as follows
Example program run
Command: bloop
invalid command bloop
Command: whereis bloop
0 is at 0,0
Command: whereis 3,4
3 is at 0,1
Command: whereis 3 4
3 is at 0,1
Command: whereis 7
7 is at 0,-1
Command: whereis 49
49 is at 4,-3
Command: whereis 32767
32767 is at 91,-84
Command: whereis 32768
32768 is at 91,-83
Command: whereis 8388608
8388608 is at -1448,-344
Command: whereis 8388609
8388609 is at -1448,-345
Command: whatsat 3
comma missing
Command: whatsat 3,bloop
27 is at 3,0
Command: whatsat bloop,3
33 is at 0,3
Command: whatsat -1448,-343
8388607 is at -1448,-343
Command: whatsat 2000,2000
15996000 is at 2000,2000
Command: whatsat 1,1
2 is at 1,1
Command: quit Goodbye
Partial program
// spiral of integers 0, 1, 2
// command line interface with user
#include
#include
#include
#define LINELIMIT 30
#define NUMLIMIT LINELIMIT-8
char *comline,*number,*xchars,*ychars, *commaloc;
size_t bufsize=LINELIMIT;
size_t numsize=NUMLIMIT;
int whereis (int P, int* Xval, int* Yval) // to be completed
{
{
if (P == 0) {
*Xval = 0;
*Yval = 0;
return 1;
}
else {
int i,k;
k=0;
while( 4*k*k + 2*k < P)
k++;
if(4*k*k + 2*k == P ) {
*Xval = -1*k;
*Yval = -1*k;
return 1;
}
// Along the x-axis there is a width of 2*k +1 while along y-axis there is a
// length of 2*k
k = k-1;
int diff = P - (4*k*k + 2*k);
// if the left most co-ordinate is -k,-k then total number of points in
// that rectangle is 8*k+5
// distance of 2k+1 right then 2*k+1 upwards, then 2k +2 leftwards
// then 2*k+1 downwards
if( diff < 2*k+1) {
*Xval = diff-k;
*Yval = -1*k;
}else if (diff > 2*k+1 && diff <= 4*k+2){
*Xval = k+1;
*Yval = diff - (2*k+1) - k;
} else if (diff > 4*k+2 && diff <= 6*k+4){
*Xval = k+1 - (diff - 4*k -2);
*Yval = k+1;
}else {
*Xval = -1*k -1;
*Yval = k+1 - (diff - 6*k-4);
}
return 1;
}
}
int whatsat (int X, int Y) // to be completed
{
}
int main()
{
int P, x, y, i, go_on=1, command,N, cindex;
comline=malloc(bufsize*sizeof(char));
number=malloc(numsize*sizeof(char));
xchars=malloc(numsize*sizeof(char));
ychars=malloc(numsize*sizeof(char));
while(go_on) { printf("Command: ");
memset(comline,' ',bufsize);
getline(&comline,&bufsize,stdin);
if (memcmp(comline,"quit",strlen("quit"))==0) go_on=0;
else if (memcmp(comline,"whereis",strlen("whereis"))==0)
{
strcpy(number,comline+strlen("whereis"));
N=atoi(number);
if (N<0) printf("Negative numbers not in spiral ");
else { i=whereis(N,&x,&y);
printf("%d is at %d,%d ",N,x,y);
}
}
else if (memcmp(comline,"whatsat",strlen("whatsat"))==0)
{
// find comma index = C
commaloc=strpbrk(comline,",");
if (commaloc==NULL) printf("comma missing ");
else
{
// overwrite comma with null byte
*commaloc=0;
// copy first number to xchars
strcpy(xchars,comline+strlen("whatsat"));
// copy second number to ychars
strcpy(ychars,commaloc+1);
// convert strings to numbers
x=atoi(xchars);
y=atoi(ychars);
// find contents of the spiral at x,y
i=whatsat(x,y);
if (i==-1) printf("co-ordinates out of range ");
else printf("%d is at %d,%d ",i,x,y); } } else printf("invalid command %s ",comline);
}
printf("Goodbye ");
}
10 15 14 (2.2) (0,2) (2.2) 10 (2,0) --"( (1,0) , (-10)(o.0) (0.0)Explanation / Answer
ANSWER:
//Submitting solution to Part 1 where is
int whereis (int P, int* Xval, int* Yval) // to be completed
{
if (P == 0) {
*Xval = 0;
*Yval = 0;
return 1;
}
else {
int i,k;
k=0;
while( 4*k*k + 2*k < P)
k++;
if(4*k*k + 2*k == P ) {
*Xval = -1*k;
*Yval = -1*k;
return 1;
}
// Along the x-axis there is a width of 2*k +1 while along y-axis there is a
// length of 2*k
k = k-1;
int diff = P - (4*k*k + 2*k);
// if the left most co-ordinate is -k,-k then total number of points in
// that rectangle is 8*k+5
// distance of 2k+1 right then 2*k+1 upwards, then 2k +2 leftwards
// then 2*k+1 downwards
if( diff < 2*k+1) {
*Xval = diff-k;
*Yval = -1*k;
}else if (diff > 2*k+1 && diff <= 4*k+2){
*Xval = k+1;
*Yval = diff - (2*k+1) - k;
} else if (diff > 4*k+2 && diff <= 6*k+4){
*Xval = k+1 - (diff - 4*k -2);
*Yval = k+1;
}else {
*Xval = -1*k -1;
*Yval = k+1 - (diff - 6*k-4);
}
return 1;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.