Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CS-620 programming Assignment 0 by Akhil Bandari.Date: 30-01-2-15.
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <string.h>
- void main(int argc , char *argv[])
- {
- //Check file is supplied or not
- if (argc!=2)
- {
- printf("Error!.Filename is missing\n");
- }
- //If file is supplied go on....
- else
- {
- //File handling code goes here
- FILE *file = fopen( argv[1], "r");
- //Check whether file opened correctly or not
- if(file == 0)
- {
- printf("Couldn't open file. Give correct file.\n");
- }
- //If opened correctly start process of building matrices
- else
- {
- //Create tools for reading from file
- size_t len = 0;
- ssize_t read;
- char * line = NULL;
- char *split;
- const char*s = "\t";
- int line_number = 0, n_by_n , n_minus_one_by_n_minus_one , drop_row_column, i , j;
- //Declare matrices of type double
- //A -original Matrix
- //B -modified Matrix
- double **A,**B ;
- while ((read = getline(&line, &len, file)) != -1)
- {
- line_number+=1;
- //If it is a beginning line of file
- if (strlen(line) == 2 && line_number == 1)
- {
- n_by_n = atoi(line);
- //Allocating memory for original matrix A
- A = (double **)malloc(n_by_n * sizeof(double));
- for (i=0; i<n_by_n; i++)
- {
- A[i] = (double *)malloc(n_by_n * sizeof(double));
- }
- }
- //else it is a Matrix row
- else if(strlen(line) > 2)
- {
- /* get the first token */
- split = strtok(line, s);
- i = line_number - 2;
- j = 0;
- /* walk through other tokens */
- while( split != NULL )
- {
- //Store each splitted token into Matrix A
- A[i][j] = atof(split);
- j++;
- split = strtok(NULL, s);
- }
- }
- //It is the last line of file.Here start building Matrix B
- else if(strlen(line) == 2)
- {
- n_minus_one_by_n_minus_one = n_by_n -1 ;
- drop_row_column = atoi(line);
- //Allocating memory for matrix B
- B = (double **)malloc(n_minus_one_by_n_minus_one * sizeof(double));
- for (i=0; i<n_minus_one_by_n_minus_one ; i++)
- {
- B[i] = (double *)malloc(n_minus_one_by_n_minus_one * sizeof(double));
- }
- //Copy values of Matrix A into Matrix B dropping the required row and column
- for (i = 0 ; i < n_by_n ; i++)
- {
- if (i != drop_row_column)
- {
- for (j = 0 ; j < n_by_n ; j++)
- {
- if (j != drop_row_column)
- {
- if (i < drop_row_column && j < drop_row_column)
- B[i][j] = A[i][j];
- else if (i < drop_row_column && j > drop_row_column)
- B[i][j-1] = A[i][j];
- else if (i > drop_row_column && j < drop_row_column)
- B[i-1][j]=A[i][j];
- else
- B[i-1][j-1]=A[i][j];
- }
- }
- }
- }
- }
- }
- //Beginning the printing of formatted output
- printf("Length: %d\n",n_by_n);
- printf("Original matrix:\n");
- //loop through original Matrix A
- for (i = 0 ; i < n_by_n ; i++)
- {
- for (j = 0; j < n_by_n; j++)
- {
- printf("%.4lf ", A[i][j]);
- }
- printf("\n");
- }
- printf("Deleting row and column %d\n",drop_row_column);
- printf("New matrix:\n");
- //loop through newly created matrix B
- for (i = 0 ; i < n_minus_one_by_n_minus_one ; i++)
- {
- for (j = 0 ; j < n_minus_one_by_n_minus_one ; j++)
- {
- printf("%.4lf ", B[i][j]);
- }
- printf("\n");
- }
- //Close the file for house keeping
- fclose(file);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement