2 years ago

#38786

test-img

Dimitris Minagias

How can I input and load vectors and matrices from files to my main program?

I am trying to create a program that reads vectors or even matrices from files and perform operations on and between them, with the results stored in files. Here is the code of the project so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define pi 3.141

float determinant(float [][25], float);
void cofactor(float [][25], float);
void transpos(float [][25], float [][25], float);


// function to add two 3x3 matrix
void add(int m[10][10], int n[10][10], int sum[10][10],int q)
{
  for(int i=0;i<q;i++)
    for(int j=0;j<q;j++)
      sum[i][j] = m[i][j] + n[i][j];
}

// function to subtract two 3x3 matrix
void subtract(int m[10][10], int n[10][10], int result[10][10],int q)
{
  for(int i=0;i<q;i++)
    for(int j=0;j<q;j++)
      result[i][j] = m[i][j] - n[i][j];
}

// function to multiply two 3x3 matrix
void multiply(int m[10][10], int n[10][10], int result[10][10],int q)
{
  for(int i=0; i < q; i++)
  {
    for(int j=0; j < q; j++)
    {
      result[i][j] = 0; // assign 0
      // find product
      for (int k = 0; k < q; k++)
      result[i][j] += m[i][k] * n[k][j];
    }
  }
}

// function to find transpose of a 3x3 matrix
void transpose(int matrix[10][10], int trans[10][10],int q)
{
  for (int i = 0; i < q; i++)
    for (int j = 0; j < q; j++)
      trans[i][j] = matrix[j][i];
}

// function to display 3x3 matrix
void display(int matrix[10][10],int q)
{
  for(int i=0; i<q; i++)
  {
    for(int j=0; j<q; j++)
      printf("%d\t",matrix[i][j]);

    printf("\n"); // new line
  }
}

float magnitude(int a[10],int s)
{
int i,c=0;
float d;
for (i = 0; i < s; i++)
    for (i = 0; i < s; i++)
    {
        c = c +pow(a[i],2);
    }
      d=sqrt(c);
      return d;
}

float dotprod(int a[10],int b[10],int s)
{ int i;
float j;
    for(i=0;i<s;i++)
        {
            j=j+a[i]*b[i];
            }
            return j;
}
void cofactor(float num[25][25], float f)
{
 float b[25][25], fac[25][25];
 int p, q, m, n, i, j;
 for (q = 0;q < f; q++)
 {
   for (p = 0;p < f; p++)
    {
     m = 0;
     n = 0;
     for (i = 0;i < f; i++)
     {
       for (j = 0;j < f; j++)
        {
          if (i != q && j != p)
          {
            b[m][n] = num[i][j];
            if (n < (f - 2))
             n++;
            else
             {
               n = 0;
               m++;
               }
            }
        }
      }
      fac[q][p] = pow(-1, q + p) * determinant(b, f - 1);
    }
  }
  transpos(num, fac, f);
}

float determinant(float g[25][25], float k)
{
  float s = 1, det = 0, b[25][25];
  int i, j, m, n, c;
  if (k == 1)
    {
     return (g[0][0]);
    }
  else
    {
     det = 0;
     for (c = 0; c < k; c++)
       {
        m = 0;
        n = 0;
        for (i = 0;i < k; i++)
          {
            for (j = 0 ;j < k; j++)
              {
                b[i][j] = 0;
                if (i != 0 && j != c)
                 {
                   b[m][n] = g[i][j];
                   if (n < (k - 2))
                    n++;
                   else
                    {
                     n = 0;
                     m++;
                     }
                   }
               }
             }
          det = det + s * (g[0][c] * determinant(b, k - 1));
          s = -1 * s;
          }
    }

    return (det);
}

void transpos(float num[25][25], float fac[25][25], float r)
{
  int i, j;
  float b[25][25], inverse[25][25], d;

  for (i = 0;i < r; i++)
    {
     for (j = 0;j < r; j++)
       {
         b[i][j] = fac[j][i];
        }
    }
  d = determinant(num, r);
  for (i = 0;i < r; i++)
    {
     for (j = 0;j < r; j++)
       {
        inverse[i][j] = b[i][j] / d;
        }
    }
   printf("\n\n\nThe inverse of matrix is : \n");

   for (i = 0;i < r; i++)
    {
     for (j = 0;j < r; j++)
       {
         printf("\t%.2f", inverse[i][j]);
        }
    printf("\n");
     }
}
int main()
{
    int arr1[10][10];
  int det=0;
float g[25][25], k, de;
    int a[10],b[10],c[10];
    int choice,n,i,option;
float z,cos;
int u1, u2, u3, v1, v2, v3;
int uvi, uvj, uvk;
int d[10][10],j ;
  int e[10][10];
  int f[10][10];

  printf("Choose an option\n");
  printf("1. Matrices\n2. Vectors\n");
  scanf("%d",&option);
  switch(option)
  {
  case(1):
    {

  // variable to take choice
  int choice;

  // menu-driven
  do
  {
    // menu to choose the operation
    printf("\nChoose the matrix operation,\n");
    printf("----------------------------\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Transpose\n");
    printf("5. Determinant\n");
    printf("6. Inverse\n");
    printf("7. Exit\n");
    printf("----------------------------\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
      case 1:
          printf("Enter the order of the Matrix :\n ");
  scanf("%f", &k);
          printf("Input elements in the first matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&d[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",d[i][j]);
        printf("\n");
     }
     printf("Input elements in the second matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&e[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",e[i][j]);
        printf("\n");
     }
        add(d, e, f,k);
        printf("Sum of matrix: \n");
        display(f,k);
        break;
      case 2:
          printf("Enter the order of the Matrix : ");
  scanf("%f", &k);
          printf("Input elements in the first matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&d[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",d[i][j]);
        printf("\n");
     }
     printf("Input elements in the second matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&e[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",e[i][j]);
        printf("\n");
     }
        subtract(d, e, f,k);
        printf("Subtraction of matrix: \n");
        display(f,k);
        break;
      case 3:
          printf("Enter the order of the Matrix :\n ");
  scanf("%f", &k);
          printf("Input elements in the first matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&d[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",d[i][j]);
        printf("\n");
     }
     printf("Input elements in the second matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&e[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",e[i][j]);
        printf("\n");
     }
        multiply(d, e, f,k);
        printf("Multiplication of matrix: \n");
        display(f,k);
        break;
      case 4:
          printf("Enter the order of the Matrix : ");
  scanf("%f", &k);
          printf("Input elements in the matrix :\n");
       for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&d[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("% 4d",d[i][j]);
        printf("\n");
     }
        printf("Transpose of the  matrix: \n");
        transpose(d, f,k);
        display(f,k);
        break;
        case 5:
            {
                 printf("\n\nCalculate the determinant of a 3 x 3 matrix :\n");
       printf("-------------------------------------------------\n");

     printf("Input elements in the first matrix :\n");
       for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
               printf("element - [%d],[%d] : ",i+1,j+1);
               scanf("%d",&arr1[i][j]);
            }
        }
     printf("The matrix is :\n");
     for(i=0;i<3;i++)
     {
       for(j=0;j<3 ;j++)
         printf("% 4d",arr1[i][j]);
        printf("\n");
     }

  for(i=0;i<3;i++)
      det = det + (arr1[0][i]*(arr1[1][(i+1)%3]*arr1[2][(i+2)%3] - arr1[1][(i+2)%3]*arr1[2][(i+1)%3]));

  printf("\nThe Determinant of the matrix is: %d\n\n",det);
  break;
            }
        case 6:
            {
                 printf("Enter the order of the Matrix : ");
  scanf("%f", &k);
  printf("Enter the elements of %.0fX%.0f Matrix : \n", k, k);
  for (i = 0;i < k; i++)
    {
     for (j = 0;j < k; j++)
       {
           printf("element - [%d],[%d] : ",i+1,j+1);
        scanf("%f", &g[i][j]);
        }
    }
    printf("The matrix is :\n");
     for(i=0;i<k;i++)
     {
       for(j=0;j<k ;j++)
         printf("%.0f ",g[i][j]);
        printf("\n");
     }
  de= determinant(g, k);
  if (de == 0)
   printf("\nInverse of Entered Matrix is not possible\n");
  else
   cofactor(g, k);
   break;
}

            }
      case 7:
        printf("Thank You.\n");
        exit(0);
      default:
        printf("Invalid input.\n");
        printf("Please enter the correct input.\n");
        exit(0);
    }while(1);
    }


      case(2):
        {
           do
  {
    // menu to choose the operation
    printf("\nChoose the vector operation,\n");
    printf("----------------------------\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Dot Product\n");
    printf("4. Cross Product 3x3\n");
    printf("5. Magnitude\n");
    printf("6. Angle\n");
    printf("7. Exit\n");
    printf("----------------------------\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
  switch(choice)
  {
  case(1):
    {
        printf("Enter vector size: ");
 scanf("%d", &n);
 printf("Enter elements of vector a:\n");
 for (i = 0; i < n; i++)
   scanf("%d", &a[i]);
   printf("Enter elements of vector b:\n");
   for (i = 0; i < n; i++)
    scanf("%d", &b[i]);
    printf("Addition vector:\n");
     for (i = 0; i < n; i++)
        c[i]=a[i]+b[i];
   for (i = 0; i < n; i++)
      printf("%d ", c[i]);

    break;
  }
  case(2) :
    {
        printf("Enter vector size: ");
 scanf("%d", &n);
 printf("Enter elements of vector a:\n");
 for (i = 0; i < n; i++)
   scanf("%d", &a[i]);
   printf("Enter elements of vector b:\n");
   for (i = 0; i < n; i++)
    scanf("%d", &b[i]);
    printf("Subtraction vector:\n");
    for (i = 0; i < n; i++)
        c[i]=a[i]-b[i];
   for (i = 0; i < n; i++)
      printf("%d ", c[i]);
    break;
    }
    case(3):
        {
            printf("Enter vector size: ");
 scanf("%d", &n);
 printf("Enter elements of vector a:\n");
 for (i = 0; i < n; i++)
   scanf("%d", &a[i]);
   printf("Enter elements of vector b:\n");
   for (i = 0; i < n; i++)
    scanf("%d", &b[i]);
    printf("Dot product of vectors:\n");
    printf("%.0f",dotprod(a,b,n));
    break;
        }
    case(4):
        {
            printf("Enter elements of vector a:\n");
   scanf("%d", &u1);
   scanf("%d", &u2);
   scanf("%d", &u3);
   printf("Enter elements of vector b:\n");
   scanf("%d", &v1);
   scanf("%d", &v2);
   scanf("%d", &v3);


    uvi = u2 * v3 - v2 * u3;
    uvj = v1 * u3 - u1 * v3;
    uvk = u1 * v2 - v1 * u2;

 printf("The cross product of the 2 vectors \n u = %di + %dj + %dk and \n v = %di + %dj + %dk\n",
            u1, u2, u3, v1, v2, v3);
    printf(" u X v: %di  %dj  %dk", uvi, uvj, uvk);
    break;
        }
    case(5):
        {
            printf("Enter vector size: ");
 scanf("%d", &n);
 printf("Enter elements of vector a:\n");
 for (i = 0; i < n; i++)
   scanf("%d", &a[i]);
   printf("Magnitude of vector:\n");
      printf("%0.2f ", magnitude(a,n));
      break;
        }
    case(6):
        {
            printf("Enter vector size: ");
 scanf("%d", &n);
 printf("Enter elements of vector a:\n");
 for (i = 0; i < n; i++)
   scanf("%d", &a[i]);
   printf("Enter elements of vector b:\n");
   for (i = 0; i < n; i++)
    scanf("%d", &b[i]);
    cos=dotprod(a,b,n)/(magnitude(a,n)*magnitude(b,n));
  k=acos(cos);
    z=(k*180)/pi;
    printf("the angle between the vectors is:%.1f degrees",z);
    break;
        }
    case(7):
        {
            printf("Thank You.\n");
        exit(0);
        }
    default:
        {
            printf("Invalid input.\n");
        printf("Please enter the correct input.\n");
       exit(0);
        }
  }
  }while(1);
        }

  }
  return 0;
}

My problem is that I cannot find a way to load and save data to my project, so any advice would be appreciated.

c

save

load

0 Answers

Your Answer

Accepted video resources