Question

In: Computer Science

Write a program called distance_square.c that reads an integer n from standard input, and prints an...

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 

Solutions

Expert Solution

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


Related Solutions

Write a program called x.c that reads an integer n from standard input, and prints an...
Write a program called x.c that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X". You can assume n is odd and >= 5. Solve this problem using only while loop. Solution: ./x Enter size: 5 *---* -*-*- --*-- -*-*- *---* ./x Enter size: 9 *-------* -*-----*- --*---*-- ---*-*--- ----*---- ---*-*--- --*---*-- -*-----*- *-------* ./x Enter size: 15 *-------------* -*-----------*- --*---------*-- ---*-------*--- ----*-----*---- -----*---*----- ------*-*------ -------*------- ------*-*------...
Write a short main program that reads an integer n from standard input and prints (to...
Write a short main program that reads an integer n from standard input and prints (to standard output) n lines of * characters. The number of *’s per line should double each time, starting with 1. E.g., if n = 5, the output should be as follows: * ** **** ******** ****************
Write a program that reads a positive integer n , prints all sums from 1 to...
Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers from 1 to 4 is 10;      ...
in.java Write a program that reads an integer from the user and prints a rectangle of...
in.java Write a program that reads an integer from the user and prints a rectangle of starts of width 5 3 and height N. Sample run 1: Enter N: 5 *** *** *** *** *** Bye Sample run 2: Enter N: 8 *** *** *** *** *** *** *** *** Bye Sample run 3: Enter N: 2 *** *** Bye Sample run 4: Enter N: -2 Bye
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
Write a program that prints the question “Do you wish to continue?” and reads the input....
Write a program that prints the question “Do you wish to continue?” and reads the input. If the user input is “Y”, “Yes”, “YES”, then print out “Continuing”. If the user input is “N” or “No”, “NO” then print out “Quit”. Otherwise, print “Bad Input”. Use logical operators. c++
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a Python program that reads an integer and prints how many digits the number has,...
Write a Python program that reads an integer and prints how many digits the number has, by checking whether the number is ≥10,≥100,≥1000, and so on (up to 1,000,000). Your program should also identify if a number is negative.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT