Let \(A=\{a_{ij}\}_{1\leq i\leq 5, 1\leq j\leq 5}\) be a matrix defined by \(a_{ij}=\frac{1}{i+j}\). Write a program that creates such a matrix in a two-dimensional array and displays the values.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#define N 5
void main()
{
double A[N][N];
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[i][j]=1./(i+j+2);
for(i=0;i<N;i++) {
for(j=0;j<N;j++)
printf("%7.4lf",A[i][j]);
printf("\n");
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Keeping two stacks within a single linear array