In: Computer Science
Problem Description:
In C write a program that assigns random integers between 1 and 20 to a 5 x 5 two-dimensional array then displays the array with average of each row in a table format and the total of all elements in the array. Also calculate the total of each diagonal, top left to bottom right and top right to bottom left. Display the largest of the diagonal’s totals.
Example of the array:
6 10 3 19 20
1 17 13 18 1
18 7 14 9 2
16 11 8 9 10
6 15 5 12 2
Sample output:
Avg.
6 10 3 19 20 ???
1 17 13 18 1 ???
18 7 14 9 2 ???
16 11 8 9 10 ???
6 15 5 12 2 ???
Total of element in array: ????
Total of diagonal, top left to bottom right: ????
Total of diagonal, top right to bottom left: ????
Top right to bottom left diagonal total have the largest value of ???
Requirement:
In this program, two constant variables have used, r for row and c for column, both of them have the value 5. rand() is used to generate the random value between 1 and 20 using the formula rand()%(upper+1-lower)+1, ie rand()%(20+1-1)+1. d1 and d2 are used to store the sum of diagoals and a one dimensional array b is used to store each row and is passed to avg_row function to find the average of each row. A function diag_comp is used to compare the total of both diagonals. The function ran_val is defined to generate random values and is called inside the loop to generate 25 values( 5row x 5 cols).
Program code and output screen is given for your reference.
/* Main function of the C program. */
#include <stdio.h>
#include <stdlib.h>
#define r 5//constant for rows
#define c 5//constat for cols
int ran_val();//for random values
float avg_row(int b[10]); //to calucate average of each row
void diag_comp(int x,int y);//to compare diagonal totals
int main()
{
int a[r][c];
int b[c];
int i,j,t=0,d1=0,d2=0;
float avg;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=ran_val();
}
}
printf("\t\t avg\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
t=t+a[i][j];
printf("%d ",a[i][j]);
b[j]=a[i][j];
}
avg=avg_row(b);//calling function to calculate average of each
row
printf(" %f",avg);
printf("\n");
}
printf(" \nTotal of elements in array : %d \n ",t);
//calculating total of diagonal from top left to bottom right
for(i=0;i<r;i++)
{
d1=d1+a[i][i];
}
printf("\nTotal of diagonal, top left to bottom right:
%d\n",d1);
// calculating total of daiagonal from top right to bottom
left:
for(i=0,j=c-1;i<r;i++,j--)
{
d2=d2+a[i][j];
}
printf("\nTotoal of diagonal,top right to bottom left: %d
\n",d2);
diag_comp(d1,d2);//calling the function to compare diagonal
totals.
return 0;
}
int ran_val()//function for generating random values
{
int a;
a=rand()%(20+1-1)+1;//generating random values between 1 and
20
return a;
}
float avg_row(int b[10])
{
int sum=0,i,k;
float avg;
for (i=0;i<c;i++)
{
sum=sum+b[i];
}
avg=(float)sum/c;
return avg;
}
void diag_comp(int x, int y)//comparing diagonal totals
{
if(x>y)
{
printf("\nTop left to bottom right diagonal total have the largest
value of : %d ", x );
}
else
{
printf("\nTop right to bottom left diagonal total have the largest
value of : %d \n", y );
}
}
/* Main function of the C program. */
#include <stdio.h>
#include <stdlib.h>
#define r 5//constant for rows
#define c 5//constat for cols
int ran_val();//for random values
float avg_row(int b[10]); //to calucate average of each row
void diag_comp(int x,int y);//to compare diagonal totals
int main()
{
int a[r][c];
int b[c];
int i,j,t=0,d1=0,d2=0;
float avg;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=ran_val();
}
}
printf("\t\t avg\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
t=t+a[i][j];
printf("%d ",a[i][j]);
b[j]=a[i][j];
}
avg=avg_row(b);//calling function to calculate average of each row
printf(" %f",avg);
printf("\n");
}
printf(" \nTotal of elements in array : %d \n ",t);
//calculating total of diagonal from top left to bottom right
for(i=0;i<r;i++)
{
d1=d1+a[i][i];
}
printf("\nTotal of diagonal, top left to bottom right: %d\n",d1);
// calculating total of daiagonal from top right to bottom left:
for(i=0,j=c-1;i<r;i++,j--)
{
d2=d2+a[i][j];
}
printf("\nTotoal of diagonal,top right to bottom left: %d \n",d2);
diag_comp(d1,d2);//calling the function to compare diagonal totals.
return 0;
}
int ran_val()//function for generating random values
{
int a;
a=rand()%(20+1-1)+1;//generating random values between 1 and 20
return a;
}
float avg_row(int b[10])
{
int sum=0,i,k;
float avg;
for (i=0;i<c;i++)
{
sum=sum+b[i];
}
avg=(float)sum/c;
return avg;
}
void diag_comp(int x, int y)//comparing diagonal totals
{
if(x>y)
{
printf("\nTop left to bottom right diagonal total have the largest value of : %d ", x );
}
else
{
printf("\nTop right to bottom left diagonal total have the largest value of : %d \n", y );
}
}
OUTPUT SCREEN
Function call diagram