Advertisement
narenarya

Akhil assignment 0

Jan 31st, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.32 KB | None | 0 0
  1. //CS-620 programming Assignment 0 by Akhil Bandari.Date: 30-01-2-15.
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <malloc.h>
  6. #include <string.h>
  7.  
  8. void main(int argc , char *argv[])
  9.  
  10. {
  11.     //Check file is supplied or not
  12.     if (argc!=2)
  13.     {
  14.  
  15.         printf("Error!.Filename is missing\n");
  16.     }
  17.  
  18.     //If file is supplied go on....
  19.  
  20.     else
  21.     {
  22.         //File handling code goes here
  23.         FILE *file = fopen( argv[1], "r");
  24.  
  25.         //Check whether file opened correctly or not
  26.         if(file == 0)
  27.         {
  28.             printf("Couldn't open file. Give correct file.\n");
  29.  
  30.         }
  31.         //If opened correctly start process of building matrices
  32.         else
  33.         {  
  34.                //Create tools for reading from file
  35.  
  36.                size_t len = 0;
  37.                ssize_t read;
  38.                char * line = NULL;
  39.                char *split;
  40.                const char*s = "\t";
  41.                int line_number = 0, n_by_n , n_minus_one_by_n_minus_one , drop_row_column, i , j;
  42.  
  43.                //Declare matrices of type double
  44.                //A -original Matrix
  45.                //B -modified Matrix
  46.  
  47.                double **A,**B ;
  48.  
  49.  
  50.                while ((read = getline(&line, &len, file)) != -1)
  51.                {
  52.                     line_number+=1;
  53.                    
  54.                     //If it is a beginning line of file
  55.                     if (strlen(line) == 2 && line_number == 1)
  56.                     {
  57.                             n_by_n =  atoi(line);
  58.  
  59.                             //Allocating memory for original matrix A
  60.  
  61.                             A = (double **)malloc(n_by_n * sizeof(double));
  62.                            
  63.                             for (i=0; i<n_by_n; i++)
  64.                             {
  65.                                 A[i] = (double *)malloc(n_by_n * sizeof(double));
  66.                             }
  67.                     }
  68.  
  69.                     //else it is a Matrix row
  70.                     else if(strlen(line) > 2)
  71.                     {
  72.  
  73.  
  74.                             /* get the first token */
  75.                                split = strtok(line, s);
  76.                                i = line_number - 2;
  77.                                j = 0;
  78.                                
  79.                             /* walk through other tokens */
  80.                             while( split != NULL )
  81.                             {
  82.                                   //Store each splitted token into Matrix A
  83.                                   A[i][j] = atof(split);
  84.                                   j++;
  85.                                   split = strtok(NULL, s);
  86.                             }
  87.  
  88.                     }
  89.                    
  90.                     //It is the last line of file.Here start building Matrix B
  91.                     else if(strlen(line) == 2)
  92.                     {
  93.  
  94.                         n_minus_one_by_n_minus_one = n_by_n -1 ;
  95.  
  96.                         drop_row_column = atoi(line);
  97.  
  98.                         //Allocating memory for matrix B
  99.                         B = (double **)malloc(n_minus_one_by_n_minus_one * sizeof(double));
  100.                            
  101.                         for (i=0; i<n_minus_one_by_n_minus_one ; i++)
  102.                         {
  103.                                 B[i] = (double *)malloc(n_minus_one_by_n_minus_one * sizeof(double));
  104.                         }
  105.  
  106.                         //Copy values of Matrix A into Matrix B dropping the required row and column
  107.  
  108.  
  109.  
  110.                         for (i = 0 ; i < n_by_n ; i++)
  111.                         {
  112.                             if (i != drop_row_column)
  113.                             {
  114.                                 for (j = 0 ; j < n_by_n ; j++)
  115.                                 {
  116.                                     if (j != drop_row_column)
  117.                                     {
  118.                                         if (i < drop_row_column && j < drop_row_column)
  119.                                             B[i][j] = A[i][j];
  120.                                         else if (i < drop_row_column && j > drop_row_column)
  121.                                             B[i][j-1] = A[i][j];
  122.                                         else if (i > drop_row_column && j < drop_row_column)
  123.                                             B[i-1][j]=A[i][j];
  124.                                         else
  125.                                             B[i-1][j-1]=A[i][j];
  126.                                     }
  127.                                 }
  128.                             }
  129.                         }
  130.                     }
  131.  
  132.                }
  133.  
  134.           //Beginning the printing of formatted output
  135.           printf("Length: %d\n",n_by_n);
  136.           printf("Original matrix:\n");
  137.  
  138.          //loop through original Matrix A
  139.           for (i = 0 ; i <  n_by_n ; i++)
  140.           {
  141.                 for (j = 0; j < n_by_n; j++)
  142.                 {
  143.                     printf("%.4lf   ", A[i][j]);
  144.                 }
  145.                 printf("\n");
  146.           }
  147.  
  148.           printf("Deleting row and column %d\n",drop_row_column);
  149.  
  150.           printf("New matrix:\n");
  151.           //loop through newly created matrix B
  152.  
  153.           for (i = 0 ; i <  n_minus_one_by_n_minus_one ; i++)
  154.           {
  155.                 for (j = 0 ; j < n_minus_one_by_n_minus_one ; j++)
  156.                 {
  157.                     printf("%.4lf   ", B[i][j]);
  158.                 }
  159.                 printf("\n");
  160.           }
  161.  
  162.  
  163.          //Close the file for house keeping
  164.          fclose(file);
  165.  
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement