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

PROGRAM 2: X Table Write a program to produce a number table as described here.

ID: 645310 • Letter: P

Question

PROGRAM 2: X Table
Write a program to produce a number table as described here. This table should be built from a single integer value provided by the user. The program will display a square 4x4 of various numbers, starting with the value provided by the user. The entered number should appear in an X like pattern across the table. Filling the rest of the square, each successive filler number printed will be one less than the filler number previously printed.

For example, the following output should be produced when the user inputs the starting value 15:

Gimme a starting value: 15
15 14 13 15
12 15 15 11
10 15 15 9
15 8 7 15

And here is the output that would be produced when the user inputs the starting value 10:

Gimme a starting value: 10
10 9 8 10
7 10 10 6   
5 10 10 4   
10 3 2 10

HINT: Don't worry too much about getting all the spacing beautifully correct. I'll be scoring the numbers printed, not the beauty of your table.

Explanation / Answer

// Program for X table

#include <stdio.h>
int main()
{
   int A[4][4];
   int x,duplicateX, i,j;
   printf("Enter X value to fill in 4x4 Matrix : ");
   scanf("%d",&x);
   duplicateX=x;
  
   //storing of matrix values with X and less than filler number before
  
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    {
   if(i==j)
   A[i][j]=x;
   else if(i+ j==3)
   A[i][j]=x;
   else
   A[i][j]=--duplicateX;
   }
   
   // Printing of matrix 4X4
   printf(" Gimme starting value : %d ",x);
   for(i=0;i<4;i++)
   {
   for(j=0;j<4;j++)
       printf(" %d",A[i][j]);
   printf(" ");
   }
   return 0;
}

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