① 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);
}
這是運行結果