This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg2.c
51 lines (41 loc) · 1.43 KB
/
avg2.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#define MAX_DIMENSION 10
int main()
{
int numberOfColums =0, numberOfRows =0;
printf("\nEnter number of rows: ");
scanf("%d",&numberOfRows);
printf("\nEnter number of columns: ");
scanf("%d",&numberOfColums);
if(numberOfColums != numberOfRows){
printf("\nError. The input must be a square matrix. ");
return -1;
}
int matrix[MAX_DIMENSION][MAX_DIMENSION];
printf("\nFill the array:\n");
for(int i= 0;i<numberOfRows;i++){
for(int a= 0;a<numberOfColums;a++){\
scanf("%d",&matrix[i][a]);
}
}
sum(matrix,numberOfRows,numberOfColums);
return 0;
}
void sum(int matrix1[][MAX_DIMENSION],int numberOfRows,int numberOfColums){
int sum2=0;
printf("\n The elements of the main diagonal are: ");
for(int i= 0;i<numberOfRows;i++){
for(int a= 0;a<numberOfColums;a++){
if(i==a){
printf("%d ",matrix1[i][a]);
sum2 = sum2+ matrix1[i][a];
}
}
}
printf("\n Their sum = %d",sum2);
}