Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
4
Indexable
#include <stdio.h>

int main() {
   float a[10][10], b[10], x[10], y[10];
   int n, q = 0, i = 0, j = 0;

   printf("Enter number of equations : ");
   scanf("%d", &n);

   printf("Enter coefficient matrix\n");
   for (i = 0; i < n; i++) {
      printf("Enter %dth row\n", i);
      for (j = 0; j < n; j++) {
         printf("a[%d, %d ] = ", i, j);
         scanf("%f", &a[i][j]);
      }
   }

   printf("\nEnter values to the right side of equation\n");
   for (i = 0; i < n; i++) {
      printf("b[%d] = ", i);
      scanf("%f", &b[i]);
   }

   printf("Entered equations are:\n");
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
         printf("%.2f\t", a[i][j]);
      }
      printf(": %.2f\n", b[i]);
   }

   printf("Enter initial values of x\n");
   for (i = 0; i < n; i++) {
      printf("x[%d] = ", i);
      scanf("%f", &x[i]);
   }

   printf("\nEnter the no. of iteration : ");
   scanf("%d", &q);

   while (q > 0) {
      for (i = 0; i < n; i++) {
         y[i] = (b[i] / a[i][i]);
         for (j = 0; j < n; j++) {
            if (j == i)
               continue;
            y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
            x[i] = y[i];
         }
         printf("\nx[%d] = %.2f ", i + 1, y[i]);
      }
      printf("\n");
      q--;
   }
   return 0;
}
Editor is loading...
Leave a Comment