[Functions]D. Liang 5.17 Displaying matrix of 0s and 1s.c

Description

Write a function that displays an n by n matrix. Each element in the matrix is 0 or 1. The elements of first row, the elements of the first column and the diagonal elements are 1s, the other are 0s.

Input

The first line is a positive integer (1 <= t <= 10), denoting the number of test cases followed.
In each test case, the input is one single line, containing a positive integer n (1 <= n <= 100) which denotes the width of matrix.

Output

For each test case, the output contains n line and each line consists of n integers separated by white spaces.
(There is a white spaces in front of the first integer in each line, no white spaces behind the last integer in each line.)

Sample Input

4
1
2
3
10

Sample Output

 1
 1 1
 1 1
 1 1 1
 1 1 0
 1 0 1
 1 1 1 1 1 1 1 1 1 1
 1 1 0 0 0 0 0 0 0 0
 1 0 1 0 0 0 0 0 0 0
 1 0 0 1 0 0 0 0 0 0
 1 0 0 0 1 0 0 0 0 0
 1 0 0 0 0 1 0 0 0 0
 1 0 0 0 0 0 1 0 0 0
 1 0 0 0 0 0 0 1 0 0
 1 0 0 0 0 0 0 0 1 0
 1 0 0 0 0 0 0 0 0 1

Analysis:

***According to the observation matrix, there are the following rules:

  1. All elements in the first row are 1;
  2. All elements in the first column are 1;
  3. All diagonal elements are 1;
  4. All other elements are 0 except the above situations;
  5. Branches i i i and columns j j j to discuss.

My code

//   Date:2020/4/3
//   Author:xiezhg5
#include <stdio.h>
int main(void)
{
	int m;
	scanf("%d",&m);
	while(m--)      //判断条件可还行 
	{
		int n,i,j;
		scanf("%d",&n);      //表明用户将要输入n个数 
        for(i=0;i<n;i++)    //i表示行 
        {
        	for(j=0;j<n;j++)  //嵌套for循环,j表示列 
        	{
        		if(i==j||i==0||j==0)   //矩阵规律 
        			printf(" 1");     //注意数字间要留空格 
        		else
        			printf(" 0");
            }
            printf("\n");
        }
	}
	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
An LCD (Liquid Crystal Display) is a type of display technology that uses liquid crystals to display information. LCDs are commonly used in electronic devices such as digital watches, calculators, and mobile phones, and can also be used in more complex systems such as embedded systems and microcontrollers. A matrix keypad, on the other hand, is a type of input device that allows users to input data through a combination of buttons arranged in a matrix pattern. A typical matrix keypad has rows and columns of buttons that can be pressed to input data. The arrangement of the buttons in a matrix pattern allows for a large number of buttons to be accommodated in a small space. To interface an LCD and a matrix keypad with a microcontroller, several specifications need to be considered. These include: 1. Pinout: The pinout of the LCD and matrix keypad needs to be understood in order to connect them to the microcontroller. The pinout typically includes pins for power, ground, data, and control signals. 2. Voltage levels: The voltage levels of the LCD and matrix keypad need to be compatible with the microcontroller. Most microcontrollers operate at 5V or 3.3V, so the LCD and matrix keypad should also operate at these voltage levels. 3. Data communication protocol: The data communication protocol between the microcontroller, LCD, and matrix keypad needs to be understood in order to transfer data between them. The most common data communication protocols used are SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit). 4. LCD commands: The LCD has a set of commands that can be used to control its behavior, such as clearing the screen, setting the cursor position, and displaying text. These commands need to be understood in order to control the LCD using the microcontroller. 5. Matrix keypad scanning: The matrix keypad needs to be scanned in order to detect which buttons are being pressed. This is typically done using a technique called matrix scanning, where the rows and columns of the keypad are scanned sequentially to detect button presses. Overall, interfacing an LCD and matrix keypad with a microcontroller requires a thorough understanding of their specifications and how they can be connected and controlled.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值