In: Computer Science
Write a program called distance_square.c that reads an integer n from standard input, and prints an nxn pattern of integers. Each integer is the minimum number of steps required to reach the centre of the square. Steps can only be up, down, left or right (no diagonal movement). the question should be allowed to use only while loop
4 3 2 3 4 3 2 1 2 3 2 1 0 1 2 3 2 1 2 3 4 3 2 3 4
Observing the example above, each integer represents the minimum number of steps required to reach the centre of the square. For example, the top left corner contains the integer 4. The centre of the square can be reached in 4 steps (right, right, down, down).
You can assume n is odd and >= 3.
Make your program match the examples below exactly.
This exercise is designed to give you practice with while loops, if statements and some mathematical operators. Do not use arrays for this exercise!
Note: you are not permitted to use an array in this exercise. and you are suppose to use while loop only!!
./distance_square Enter square size: 3 2 1 2 1 0 1 2 1 2 ./distance_square Enter square size: 9 8 7 6 5 4 5 6 7 8 7 6 5 4 3 4 5 6 7 6 5 4 3 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 3 4 5 6 7 6 5 4 3 4 5 6 7 8 7 6 5 4 5 6 7 8 ./distance_square Enter square size: 15 14 13 12 11 10 9 8 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 8 9 10 11 12 13 14
to solve this we take two variable for handling cell value (x,y) means value at x,y position
as
k=n-1 and l=k and i as row variable and j as column variable
now for every row we start the print from l and decrease the value of l by one untill column variable j < n/2 after that we will increase it by one
after each row we update the k as
decrease the value of k by one untill row variable i< n/2 after that we will increase it by one
following is the pattern loop
while(i<n) // loop for rows
{
l=k;
j=0;
while(j<n) //
Loop for column
{
if(j>=n/2)
{
printf("%d ",l);
l++;
}
else
{
printf("%d ",l);
l--;
}
j++;
}
printf("\n");
if(i>=n/2)
{
k++;
}
else
{
k--;
}
i++;
}
complete program is as follows
#include<stdio.h> // Header file for standard input and output
int main()
{
int n,i=0,j;
printf("Enter a No.");
scanf("%d",&n);
int k=n-1,l;
if(n%2) // check input is odd or even
{
while(i<n) // loop for rows
{
l=k;
j=0;
while(j<n) // Loop for column
{
if(j>=n/2)
{
printf("%3d ",l); // here %3d is use for formating
l++;
}
else
{
printf("%3d ",l);
l--;
}
j++;
}
printf("\n");
if(i>=n/2)
{
k++;
}
else
{
k--;
}
i++;
}
}
else
printf("Value Must be a Odd Value"); // Error message if value is even
return 0;
}
screen shot
output 1:
Output 2
Output 3
Output 4