① c语言 按行输入3行3列矩阵A的各元素,计算主对角线之和并输出
C语言程序如下:
#include<stdio.h>
int main()
{
int a[3][3],sum=0;
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
sum=sum+a[i][j];
printf("sum=%d ",sum);
return 0;
}
称为对角元素
因此,计算主对角线之和并输出的思路就是判断矩阵数组元素是否是主对角元素,如果数组元素的两个下标相等,则该数组元素为对角元素。
② B题 C语言编程 数组之方程对角线
/****** your code here ******/
int i,s=0;
for(i=0;i<n;i++)
s+=a[i][i];
return s;
/***** your code end *****/
③ c语言对角线
从左上角到右下角的对角线叫右下对角线,也叫主对角线。
从右上角到左下角的对角线叫左下对角线,也叫副对角线。
④ 请问怎么用C语言实现二维数组的对角线输出
for(i=0;i<n;i++)
printf("%d",a[i][i]);
⑤ 如何用c语言的循环嵌套输出有对角线的正方形
//JHTP Exercise 4.29:Square of Asterisks
//by [email protected]
/**(Square of Asterisks) Write an application that prompts the user to enter the size of the side
of a square, then displays a hollow square of that size made of asterisks. Your program should work
for squares of all side lengths between 1 and 20.*/
import java.util.Scanner;
public class SquareOfAsterisks {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请输入要输出的正方形大小(整数):");
int size=input.nextInt();
int number=1;
int sentinel=1;
while (sentinel<=size){
while (number<=size){
System.out.print("*");
number++;
}
System.out.print("\n");
number=1;
sentinel++;
}
}
}
⑥ c语言中如何输出二维数组a[5][5]的对角线元素
/*
*Date:2015/12/18
*/
#include<stdio.h>
#include<stdlib.h>
intmain(intargc,char*argv[])
{
inta[5][5]={
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}
};
inti;
for(i=0;i<5;i++){
printf("%d",a[i][i]);
}
printf(" ");
exit(EXIT_SUCCESS);
}
这是运行结果