Programming in C Write a program token c that tracks a token

Programming in C

Write a program token. c that tracks a token moving sequentially along 15 horizontal cells while skipping occupied ones. The program asks for the locations of two occupied cells and for the initial token location. The three locations are distinct integers between 1 and 15, inclusive. The token then starts at the specified location, and at each step moves one cell to the right, and if that cell is occupied, it skips to the next cell, until it ends at the rightmost unoccupied cell. Print the whole array of cells, at each step, representing the occupied cells by X. and the token by 0. If any of the three input locations is not between 1 and 15, or if any two are the same, print \"Invalid input!\" and terminate the program.

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

#include <stdio.h>

int main()
{
int arr[15];
int a,b,c;
printf(\"Enter Two occupied cells: \ \"); //read two occupied cells
scanf(\"%d\",&a);
scanf(\"%d\",&b);
printf(\"Enter token starting position: \ \"); //read starting position
scanf(\"%d\",&c);
  
if(a<1 || a>15 || b<1 || b>15 || c<1 || c>15){ //if any of input is not in the range 1-15, then print invalid and exit
printf(\"Invalid input!\");
return 0;
}
  
if(a==b || b==c || a==c){ //if any of the 2 inputs are equal, then print invalid and exit
printf(\"Invalid input!\");
return 0;
}

for(int i=0; i<15; i++) //initialize the array with 0
arr[i] = 0;
  
arr[a-1] = 1; //set the position of a and b in array
arr[b-1] = 1;
  
int last=15;
for(last=15; last>0; last--){ //find the last free position in array, so that iteration ends when toeken reaches last
if(arr[last-1]==0)
break;
}
  
printf(\"\ \ \");
for(int i=c; ; i++){ //iterate till token reaches last
if(i==(a)||i==(b)){ //to skip the line after a jump (if previous arr position is occupied by either a or b)
c++;
continue;
}
printf(\"|\");
for(int j=0; j<15; j++){ //to print 15 positions with position of x and 0
if((j+1)==a||(j+1)==b){
printf(\"x|\");
continue;   
}
  
if((j+1)==c)
printf(\"O\");
printf(\"|\");
}
  
if(c==last) //exit the loop if token\'s current position(c) reaches last
break;
c++;
printf(\"\ \");
}

return 0;
}

----------------------------------------------------

OUTPUT:

Enter Two occupied cells:   
5   
15
Enter token starting position:
1

  
|O||||x||||||||||x|   
||O|||x||||||||||x|   
|||O||x||||||||||x|   
||||O|x||||||||||x|   
|||||x|O|||||||||x|   
|||||x||O||||||||x|   
|||||x|||O|||||||x|   
|||||x||||O||||||x|   
|||||x|||||O|||||x|   
|||||x||||||O||||x|   
|||||x|||||||O|||x|   
|||||x||||||||O||x|   
|||||x|||||||||O|x|

Programming in C Write a program token. c that tracks a token moving sequentially along 15 horizontal cells while skipping occupied ones. The program asks for t
Programming in C Write a program token. c that tracks a token moving sequentially along 15 horizontal cells while skipping occupied ones. The program asks for t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site